python - Pyserial string to byte encoding to write to microcontroller -
i'm trying communicate between pc , microcontroller (mc). microcontroller read using getchar() until 4 characters have been read or bumps characters '\0', '\', or '\r'.
the communication works fine hyper-terminal. however, python script doesn't seem sending '\0', '\', or '\r' when encoding input string , concatenating 1 special characters it.
command = input("enter command: ") port.write(bytes(command + '\n', 'utf-8')) so if entered command x should send 'x'and'\n' , mc should stop waiting more characters read because of new line. however, if enter x, mc wait 4 more characters read.
how convert string special characters bytes? thanks.
the mc code is:
buffer[ii] = getchar(); while(buffer[ii] != '\0' && buffer[ii] != '\n' && buffer[ii] != '\r' && ii < 4 - 1) { buffer[++ii] = getchar(); }
you can convert string array of integers 0 <= n <= 256 either:
map(ord,command+'\n') or bytearray(command+'\n',"utf-8")
if had write each byte 1 one port:
>>> b in bytearray("message\n","utf-8"): ... port.write(b) should trick.
Comments
Post a Comment