读取串行并将 HEX 转换为 DEC
Read Serial and Convert HEX to DEC
阅读连载
使用 PySerial 创建了以下程序:
import serial
class comunicacao():
def __init__(self, porta, baud):
s = serial.Serial(porta, baud)
data = s.read(18)
data = data
print("Data: ", (data))
comunicacao('COM7', 57600)
正在接收十进制数10000
进行测试,打印输出为:Data: b'\x020000000000002710\x03'
因为十六进制的 2710 是十进制的 10000。
转化
所以尝试用以下方式转换:
print("Data: ", int(data, 16))
给出错误:
print("Data: ", int(data, 16))
ValueError: invalid literal for int() with base 16: b'\x020000000000002710\x03'
- 使用
data = s.read(18).decode()
打印输出为 Data: 0000000000002710
并尝试使用 int()
进行转换会给出错误:
print("Data: ", int(data, 16))
ValueError: invalid literal for int() with base 16: '\x020000000000002710\x03'
data = data.lstrip("0")
和 data = s.read(18).decode()
没有去除前导零。
- 和
data = data.lstrip("0")
与 data = s.read(18)
给出错误:
print("Data: ", (data.lstrip("0")))
TypeError: a bytes-like object is
required, not 'str'
问题
如何将此数据类型(我认为它是 ASCII,但它是 HEX 数字)转换为 DEC?
这个怎么样:
data = '\x020000000000002710\x03'
# If we say that "\x02" opens an hexadecimal representation of an integer,
# and that "\x03" ends it, then for any number of ints in hex form in data, do:
converted = [int(x, 16) for x in data.replace("\x02", "").split("\x03") if x]
print(converted)
print(converted[0])
您将获得从端口读取的所有号码的列表。
这段代码的作用是删除 \x02 字符以免与 int() 混淆,然后按 \x03 拆分数据。
然后它使用 int(x, 16) 转换每个元素。
如果您希望混合一些其他数据,使用添加 try 语句的经典方法会更健壮:
converted = []
for x in data.replace("\x02", "").split("\x03"):
try:
converted.append(int(x, 16))
except: pass
如果"\x03"不是整型分隔符,可以将\x02作为一个,结合切片提取需要的位数。
如果“\x03”仍然是 hexnum 终止符,但数字不相互跟随,则使用如下内容:
data = '\x020000000000002710\x03blahblah\x0200ff\x03mmmm'
converted = []
for x in data.split("\x02"):
end = x.find("\x03")
if end==-1: continue
x = x[:end]
try:
converted.append(int(x, 16))
except: pass
现在打印转换后的列表:
[10000, 255]
如您所见,即使十六进制数未用零均匀填充,这也会起作用。
您可以使用类似的代码公式获得漂亮可靠的代码。
阅读连载
使用 PySerial 创建了以下程序:
import serial
class comunicacao():
def __init__(self, porta, baud):
s = serial.Serial(porta, baud)
data = s.read(18)
data = data
print("Data: ", (data))
comunicacao('COM7', 57600)
正在接收十进制数10000
进行测试,打印输出为:Data: b'\x020000000000002710\x03'
因为十六进制的 2710 是十进制的 10000。
转化
所以尝试用以下方式转换:
print("Data: ", int(data, 16))
给出错误:
print("Data: ", int(data, 16))
ValueError: invalid literal for int() with base 16: b'\x020000000000002710\x03'
- 使用
data = s.read(18).decode()
打印输出为Data: 0000000000002710
并尝试使用int()
进行转换会给出错误:
print("Data: ", int(data, 16))
ValueError: invalid literal for int() with base 16: '\x020000000000002710\x03'
data = data.lstrip("0")
和data = s.read(18).decode()
没有去除前导零。- 和
data = data.lstrip("0")
与data = s.read(18)
给出错误:
print("Data: ", (data.lstrip("0")))
TypeError: a bytes-like object is required, not 'str'
问题
如何将此数据类型(我认为它是 ASCII,但它是 HEX 数字)转换为 DEC?
这个怎么样:
data = '\x020000000000002710\x03'
# If we say that "\x02" opens an hexadecimal representation of an integer,
# and that "\x03" ends it, then for any number of ints in hex form in data, do:
converted = [int(x, 16) for x in data.replace("\x02", "").split("\x03") if x]
print(converted)
print(converted[0])
您将获得从端口读取的所有号码的列表。 这段代码的作用是删除 \x02 字符以免与 int() 混淆,然后按 \x03 拆分数据。 然后它使用 int(x, 16) 转换每个元素。 如果您希望混合一些其他数据,使用添加 try 语句的经典方法会更健壮:
converted = []
for x in data.replace("\x02", "").split("\x03"):
try:
converted.append(int(x, 16))
except: pass
如果"\x03"不是整型分隔符,可以将\x02作为一个,结合切片提取需要的位数。
如果“\x03”仍然是 hexnum 终止符,但数字不相互跟随,则使用如下内容:
data = '\x020000000000002710\x03blahblah\x0200ff\x03mmmm'
converted = []
for x in data.split("\x02"):
end = x.find("\x03")
if end==-1: continue
x = x[:end]
try:
converted.append(int(x, 16))
except: pass
现在打印转换后的列表:
[10000, 255]
如您所见,即使十六进制数未用零均匀填充,这也会起作用。 您可以使用类似的代码公式获得漂亮可靠的代码。