I have to read UART device through the USB port and store the incoming bytes in a .txt
file.
I use the following command:
while 1:
x=ser.read()
f.write(str(x)) #f is the file object
However this converts some bytes
to their corresponding ascii
characters and stores some as it is
Example:
b'\x55'
is stored asb'U'
.But,
b'\xaa'
is stored as the string itself (i.e.b'\xaa'
).
If I use chr(int.from_bytes())
it gives the following error:
UnicodeEncodeError: 'cp932' codec can't encode character '\xaa' in position 0: illegal multibyte sequence
Is there a method by which I can store all the incoming bytes as the bytes without converting some to ascii
characters (eg: b'\x55'
stored as string b'\x55'
not b'U'
), as this causes problems when I process the data further.
I am using python-3.7 on 64 bit windows 10