繁体   English   中英

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

[英]Why isn't my Java program getting serial information from the Arduino?

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检查了串行数据,看起来表现良好,因此它一定是程序问题。 请帮忙!

编辑:我一直在尝试。 一个建议是实现一个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块,如下所示:

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'字符。 在这种情况下,我想您必须在推送数据时自愿添加换行符。

我真的对港口和东西一无所知。 但是,如果上面的描述可以帮助您摆脱困境,我将很高兴。

您可以尝试使用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);
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM