Reputation: 621
I'm doing some arduino project that sends and receives strings.
It sends me a string, which is a comma separated list of numbers which i save to file. I then try to read the file and send the string back in the same way i received it.
This works fine if I do this through the arduino serial monitor, yet I can't seem to get pyserial to send/encode the string in the same way it was received.
I've tried using code like this:
for b in bytearray("10,20","UTF-8"):
ser.write(str(b).encode("latin_1"))
ser.flush()
but haven't had any luck getting it sent through correctly.
Upvotes: 2
Views: 3712
Reputation: 621
What I ended up getting working was this:
ba = bytes("10,20\n", encoding="ascii")
ser.write(ba)
Upvotes: 2
Reputation: 1185
Assuming you're using Python 3, you can simply use:
ser.write("10,20".encode())
For Python 2, I think strings can be passed directly to ser.write()
without encoding them.
Upvotes: 2