Required output:


Python code:

ser = serial.Serial('/dev/ttyUSB0', 9600, timeout = 0.1)
while j < 1000:
    #read out depth from probe
#   serial_output = serial_output.decode('utf-8').strip()
    j = j + 1

Output with utf-8 decoding:



and sometimes

Traceback (most recent call last):
  File "dial.py", line 51, in <module>
    serial_output = serial_output.decode('utf-8').strip()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfe in position 0: invalid start byte

Not sure what is wrong here but perhaps baudrate. With utf-8 decryption in python3 I still get the character and random numbers at the beginning and unable to google that character for more information. Please can someone help out here.

  • you know what would help? If you let us know what is connected to the serial port, and what it is sending Commented Aug 8, 2020 at 3:00
  • Sending absolutely nothing. The code above is the entire code used. It is a digital dial indicator that I am using. Also the output with utf-8 did not show correctly. There is an invalid character with a empty square before each one which I cannot remove with strip.
    – user123928
    Commented Aug 9, 2020 at 7:27
  • you "expect -0800" ... and you get amongst other things, -\x00000800 ... there's a hint of -800 in that ... are you sure the vaguely described "digital dial indicator" isn't sending anything at all? you seem to be receiving a butt ton of data for it to not be sending anything - what is it supposed to send? do you have some sort of documentation that describes what will be sent? Commented Aug 9, 2020 at 8:51

1 Answer 1


It is tricky to help because you don't know the format of the serial data being sent. Other people will not know the format as you are not giving enough detail about the device sending the data. Given this, you will need to explore and document the data better before developing the best strategy for handling the data.

It appears that in addition to the data you are looking for, there is also other data being sent, some of which is not strict UTF-8. It also appears that your value of -800 is sent as a fixed width field padded with leading zeros so comes across as -000800.

Initially as you explore the data that is being sent across the serial connection, one strategy might be to ignore any UTF-8 errors. The bytes.decode method has an option for this:


It might be that leaving it as bytes is OK for the data being sent.

Another strategy might be to read the data a byte at a time rather than a whole line at a time with readline.

The data you want is possibly always preceded with an \x12 and followed by a\r. The hex 12 may be a control character that has significance in these sequences.

Once you have a fuller understanding of the data being sent then if you share that information here people may be able to help you handle it in an efficient manner

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.