Python Serial Vs Pyserial

On

python-pyserial 3.4-2

Package Actions

  • Source Files / View Changes
  • Bug Reports / Add New Bug
Architecture:any
Repository:Community
Split Packages:python2-pyserial
Description:Multiplatform Serial Port Module for Python
Upstream URL:https://github.com/pyserial/pyserial
License(s):custom:PYTHON
Maintainers:Morten Linderud
Filipe Laíns
Package Size:110.5 KB
Installed Size:524.0 KB
Last Packager:Felix Yan
Build Date:2018-06-30 16:52 UTC
Signed By:Felix Yan
Signature Date:2018-06-30 16:53 UTC
Last Updated:2018-08-06 08:26 UTC

Dependencies (3)

  • python (make)
  • python2 (make)

Required By (5)

  • python-twisted (optional)
  • twisted (check)

Package Contents

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:

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:

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?

CB34RCB34R

1 Answer

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

Ultraman fighting evolution rebirth. 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.

CB34RCB34R

Not the answer you're looking for? Browse other questions tagged python-2.7arduinopython-3.4 or ask your own question.