Hello everyone

I'm using python 3.1 and am trying to use the pyserial library.
However, when I try something like ser.write('hello'), I get a type error.

>>> import serial
>>> ser = serial.Serial('COM13')
>>> ser.write('a')
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    ser.write('a')
  File "C:\Program Files\Python31\lib\site-packages\serial\serialwin32.py", line 249, in write
    data = bytes(data)
TypeError: string argument without an encoding

how do I fix this? And is it normal to get this error? Most tutorials use the same format for the commands and don't get any error.

Recommended Answers

All 2 Replies

Your tutorials may be written for python 2. You could try

ser.write(bytes('a', encoding='ascii'))
# or
ser.write(b'a')

Thanks..
By the way, I also found that the .encode() method of strings also works, should anyone else find this thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.