gpt4 book ai didi

arduino - 处理 - 使用 readStringUntil() 从 Arduino 丢失串行数据

转载 作者:行者123 更新时间:2023-12-04 01:09:50 24 4
gpt4 key购买 nike

我一直在尝试为来 self 的 Arduino 的串行数据创建一个示波器。在 Arduino 串行绘图仪中,我可以获得合适频率的良好波形,但是当我尝试将数据发送到 Processing 时,它并没有从 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 串行端口以每秒 115200 位的速度相对较慢,因此可能在某些时候接收缓冲区仅包含消息的一部分,例如:1234。当 port.readStringUntil('\n') 被执行时,它没有在缓冲区中遇到 \n,因此失败并返回 NULL。您可以使用 bufferUntil 来解决此问题,如 example 所示。

关于arduino - 处理 - 使用 readStringUntil() 从 Arduino 丢失串行数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65216620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com