无法将字符串值转换为实际的十六进制形式

问题描述

我正在用 python 开发一个硬件程序,我必须在其中创建一个十六进制值的框架,并在 raspberry pi 上串行写入它,以便它可以与终端设备进行通信。

当我手动将计算值输入到框架中时,我得到了想要的结果,但现在我必须自动化这些东西。为此,我正在做以下流程。

1.创建框架。 -- current_frame = [0x0a,0x01,0x10,0x00,0x0a]

2.接受用户的输入。 -- current = float(input('Enter Current Value: '))

3.将浮点输入转换为十六进制值

def float_to_hex(f):
    hex_val = binascii.hexlify(struct.pack('>f',f))
    return hex_val

4.因为我想将获得的值分成两组,所以我使用了

hex_split = []
for i in range(0,len(hex_value),2):
    hex_split.append(hex_value[i:i+2])

5.拆分后,我必须将结果插入主框架,以便我可以计算整个字符串的校验和

for i in range(len(hex_split)):
    current_frame.insert(i + 5,hex_split[i])
print ('New Frame',current_frame)

6.我已经使用

计算了校验和
def HexToByte(hexStr):
    bytes = []
    hexStr = ''.join(hexStr.split(" "))
    for i in range(0,len(hexStr),2):
        bytes.append(int(hexStr[i:i+2],16))
    return bytes

7.最后我必须将数据写入串行

但是我没有得到我预期的结果。 请帮忙

尝试 1

import struct
import binascii

# Set Current Frame 
current_frame = [0x0a,0x0a]
print ('Original Frame: ',current_frame)
print (type(current_frame[0]))


# Take input from User
current = float(input('Enter Current Value: '))


# Function to Calculate Hex value from Float
def float_to_hex(f):
    hex_val = binascii.hexlify(struct.pack('>f',f))
    return hex_val

    
# Use function to get hex value
hex_value = float_to_hex(current).decode()
print ('Hex value of Current is: ',hex_value)


# Split the values into group of 2
hex_split = []
for i in range(0,2):
    hex_split.append(hex_value[i:i+2])
    
    
print ('Hex value after Split :',hex_split)
print (type(hex_split[0]))

for i in range (0,len(hex_split)):
    hex_split[i] = int(hex_split[i],16)
    hex_split[i] = hex(hex_split[i])
    
print (hex_split)

for i in range(len(hex_split)):
    current_frame.insert(i + 5,current_frame)

输出

>>> %Run range_test.py
Enter Value of Current: 0.3
3e99999a
['3e','99','9a']
['0a','01','10','00','0a','3e','9a']
[10,1,16,10,62,153,154]
0x22f
2e
0a0110000a3e99999a2e
 0x0a  0x01  0x10  0x00  0x0a  0x3e  0x99  0x99  0x9a  0x2e
<class 'str'>

尝试 2

import struct
import binascii

set_current = [ '0a','0a' ]
pos = 5

import serial
import time

ser = serial.Serial(
                    port='/dev/ttyUSB0',baudrate = 125000,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS,timeout = None
                    )

# Calculate Hex Value for Current
def float_to_hex(f):
    hex_val = binascii.hexlify(struct.pack('>f',f))
    return hex_val

# Function Calculate Checksum
def HexToByte(hexStr):
    bytes = []
    hexStr = ''.join(hexStr.split(" "))
    for i in range(0,16))
    return bytes

current_value = float(input("Enter Value of Current: "))
data = float_to_hex(current_value).decode()
print (data)

# Create List of Hex element
new_data = []
for i in range(0,len(data),2):
    new_data.append((data[i:i+2]))
print (new_data)

# Insert hex list into main list
for i in range (len(new_data)):
    set_current.insert(i + 5,new_data[i])
print (set_current)
# 
# Join List
set_current = ''.join(map(str,set_current))

# Calculate Checksum
checksum = HexToByte(set_current)
print (checksum)
y = 0
for i in checksum:
    y = y + i

print (hex(y))
new_sum = hex(y-1)[3:]
print (new_sum)
set_current = set_current + (new_sum)
print (set_current)


x = []
for i in range(0,len(set_current),2):
    x.append((set_current[i:i+2]))
    
new_x = ' '.join(map(lambda a: ' 0x'+ str(a),x))
print ((new_x))
print (type(new_x))

ser.write(str.encode(new_x))
data_read  = ser.read(3)
print ("set_max_current",data_read)

输出

>>> %Run demo1.py
Original Frame:  [10,10]
<class 'int'>
Enter Current Value: 0.3
Hex value of Current is:  3e99999a
Hex value after Split : ['3e','9a']
<class 'str'>
['0x3e','0x99','0x9a']
New Frame [10,'0x3e','0x9a']

想要的答案应该是这样的

set_max_current = ([0x0a,0x0a,0x3f,0x80,0xAD])
print (set_max_current)
ser.write(serial.to_bytes(set_max_current))
data_read = ser.read(3)
print ("set_max_current",data_read)

输出

>>> %Run c.py
[10,63,128,173]
set_max_current b'\x03\x00\x02'

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)