简体   繁体   English

从 Ignition 中的串行读取十六进制或十进制

[英]Read hex or decimal from serial in Ignition

I have a serial device that returns 23 hex values.我有一个返回 23 个十六进制值的串行设备。 I read the values using system.serial.readBytes('COM1', 23) in Ignition.我在 Ignition 中使用system.serial.readBytes('COM1', 23)读取值。 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]) .这将返回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?我知道这里的值是有符号 2 的补码的十进制值,但我怎样才能得到十进制值? For example -85 should be 171, or 0xAB in hex.例如 -85 应该是 171,或者十六进制的 0xAB。 I would prefer to directly read the hex values, but I don't know how to do this.我更愿意直接读取十六进制值,但我不知道该怎么做。 Any idea?任何的想法?

You can convert them easily to a decimal value.您可以轻松地将它们转换为十进制值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM