java - 为什么我的 Java 程序没有从 Arduino 获取串行信息?

标签 java serial-port arduino processing

import processing.serial.*;
int newval = 0;      //Varible for feeding values onto the end of the 
Serial myPort;
String temp = "0";   //Temporary varible for pulling Strings off the serial connection
float[] vals;        //Array of data to be graphed

void setup() {
  //Setting up size and the serial port
  size(1920,1080);
  smooth();
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);

  //Initilizing vals 
  vals = new float [width];

  //Setting everything in vals to 0
  for (int i = 0; i < vals.length - 1;i++)
  {
    vals [i] = 0;
  }

}


void draw() {

  background(0);

  //Drawing function
  for (int i = 0; i < vals.length - 1; i += 1)
  {
    stroke(255);
    strokeWeight(1);
    line(i,vals[i], i+1, vals[i+1]);
  }

  //Push everything in vals back one slot
  for (int i = 0; i < vals.length - 1; i++)
  {
    vals[i] = vals[i+1];
  }

  //Pull data from the serial connection
  if ( myPort.available() > 0) 
  {
    temp = (new String(myPort.readBytesUntil('\n'))).trim(); //Take a string off of the serial 
                                                             //port and remove any spaces
    newval = Integer.parseInt(temp);                         //Convert temp to an integer
  }

  vals[vals.length - 1] = newval;                            //Set the last space in vals to newval
  println(newval);                                           //Print newval for debugging purposes

}

上面是我正在使用的代码。

我已将我的 Arduino 连接到一个电位器,然后将其连接到一个模拟输入引脚,有效地创建了一个可变分压器。此代码成功提取了第一个整数数据,并将其绘制为一条连续的水平线,未能获取任何其他数据。我检查过,它确实输入了 if (myPort.availible > 0 )newval 没有改变。 Arduino 运行良好,我通过 Arduino IDE 检查了串行数据,似乎运行良好,因此这一定是程序问题。请帮忙!

编辑:我一直在努力。一个建议是实现一个 serialevent 函数。这产生了相同的结果。

Arduino 代码:

#define PIN A0

void setup()
{
  Serial.begin(9600);
}

void loop ()
{
  Serial.print(analogRead(PIN));
  Serial.print('\n');
}

新的(有效的)Arduino 代码:

#define PIN A0

void setup()
{
  Serial.begin(9600);
}

void loop ()
{
  Serial.print(analogRead(PIN));
  Serial.print('\n');
  delay(20);
}

最佳答案

我不知道串行编程是如何工作的。但我想我看到了一个可能出现这个问题的地方:

temp = (new String(myPort.readBytesUntil('\n'))).trim();

这里,readBytesUntil 实际上试图找到换行符。如果找不到该字符,则返回 null。

如果temp有空值,newval会导致空指针异常。 尝试在 if 循环周围放置一个 try catch block ,如下所示:

try
{
  if ( myPort.available() > 0) 
  {
    temp = (new String(myPort.readBytesUntil('\n'))).trim();   //Take a string off of the serial     
                                                                 port and remove any spaces
    newval = Integer.parseInt(temp);                            //Convert temp to an integer
  }
}
catch(NullPointerException e)
{
 e.printStackTrace();
}

现在如果遇到空指针异常,说明你的端口没有'\n'字符 您希望解析的可能字符串的末尾。在那种情况下,我想你必须在推送数据时自愿添加换行符。

我真的对端口之类的东西一无所知。但是,如果以上描述可以帮助您解决问题,我将很高兴。

关于java - 为什么我的 Java 程序没有从 Arduino 获取串行信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20446714/

相关文章:

java - Java 中 vector 不随 "AES/CBC/pkcs7padding"变化

java - Eclipse 插件 - 创建带有表的 View

java - Swing:非输入文本字段

vba - 通过串口excel-vba发送字节数组失败

c++ - Qt Serial Port - 一致读取数据

linux - 使用 picocom - 发送请求

c - Arduino 读取多个 onewire 传感器

java - JPA,三个实体,它们的关系。 ¿CascadeType 是否正确删除?

air - Adobe AIR Android 蓝牙串行数据通信

C++ float 到 stringstream 的字符串转换失败