【问题标题】:Arduino and Processing. Error mismatchArduino 和处理。错误不匹配
【发布时间】:2016-11-11 13:22:25
【问题描述】:

我的 Arduino 代码:

#include<EngduinoThermistor.h>
void setup()
{
  Serial.begin(9600);
  EngduinoThermistor.begin();
}void loop()
{
  float temp;
  temp = EngduinoThermistor.temperature();
  Serial.println(temp);
  delay(1000);
}

我的处理代码:

 import processing.serial.*;
    Serial port;
    float x = 0;

void setup() {
  size(600, 200);
  smooth();
  background(#9F9694);
  String port = Serial.list()[3];
  port = new Serial(this, "/dev/tty.usbmodem1411", 9600);
}

void draw() {
  stroke(50, 50, 50);
  float inByte = port.read();
  stroke(#791F33);
  strokeWeight(3);
  line(x, height, x, height-inByte); 
  print(inByte);
  if (x >=width) {
    x=0;
    background(255);
  }
  x++;
}

在这里,我无法将我的 arduino 上的数据发送到处理,因为总是有一个错误说:Type mismatch processing.serial.Serial does not match with java.lang.String because of the two statements:

  String port = Serial.list()[3];
  port = new Serial(this, "/dev/tty.usbmodem1411", 9600);

我在网上查了一下,但每个人都使用相同的代码来连接 arduino 和处理。我的处理代码有什么问题? arduino 代码工作正常。

【问题讨论】:

    标签: c++ arduino processing


    【解决方案1】:

    这没有意义:

    String port = Serial.list()[3];
    port = new Serial(this, "/dev/tty.usbmodem1411", 9600);
    

    您正在创建一个名为portString 变量。这意味着port 变量只能 指向String 值。然后在下一行中,您尝试将其指向Serial 的实例。 Serial 的实例不是 String 的实例,这就是您收到错误的原因。

    您需要将String 变量和Serial 变量拆分为两个单独的变量。像这样的:

    String portString = Serial.list()[3];
    Serial portSerial = new Serial(this, "/dev/tty.usbmodem1411", 9600);
    

    还请注意,the Serial documentation 中的每个示例都是这样做的。

    【讨论】:

    • 我已经更正了。但是程序仍然无法运行...它无法从arduino读取数据,我不知道为什么...
    【解决方案2】:

    Kevin 出色地解释了您遇到的语法错误是什么意思以及如何解决它。

    除此之外,我还想指出我在Simon's question 中发现的一个问题:

    float inByte = port.read();

    根据 Serial 在read() 上的文档:

    为缓冲区中等待的下一个字节返回一个介于 0 和 255 之间的数字。如果没有字节,则返回 -1,尽管应该通过首先检查 available() 来查看数据是否可用来避免这种情况。

    有几个问题:

    1. EngduinoThermistor 以浮点数形式返回温度,这不是 read() 返回的值。它一次返回一个字节。假设温度为25.71。这将是 5 个字节(2 5 . 7 1 表示从 0 到 255 的字节将是 50 53 46 55 49)。您需要将每个字节转换为一个字符,然后将每个字符附加到一个字符串,直到找到新行 (\n) 字符,然后字节计数器将重置。将完整的字符串放在一起后,可以将其转换为浮点数。我建议使用readStringUntil()bufferUntil()readString() 的组合
    2. 您没有检查 read() 是否返回 -1,除非您使用 serialEvent(),否则您应该始终这样做

    我建议首先仔细检查数据是否在 Arduino 的串行监视器中正确显示。如果是,请关闭串行监视器(因为您一次可以打开一个到串行端口的连接),然后运行您的 Arduino 代码。

    您还可以利用这个奇怪的技巧:Arduino 已经知道您正在使用的串行端口和波特率并将其保存为首选项(因此您不必每次重新启动它时都选择端口)。这实际上存储在 Java Properties format 中,您可以在 Processing 中轻松解析。

    您可以从 Arduino Preferences 面板轻松找到此文件的位置:

    这是一个基本草图,它尝试从 Arduino 首选项中读取串行端口名称和波特率,然后读取一个字符串并将其打印到控制台(同时尝试处理一些错误):

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    import processing.serial.*;
    
    String portName = null;
    int baudRate = 9600;
    
    Serial arduino;
    
    void setup(){
      readArduinoPrefs("/Users/George/Library/Arduino15/preferences.txt");
      println("last used Arduino port",portName,"with baud rate",baudRate);
    
      try{
        arduino = new Serial(this,portName,baudRate);
      }catch(Exception e){
        System.err.println("ERROR connecting to Arduino port " + portName);
        String message = e.getMessage();
        if(message.contains("not found")) println("Please make sure your Arduino is connected via USB!");
        if(message.contains("busy")) println("Please make sure the port isn't already opened/used by Serial Monitor or other programs!");
        e.printStackTrace();
        exit();
      }
    }
    void draw(){
    }
    void serialEvent(Serial s){
      println("received string",s.readStringUntil('\n'));
    }
    void readArduinoPrefs(String prefsPath){
      Properties prop = new Properties();
      InputStream input = null;
    
      try {
    
        input = new FileInputStream(prefsPath);
    
        prop.load(input);
    
        portName = prop.getProperty("serial.port");
        baudRate = int(prop.getProperty("serial.debug_rate"));
    
      } catch (IOException ex) {
        ex.printStackTrace();
      } finally {
        if (input != null) {
          try {
            input.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    
    }
    

    在实用方面,您始终可以在 Arduino 中手动配置时输入端口名称和波特率,只要记住仔细检查syntax,就您而言:

    Serial(parent, portName, baudRate)
    

    例如

       new Serial(this, "/dev/tty.usbmodem1411", 9600);
    

    正如凯文指出的那样

    【讨论】:

      猜你喜欢
      • 2011-03-28
      • 2017-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-04
      • 1970-01-01
      相关资源
      最近更新 更多