python2.7和3.4区别_pySerial与python 2.7和3.4的区别

1586010002-jmsa.png

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);

}

}

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())

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.

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.

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?

解决方案

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

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

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).

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值