【问题标题】:Why isn't my Java program getting serial information from the Arduino?为什么我的 Java 程序没有从 Arduino 获取串行信息?
【发布时间】:2013-12-07 21:13:35
【问题描述】:
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 &gt; 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);
}

【问题讨论】:

  • 您能否也发布您的 Arduino 代码,或者至少发布您将数据写入串行的部分,以便更容易测试/调试?

标签: java serial-port arduino processing


【解决方案1】:

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

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

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

我真的对端口和东西一无所知。但如果以上描述能帮助您摆脱问题,我会很高兴。

【讨论】:

  • 非常感谢!这不是空指针异常,但是在测试您的理论时,我找到了答案。就像我说的,没有空指针,所以我知道它必须是 Arduino 代码(C 变体)。我有一些 Arduino 代码确实有效,所以我分析了它并意识到缺少一行基本代码:delay(25);那解决了它。非常感谢您让我踏上了解决问题的道路。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多