【发布时间】:2019-11-25 01:07:14
【问题描述】:
TL,DR : bufferUntil() 和 readStringUntil() 在设置为 '\n' 时工作正常,但对其他字符会产生问题。
向pc发送数据的代码如下;
Serial.print(rollF);
Serial.print("/");
Serial.println(pitchF);
而加工出来的相关部分是;
myPort = new Serial(this, "COM3", 9600); // starts the serial communication
myPort.bufferUntil('\n');
void serialEvent (Serial myPort) {
// reads the data from the Serial Port up to the character '\n' and puts it into the String variable "data".
data = myPort.readStringUntil('\n');
// if you got any bytes other than the linefeed:
if (data != null) {
data = trim(data);
// split the string at "/"
String items[] = split(data, '/');
if (items.length > 1) {
//--- Roll,Pitch in degrees
roll = float(items[0]);
pitch = float(items[1]);
}
}
}
来自我传入数据的图片(来自 arduino 串行监视器):
0.62/-0.52
0.63/-0.52
0.63/-0.52
0.64/-0.53
0.66/-0.53
0.67/-0.53
0.66/-0.54
到这里为止,一切都很好。没什么特别的。当我将 bufferUntil() 和 readStringUntil() 函数的参数更改为“\n”以外的任何参数时,就会出现问题。当然,当我这样做时,我也会更改 arduino 代码中的相应部分。例如,当用 'k' 替换 '\n' 时,从 arduino 串行监视器看到的传入数据看起来像,
45.63/22.3k21.51/77.32k12.63/88.90k
然后就这样继续下去。但是处理无法获得每个缓冲区中的第二个值。当我通过也在处理控制台上打印值来检查它时,我得到了第一个(滚动)的值,但是第二个值(音高)显示为 NaN。那么问题是什么?是什么原因只能在'\n'时起作用。
【问题讨论】:
-
这不是 java 而是 Arduino C++。 arduino.cc/reference/en/language/functions/communication/serial/…
标签: java arduino serial-port processing