Skip to content Skip to sidebar Skip to footer

Pyserial Differences With Python 2.7 And 3.4

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 '

Solution 1:

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.

Post a Comment for "Pyserial Differences With Python 2.7 And 3.4"