0

Output:

b'00000-000000\r\n'
b'450302198011281530\r\n'
b''
b''
b'+\x00--To--\r\x12-\x00000800\r\x12-\x00000800\r\x12'
b'-\x00000800\r\x12-\x00000800\r\x12'
b'-\x00000800\r\x12-\x00000800\r\x12'

Required output:

-800
-800
-800

Python code:

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

Output with utf-8 decoding:

00000-000000
450302198011281530


-000800
-000800
-000800

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.

3
  • you know what would help? If you let us know what is connected to the serial port, and what it is sending – Jaromanda X Aug 8 '20 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 Aug 9 '20 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? – Jaromanda X Aug 9 '20 at 8:51
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:

https://docs.python.org/3/library/stdtypes.html#bytes.decode

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, privacy policy and cookie policy

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