处理 - 使用 readStringUntil() 缺少来自 Arduino 的串行数据

问题描述 投票:0回答:1

我一直在尝试为 Arduino 的串行数据创建一个示波器。在 Arduino 串行绘图仪中,我可以获得合适频率的良好波形,但是当我尝试将数据发送到处理时,它没有从 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;
  }
}
arduino processing
1个回答
4
投票

这是因为

readStringUntil
是一个非阻塞函数。假设 Arduino 正在打印一行:12345 每秒 115,200 位的串行端口相对较慢,因此有可能在某些时候接收缓冲区仅包含消息的一部分,例如:1234。

当执行

port.readStringUntil('\n')
时,它在缓冲区中没有遇到
\n
,因此失败并返回
NULL

您可以使用

bufferUntil
来解决此问题,如 此示例

© www.soinside.com 2019 - 2024. All rights reserved.