2

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.

1
  • Is this Python 2 or 3? Commented Apr 14, 2016 at 3:05

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

What I ended up getting working was this:

ba = bytes("10,20\n", encoding="ascii")
ser.write(ba)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.