0

I have a serial device (loadcell) connected to pc which is constantly sending data in a fixed format. It has 2 stop bits (0x40 and 0x0b). I am trying to read until 1 stop bit so I can start reading the message from the start. The problem is the code never exits the while loop. I tried many ways to convert the bytes I am receiving from the serial port into hex but I can not manage to get out of the while loop. I NEED to use hex here because 0x0b value doesn't have any visual (doesn't represent any character). I can not give that input from keyboard!!

loadcell.reset_input_buffer()
some_hex_val = 0x00
def read_thrust():
    global some_hex_val
    while some_hex_val != 0x0b or some_hex_val != 0x40:
        some_hex_val = loadcell.read().hex()
    print('Exited while')

read_thrust()

How do I read each byte as hex?

Update:

The .hex() method doesn't return anything!!? I am reading from serial port in bytes but using the .hex() method on it returns None i guess (because its not printing anything?) Printing out the types just to be sure

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
bmw-m8
  • 1
  • 1
  • 1
    `or` should be `and` – Barmar Jun 28 '22 at 05:01
  • `.hex()` returns a *string* of the hexadecimal digits and isn't what you want here. It is unclear what `loadcell` is but you probably want `some_hex_val = loadcell.read(1)` to extract one byte from the stream and compare with `while some_hex_val not in b'\x0b\x40':`. Adding a `print(some_hex_val)` in the loop would help debug. – Mark Tolonen Jun 28 '22 at 21:43

0 Answers0