Read hex or decimal from serial in Ignition

452 views Asked by At

I have a serial device that returns 23 hex values. I read the values using system.serial.readBytes('COM1', 23) in Ignition. This returns array('b', [-85, 112, 1, 18, -79, 0, 1, 116, -41, 2, -17, 10, 28, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0]). I know the values here are decimal from signed 2's complement, but how can I get the decimal value? For example -85 should be 171, or 0xAB in hex. I would prefer to directly read the hex values, but I don’t know how to do this. Any idea?

1

There are 1 answers

0
mnz On

You can convert them easily to a decimal value.

def signed2unsigned(val):
    if val >= 0:
        return val
    else:
        return 256+val