[Tutor] Converting a string to a byte array

Phil phil_lor at bigpond.com
Sun Sep 24 06:24:40 EDT 2017


Thank you for reading this.

The following code sends "Fred" to my serial connected device without 
any problems.

import serial

ser = serial.Serial('/dev/ttyACM0',9600)
ser.write(b'Fred\n')


I'm trying to do the same under pyqt but I'm having trouble converting a 
string to a byte array. This works correctly from the pyqt IDE but not 
from the console. python3 mycode.py generates "typeError: string 
argument without an encoding"

It's very likely that my method of converting a string to a byte array 
is incorrect. This is my attempt:

mytext = "Fred"
mybytes = bytes(mytext)
byte = byte + '\n'
ser.write(mybytes)

I don't understand why this works from the pyqt IDE but not when run 
from the console. I suppose the IDE is adding the correct encoding. I 
suspect utf8 is involved somewhere.

-- 
Regards,
Phil


More information about the Tutor mailing list