Sending a newline (\n) terminated string to an Arduino with serial.write().
Converting a python 2.x script to python 3.x the following produced a "unicode not supported" error:
cmd = "a string"
cmd = cmd + "\n"
serial.write(cmd)
changing to
serial.write(cmd.encode('ascii'))
eliminated the error
However, I am unclear as to exactly what is transmitted by
serial.write(cmd.encode('ascii'))
A console session produced the following:
>>>cmd = "a string"
>>>print(cmd)
a string
>>>cmd = cmd + "\n"
>>>print(cmd)
a string
#note following blank line
>>>print(cmd.encode('ascii'))
b'a string\n'
If I were to execute:
>>>serial.write(cmd.encode('ascii'))
exactly what would be sent? would it include the initial b, as in the print statement? would it include the single quotes? would it send the ascii byte code for the newline character (0x0A), or ascii byte codes for backslash (0x5C) and "n" (0x6E)?