为什么我的Java程序不能从Arduino获取串行信息?

时间:2013-12-07 21:13:35

标签: 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 to a potentiometer,然后将其连接到其中一个模拟输入引脚,有效地创建了variable voltage divider。此代码成功提取第一个整数数据,并将其绘制为连续的水平线,无法获取任何其他数据。我已经检查过,它确实输入了if (myPort.availible > 0 ),但newval没有改变。 Arduino 表现完美,我通过Arduino IDE检查了串行数据,看起来表现不错,因此必须是程序问题。请帮忙!

编辑:我一直在努力。一个建议是实现一个连续的功能。这产生了相同的结果。

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);
}

2 个答案:

答案 0 :(得分:1)

我不知道串口编程是如何工作的。但我认为我发现可能会出现这个问题:

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

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

如果temp具有空值,则newval将导致空指针异常。 尝试在try catch循环周围设置if块,如下所示:

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();
}

现在,如果遇到Null指针异常,则表示您的端口不是'\ n'字符 您希望解析的可能字符串的结尾。在这种情况下,我想你必须在推送数据的同时自愿添加新行字符。

我真的不知道端口和东西。但如果以上描述可以帮助您摆脱这个问题,我会很高兴。

答案 1 :(得分:0)

您可以尝试使用serialEvent吗?

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();
  stroke(255);
  strokeWeight(1);
  try{
    String portName = Serial.list()[0];
    myPort = new Serial(this, portName, 9600);
    myPort.bufferUntil(10);//ASCII linefeed 
  }catch(Exception e){
    System.err.println("Error opening serial device\nPlease check if the device is connected !");
  }
  //Initilizing vals 
  vals = new float [width];//initializez with 0s by default

}


void draw() {
  background(0);

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


}

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

    temp = p.readString().trim(); 
    newval = Integer.parseInt(temp);

    vals[vals.length - 1] = newval;                              //Set the last space in vals to newval
    println(newval);                                             //Print newval for debugging purposes
  }catch(Exception e){
    println("error parsing serial data: "+temp);
  }
}