为什么不是';我的Java程序不能从Arduino获取序列信息吗?
新(工作)Arduino代码:为什么不是';我的Java程序不能从Arduino获取序列信息吗?,java,serial-port,arduino,processing,Java,Serial Port,Arduino,Processing,新(工作)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 d
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
}
#define PIN A0
void setup()
{
Serial.begin(9600);
}
void loop ()
{
Serial.print(analogRead(PIN));
Serial.print('\n');
}
我不知道串行编程是如何工作的。但我认为我看到了一个可能出现这个问题的地方:
#define PIN A0
void setup()
{
Serial.begin(9600);
}
void loop ()
{
Serial.print(analogRead(PIN));
Serial.print('\n');
delay(20);
}
这里,readBytesUntil
实际尝试查找新行字符。如果无法找到字符,则返回null
如果temp
具有空值,则newval将导致空指针异常。
尝试在该if
循环周围设置Try catch
块,如下所示:
temp = (new String(myPort.readBytesUntil('\n'))).trim();
现在,如果遇到空指针异常,则意味着您的端口不包含“\n”字符
要解析的可能字符串的结尾。在这种情况下,我认为您必须在推送数据时自愿添加新行字符
我真的对港口之类的东西一无所知。但是,如果上述描述能帮助您摆脱这个问题,我会很高兴。您能尝试使用吗
import processing.serial.*;
int-newval=0//变量,用于将值输入到
串行端口;
字符串temp=“0”//用于从串行连接上拔下管柱的临时变量
浮动[]VAL//要绘制图形的数据数组
无效设置(){
//设置大小和串行端口
规模(19201080);
光滑的();
中风(255);
冲程重量(1);
试一试{
字符串portName=Serial.list()[0];
myPort=新序列号(此端口名为9600);
myPort.bufferUntil(10);//ASCII换行符
}捕获(例外e){
System.err.println(“打开串行设备时出错\n请检查设备是否已连接!”);
}
//初始化VAL
VAL=new float[width];//默认情况下初始化为0
}
作废提款(){
背景(0);
//绘图功能
对于(int i=0;i
您是否也可以发布您的Arduino代码或至少是将数据写入串行的部分,以便更容易测试/调试?非常感谢!这不是空指针异常,但在测试您的理论时,我找到了答案。正如我所说,没有空指针,所以我知道它必须是Arduino代码(C变体)。我有一些Arduino代码可以工作,所以我分析了它,并意识到有一行基本代码丢失了:延迟(25);这就解决了问题。非常感谢你让我走上解决问题的道路。
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();
}
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);
}
}