pySerial与python 2.7和3.4的差异 [英] pySerial differences with python 2.7 and 3.4

查看:318
本文介绍了pySerial与python 2.7和3.4的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,该项目需要通过串行端口将Windows 10中的python中的一些数字发送到arduino uno.作为一个简单的测试,我只想通过从命令提示符处发送"2"来打开LED,并通过从命令提示符处发送"4"来关闭LED,尽管我最终希望能够将0-99的任何数字用于不同的目的.这是我的arduino代码:

I'm working on a project that needs to send some numbers from python in windows 10 to an arduino uno over a serial port. As a simple test I just want to turn an LED on by sending '2', and off by sending '4' from a command prompt, although I would like to be able to ultimately use any number 0-99 for different purposes. Here's my arduino code:

    const int ledPin = 13; 
    byte b1, b2;
    int val;

    void setup() {
        Serial.begin(9600);
        pinMode(ledPin, OUTPUT);
    }

    void loop() {
        if (Serial.available() > 1) {
        b1 = Serial.read();
        b2 = Serial.read();
        val = ((b1-48)*10) + (b2-48);

        if (val == 2) {
            digitalWrite(ledPin, HIGH);
        }

        if (val == 4) {
            digitalWrite(ledPin, LOW);
        }
    }

这感觉像是一种破解,但是可以正常工作,因为ASCII表上的"0"从48开始(所以2 = 50,依此类推),这是当我使用以下python代码发送时arduino实际收到的内容一些数字:

This feels like kind of a hack but works because '0' starts at 48 on the ASCII table (so 2 = 50, etc.), which is what the arduino is actually receiving when I use the following python code to send some numbers:

    import serial
    ser = serial.Serial('COM4',9600)
    n = 2
    s = str(n)
    t = s.rjust(2,'0')
    ser.write(t.encode())

因此,我发现在桌面上使用python 3.4时,这的确的确打开了我的LED,而使用n = 4则将其关闭.无论我为n选择多少,它总是返回"2",如"2字节已发送"一样,这就是我想要的.我遇到的问题是,在python 3.4中这一切都很好,但当我尝试使用python 2.7在笔记本电脑上加载此项目时,我得到的是"2L"回报,而不仅仅是"2",并且LED不再打开.

So I've found that this does indeed turn on my LED when using python 3.4 on my desktop, and using n = 4 turns it off. This always returns '2' regardless of the number I choose for n, as in "2 bytes have been sent", which is what I want. The problem I'm having is that this is all fine and dandy in python 3.4, but when I tried loading this project on my laptop using python 2.7 I get a '2L' return instead of just '2', and the LED no longer turns on.

我的笔记本电脑和台式机都使用pySerial 3.3,并且我已经确认,当我在台式机上使用python 2.7时,也会发生相同的事情(不起作用),而当我在笔记本电脑上使用python 3.4时,它也可以正常工作

Both my laptop and desktop are using pySerial 3.3, and I've confirmed that the same thing happens (doesn't work) when I use python 2.7 on my desktop, and it does work when I use python 3.4 on my laptop.

由于需要稍后添加一些额外的硬件/API,因此我需要在该项目中使用python 2.7,因此在串行返回上附加"L"的意义是什么,为什么它不适用于python 2.7 ?

I need to use python 2.7 for this project because of some additional hardware/API I want to add later, so what is the significance of this 'L' being appended on the serial return and why does this not work with python 2.7?

推荐答案

所以对于以后遇到此问题的任何人,我终于在Arduino网站的所有地方找到了答案: http://playground.arduino.cc/interface/python

So for anyone coming across this in the future, I've finally found an answer on the Arduino website of all places: http://playground.arduino.cc/interfacing/python

简而言之,python 3字符串不是Unicode,而python 2字符串不是Unicode.

In short, python 3 strings are Unicode and python 2 strings are not.

我在我的问题中提到的"L",我相信它代表文字",表示一个字符串以其原义字符串形式发送,而不是0-255值的ASCII字节.因此,使用python 2在Arduino上解释串行数据要简单得多.对于此应用程序,您无需使用serial.Read()即可读取单个字节,您只需使用serial.parseInt()将您的字符串转换为int即可.默认情况下,此函数有1秒的超时时间,尽管您可以在设置块中添加Serial.setTimeout(n),其中n是您要等待的毫秒数(这对我的情况很重要-我发现5有效).

The "L" I mentioned in my question I believe stands for "Literal," meaning a string has been sent in its literal string form as opposed to a 0-255 valued ASCII byte. As such, interpreting serial data on the Arduino is much simpler with python 2. Instead of using serial.Read() to read individual bytes, for this application you can just use serial.parseInt() to convert your string to an int. This function has a 1 second timeout by default, although you can add Serial.setTimeout(n) in your setup block where n is the number of milliseconds you would like to wait (which is important for my particular case - I've found 5 to be effective).

对于python部分,ser.write('2')在python 2中执行的功能与我在python 3中所显示的功能相同-无需使用0或编码进行对齐.

As for the python portion, ser.write('2') performs the same function with python 2 as what I had shown in my question with python 3 - no need for justification with 0's or encoding.

这篇关于pySerial与python 2.7和3.4的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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