问题描述
我正在处理一个 Arduino 项目,由于内存限制,我正在将它与 Python 脚本连接起来.在 Python 方面,我有一个二维矩阵,其中包含坐标的相应 x、y 值,在此列表中是 26000 个坐标对.因此,为了为大家阐明数据结构,pathlist[0][0]
将返回我的列表第一个坐标的 X 值.在 Python 中对这个列表执行不同的操作等没有问题.然而,我遇到麻烦的地方是以有用的方式通过串行将这些值发送到 Arduino.
I'm working on an Arduino project, and I am interfacing it with a Python script due to memory limitations. On the Python side I have a 2 dimensional matrix containing respective x, y values for coordinates, and in this list is 26000 coordinate pairs. So, in interest of clarifying the data structure for all of you, pathlist[0][0]
, would return the X value of the first coordinate of my list. Performing different operations, etc. on this list in Python is posing no problems. Where I am running into trouble however is sending these values to Arduino over serial, in a way that is useful.
由于串行通信的性质(至少我认为是这样),我必须将每个整数作为一个字符串发送,并且一次只能发送一个数字.因此,像 345 这样的数字将作为 3 个单独的字符发送,这些字符当然是 3、4 和 5.
Due to the nature of serial communication (at least I think this is the case) I must send each each integer as a string, and only one digit at a time. So, a number like 345 would be sent over as 3 individual characters, those being of course, 3, 4, then 5.
我正在努力寻找一种在 Arduino 上重建这些整数的方法.
What I am struggling with is finding a way to rebuild those integers on the Arduino.
每当我发送一个值时,它都会像这样接收数据并输出它:
Whenever I send a value over, it's receiving the data and outputting it like so:
//Python is sending over the number '25'
2ÿÿ52
//Python is sending the number 431.
4ÿÿ321ÿÿÿ2
Arduino 代码是:
The Arduino code is:
String str;
int ds = 4;
void setup() {
Serial.begin(9600);
}
void loop(){
if (Serial.available()>0) {
for (int i=0; i<4; i=i+1) {
char d= Serial.read();
str.concat(d);
}
char t[str.length()+1];
str.toCharArray(t, (sizeof(t)));
int intdata = atoi(t);
Serial.print(intdata);
}
}
Python 代码如下所示:
And the Python code looks like this:
import serial
s = serial.Serial(port='/dev/tty.usbmodemfd131', baudrate=9600)
s.write(str(25))
我几乎可以肯定问题不是源于输出方法 (Serial.print
),因为当我声明另一个 int 时,它的输出格式很好,所以我假设问题在于如何构造 intdata
变量.
I'm almost certain that the problem isn't stemming from the output method (Serial.print
), seeing as when I declare another int, it formats fine on output, so I am assuming the problem lies in how the intdata
variable is constructed.
可能有助于诊断此问题的一件事是,如果我将 Serial.print(intdata)
更改为 Serial.print(intdata+5)
我的结果是2ÿÿ57
,我期望 30 (25+5).无论输入如何,这个 7 都存在.例如,我可以将 271 写入串行,结果如下所示:
One thing of note that may help diagnose this problem is that if I change Serial.print(intdata)
to Serial.print(intdata+5)
my result is 2ÿÿ57
, where I would expect 30 (25+5). This 7 is present regardless of the input. For instance I could write 271 to the serial and my result would look as follows:
//For input 271.
2ÿÿ771ÿÿÿ7
在我看来,Arduino 正在将值分成两对,并将长度附加到末尾.我不明白为什么会发生这种情况.
It appears to me that Arduino is chunking the values into pairs of two and appending the length to the end. I can't understand why that would happen though.
在我看来,ÿ
也被添加到 for 循环中.这意味着添加它们是因为在当前时刻没有发送任何内容.但是即使通过添加另一个 if(Serial.available()>0)
条件来解决这个问题,结果仍然没有被当作整数处理.
It also seems to me that the ÿ
are being added in the for loop. Meaning that they are added because nothing is being sent at that current moment. But even fixing that by adding yet another if(Serial.available()>0)
conditional, the result is still not treated like an integer.
另外,在这里使用 Pickle 是否合适?我做错了什么?
Also, would using Pickle be appropriate here?What am I doing wrong?
推荐答案
您应该等待串行数据到达.
You should wait a bit for the serial data to arrive.
Arduino 代码应该是:
The Arduino code should be:
if (Serial.available()){
delay(100); // Wait for all data.
while (Serial.available()) {
char d = Serial.read();
str.concat(d);
}
}
此外,您必须在重新使用之前清除字符串.
Also you have to clear your string before re-using it.
我忘了提到 ÿ == -1 == 255 这意味着 Serial.read()
它说它无法读取任何东西.
I forgot to mention ÿ == -1 == 255 which means Serial.read()
it is saying it can't read anything.
这篇关于Python串口通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!