Python的串行通信 [英] Python serial communication

查看:181
本文介绍了Python的串行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个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的code是:

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的code是这样的:

And the Python code looks like this:

 import serial

 s = serial.Serial(port='/dev/tty.usbmodemfd131', baudrate=9600)

 s.write(str(25))

我几乎可以肯定,这个问题不从输出方式所产生的( Serial.print ),看到我的时候声明另一个整型,其格式罚款输出,所以我假设问题出在 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是present不管输入的。比如我可以写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循环。这意味着它们被加入,因为没有什么是在该当前时刻被发送。但是,即使固定,通过添加另一个如果(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.

此外,将使用味酸这里是合适的?
我在做什么错了?

Also, would using Pickle be appropriate here? What am I doing wrong?

推荐答案

您应该等待一个位串行数据的到来。

You should wait a bit for the serial data to arrive.

Arduino的code应该是:

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的串行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆