1

I want convert input from QLineEdit and send to serial as Hex bytes.

Example:

QlineEdit input is 03040506

Write to serial as 0x03 0x04 0x05 0x06

Thanks,

Rahul Agarwal
  • 4,034
  • 7
  • 27
  • 51
aditya
  • 69
  • 8

1 Answers1

1

You can easily do this using bytes.fromhex

data = "03040506"
out = bytes.fromhex(data)
print(out)

output

b'\x03\x04\x05\x06'

To send those bytes to the serial port, just do something like ser.write(out), where ser is an open serial port.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • I've never used `pyserial`, but according to [this answer](https://stackoverflow.com/a/39425136/4014959), you should be able to send `out`, you don't need to pass it through `serial.to_bytes`. – PM 2Ring Sep 18 '18 at 07:51
  • Yeah got it working as expected. No need to do serial.to_bytes – aditya Sep 18 '18 at 07:53