当前位置: 首页 > 知识库问答 >
问题:

Arduino和加工。错误不匹配

汪天宇
2023-03-14

我的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上的数据发送到processing,因为总是会出现一个错误:类型不匹配处理。电视连续剧Serial与java不匹配。lang.String,因为有两条语句:

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

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

共有2个答案

郗河
2023-03-14

凯文在解释语法错误的含义和解决方法方面做得很好。

除此之外,我还想指出我在西蒙的问题中发现的一个问题:

浮动字节=端口。read();

根据read()上的序列号文件:

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

有几个问题:

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

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

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

这是一个基本草图,它尝试从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中手动配置端口名称和波特率时,您始终可以输入端口名称和波特率,只要记住仔细检查语法,在您的情况下:

Serial(parent, portName, baudRate)

例如

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

正如凯文指出的

柏麒
2023-03-14

这毫无意义:

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

您正在创建一个名为端口字符串变量。这意味着端口变量只能指向字符串值。然后在下一行中,尝试将其指向Serial的一个实例。Serial的实例不是String的实例,这就是为什么会出现错误。

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

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

还请注意,这正是串行文档中的每个示例所做的。

 类似资料:
  • 我正在homedepot.com练习,现在我有这样的问题卡住了,错误信息是:“Arity不匹配:步骤定义'StepsTestCase.SearchShoppingStep.click文件中的_close_button_in_add_to_cart_window(WebDriver):/C:/用户/管理员/git/MavenProject/目标/类/'带有模式[^在添加到购物车窗口中单击关闭按钮$]

  • 问题出在行collections.sort(acoesProcessar);我得到的信息是: 绑定不匹配:类型集合的泛型方法不适用于参数()。推断的类型不是有界参数

  • 我让我的应用程序工作正常,然后处理了这个xml文件中的颜色,现在出现了这个错误,我将发布带有错误信息的xml文件代码。我该如何解决这个问题? 这是错误 E/EGL_emulation:tid 2805:eglSurfaceAttrib(1146):错误0x3009(EGL_BAD_MATCH)W/OpenGLRenler:未能在表面上设置EGL_SWAP_BEHAVIOR0x942c4ba0,错误

  • 问题内容: 我收到以下错误: 我正在尝试登录并返回一个HomePage(如果成功),以及一个LoginPage(如果不使用泛型)。 我有一个基类,Page,由SecuredPage扩展,用于登录墙后面的页面。我写了一个通用的辅助方法,可以构造任何类型的页面。LoginPage上的登录方法使用此方法。LoginPage扩展Page,而HomePage扩展SecuredPage。SecuredPage

  • 问题内容: 正则表达式后给我错误 请求字符串在哪里 任何帮助,将不胜感激。 问题答案: 尚未尝试匹配。先致电再致电。 输出:

  • 问题内容: 我试图在Swift中使用JSONDecoder将JSON转换为Structs,所以我编写了所有Structs,将它们修改了几个小时,但它仍然给我这个错误。我不知道是否有办法查看给出此信息的行。我将在下面发布我的struct,并在其后发布Json File链接。 完整的错误描述是: typeMismatch(Swift.Dictionary ,Swift.DecodingError.Co