如何解决python + arduino与解析字符串之间的串行通信
我正在尝试将一组 [x,y] coordinates
从 python 发送到 arduino,以便控制机器人传动系统上的伺服电机。我正在采用以 "x,y"
格式发送字符串的方法。在 arduino 方面,我试图解析这个字符串以获取 x 和 y 子字符串。但是 indexOf
函数没有返回正确的索引。这是我的代码:
arduino = serial.Serial(port='COM3',baudrate=9600,timeout=.1)
def write_read(x):
arduino.write(bytes(x,'utf-8'))
time.sleep(0.05)
data = arduino.readline()
return data
for coordinate in coordinates:
c = str(coordinate[0]) + "," + str(coordinate[1])
print(write_read(c))
#include <Servo.h>
void setup() {
Serial.begin(9600);
Serial.setTimeout(1);
}
void loop() {
while (!Serial.available());
String coordinates = "" + Serial.readString();
// Serial.print(coordinates); //this prints normally as the string value
int i = coordinates.indexOf(',');
Serial.print(i);
}
当我发送类似 "-9.45,-16.3"
的内容时。输出为 b'-1-1-1-1-10-1-1-1-1-1-1'
。我不确定这里发生了什么或如何获取实际索引(在本例中为 5)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。