pySerial 与 python 2.7 和 3.4 的区别 [英] pySerial differences with python 2.7 and 3.4

查看:29
本文介绍了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);
        }
    }

这感觉像是一种黑客行为,但有效,因为 '0' 在 ASCII 表上从 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.

我需要在这个项目中使用 python 2.7,因为我想稍后添加一些额外的硬件/API,所以这个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 字符串不是.

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

我在我的问题中提到的L"代表文字",这意味着字符串以其文字字符串形式发送,而不是 0-255 值的 ASCII 字节.因此,使用 python 2 在 Arduino 上解释串行数据要简单得多.对于此应用程序,您可以使用 serial.parseInt() 将字符串转换为 int,而不是使用 serial.Read() 读取单个字节.默认情况下,此函数有 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天全站免登陆