处理 - 使用 readStringUntil() 从 Arduino 丢失串行数据
Processing - missing serial data from Arduino, using readStringUntil()
我一直在尝试为来自我的 Arduino 的串行数据创建一个示波器。在 Arduino 串行绘图仪中,我可以获得合适频率的良好波形,但是当我尝试将数据发送到 Processing 时,它并没有从 Arduino 接收到所有数据。有解决办法吗?
Arduino
const int analogIn = A6;
int integratorOutput = 0;
void setup() {
// put your setup code here, to run once:
pinMode(3, OUTPUT);
pinMode(2, OUTPUT);
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
integratorOutput = analogRead(analogIn);
Serial.println(integratorOutput);
}
处理中
void serialEvent (Serial port) {
// get the ASCII string:
String inString = port.readStringUntil('\n');
if (inString != null) {
inString = trim(inString); // trim off whitespaces.
inByte = float(inString); // convert to a number.
inByte = map(inByte, 0, 1023, 100, height-100); //map to the screen height.
println(inByte);
newData = true;
}
}
谢谢!
因为readStringUntil
是一个非阻塞函数。假设 Arduino 正在打印一行: 12345\n The serial port at 115200 bits per seconds 相对较慢,因此有可能在某些时候接收缓冲区仅包含消息的一部分,例如:1234。当port.readStringUntil('\n')
被执行,它在缓冲区中没有遇到 \n
,因此它失败并且 returns 遇到 NULL
。您可以使用 bufferUntil
解决此问题,如 example
我一直在尝试为来自我的 Arduino 的串行数据创建一个示波器。在 Arduino 串行绘图仪中,我可以获得合适频率的良好波形,但是当我尝试将数据发送到 Processing 时,它并没有从 Arduino 接收到所有数据。有解决办法吗?
Arduino
const int analogIn = A6;
int integratorOutput = 0;
void setup() {
// put your setup code here, to run once:
pinMode(3, OUTPUT);
pinMode(2, OUTPUT);
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
integratorOutput = analogRead(analogIn);
Serial.println(integratorOutput);
}
处理中
void serialEvent (Serial port) {
// get the ASCII string:
String inString = port.readStringUntil('\n');
if (inString != null) {
inString = trim(inString); // trim off whitespaces.
inByte = float(inString); // convert to a number.
inByte = map(inByte, 0, 1023, 100, height-100); //map to the screen height.
println(inByte);
newData = true;
}
}
谢谢!
因为readStringUntil
是一个非阻塞函数。假设 Arduino 正在打印一行: 12345\n The serial port at 115200 bits per seconds 相对较慢,因此有可能在某些时候接收缓冲区仅包含消息的一部分,例如:1234。当port.readStringUntil('\n')
被执行,它在缓冲区中没有遇到 \n
,因此它失败并且 returns 遇到 NULL
。您可以使用 bufferUntil
解决此问题,如 example