I am using Python 3.3 and SerialIO 2.7. When I try to write a string to a serial port using the following command:
ser.write('MR0000;')
I get the following errors:
Traceback (most recent call last):
File "/Users/Clark/PycharmProjects/TS2000/Main.py", line 14, in GetData
channels = SerialIO.GetChannelData()
File "/Users/Clark/PycharmProjects/TS2000/SerialIO.py", line 191, in GetChannelData
TS2KRawMemoryConfig = TS2K.ReadMemoryConfig(0, 29)
File "/Users/Clark/PycharmProjects/TS2000/SerialIO.py", line 49, in ReadMemoryConfig
ser.write(readCommand)
File "/usr/local/Cellar/python3/3.3.3/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/pyserial-2.7-py3.3.egg/serial/serialposix.py", line 491, in write
File "/usr/local/Cellar/python3/3.3.3/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/pyserial-2.7-py3.3.egg/serial/serialutil.py", line 76, in to_bytes
I am new at Python but I think the problem is that b.append(item) does not accept a string. It only accepts an integer.
with Python 3, pySerial expects "bytes".
either use the 'b' modifier or the encode method:
ser.write(b'MR0000;')
ser.write('MR0000;'.encode('latin1'))
depending on the device and purpose, one or the other might me better suited.
Thanks for looking at this so quickly. The documentation still shows that write and writelines takes string data.
I'll close this but I'm not sure how to do it….
On Feb 1, 2014, at 6:38 PM, Chris Liechti cliechti@users.sf.net wrote:
Related
Bugs:
#158Strictly speaking, I think this is still a bug (I installed pyserial from pipy using pip a couple of days ago; my installed version 2.7).
In particular, the documentation says:
The following shows how the bug happens:
One possible fix is as follows:
Another possible fix is to change the documentation.
It is not entirely clear to me what is really the intended behavior of to_bytes: There seems to be a difference in the implementation of Serial for IronPython and Jython than the rest, e.g., PosixSerial, where to_bytes() is called for the argument of write and it is beyond my knowledge to understand why this is so. In any case, there is an inconsistency, which IMHO should be removed.
Last edit: Csaba Szepesvari 2015-01-11