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?
Asked
Active
Viewed 408 times
3

konichiwa
- 532
- 1
- 5
- 23
-
2Theres no method on the serial module for reading hex. Even readStringAsBytes will still return a byte string, which you would have to cast over to hex. – tazer84 Oct 03 '18 at 23:43
-
1Please convert your comment into an answer, so I can mark this as answered. – konichiwa Jul 01 '19 at 06:05
1 Answers
0
You can convert them easily to a decimal value.
def signed2unsigned(val):
if val >= 0:
return val
else:
return 256+val

mnz
- 156
- 6