将十六进制2的补码转换为十进制

时间:2020-01-31 17:46:16

标签: python-3.x hex

我有一个通过RS-232连接的传感器,该传感器正试图与Python 3.7交互。我可以使用随附的代码从传感器获取离子电流数据,但是输出为十六进制(代码下方的输出示例)。我联系了制造传感器的供应商,他们告诉我输出是十六进制2的补码,并且在手册中他们指出:

“离子电流以通常的格式表示:4字节长,2的补码整数 单位为10-16 A,首先发送最低有效字节。”

如果任何人都可以提供有关如何将我的输出转换为可读格式的见解,我们将不胜感激。我几乎没有Python的背景,并且非常在学习阶段,因此,向任何好的学习资源的方向也都值得赞赏。

感谢您的时间。

Created on Tue Jan 28 11:12:41 2020

@author: cchunt6058321
"""

# Import the libraries
import serial
import time

# Define the rga serial object
rga = serial.Serial("com3", timeout=1, baudrate=28800, \
    bytesize=8, stopbits=1, parity="N",rtscts=1)

# Initialize the hardware
# ----------------------
# Clears all RGA RS-232 buffers, runs a fresh set of tests on the ECU's hardware, sends back the STATUS byte
rga.write(b"IN0\r")
time.sleep(5.0)
# Set electron energy to 70eV. Can be set from 25-105eV
rga.write(b"EE70\r")
time.sleep(5.0)
# Set ion energy to high value (12eV). Can be set from 8-12V
rga.write(b"IE1\r")
time.sleep(5.0)
# Set the focus plate voltage to -100V. Can be set from 0-150V
rga.write(b"VF120\r")
time.sleep(5.0)
# Switch on the filament and set the electron emission current to 1.0mA. Can be set from 0-3.5mA
rga.write(b"FL1.0\r")
time.sleep(5.0)
# Set the CEDM voltage to 1400, Can be set from 0-2400V
rga.write(b"hv1400\r")
time.sleep(5.0)
# Set minimum averaging and maximum scan rate
rga.write(b"nf1\r")
time.sleep(5.0)
# Reset the zero of the detector
rga.write(b"ca\r")
time.sleep(5.0)
# Set the steps per amu
rga.write(b"sa10\r")
time.sleep(5.0)

# Define the start time
start_time = serial.time.time()

# Set the upper and lower analog sweep limits
rga.write(b"mi0\r")
rga.write(b"mf20\r")

# Start performing sweeps
sweep = rga.write(b"sc1\r")

# Open a data file to write to.
# f = open("test.txt","a")

# Read the data.
while (True):
        if (rga.in_waiting>0):
                data_str_byte = rga.read(rga.in_waiting)
                print(data_str_byte)
                time.sleep(0.1)

# Close the data file to write to
# f.close()

# # Close the port in use
rga.close()

读取的数据: b'0 \ n \ r0 \ n \ r0 \ n \ r0 \ n \ r0 \ n \ r0 \ n \ r0 \ n \ r' b':\ xff' b'\ xff \ xff' b'] \ x06' b'\ x00 \ x00' b'a \ x14' b'\ x00 \ x00' b'd \ n' b'\ x00 \ x00' b'\ xf5 \ r' b'\ x00 \ x00' b'^ \ x13' b'\ x00 \ x00' b'3 \ x1c' b'\ x00 \ x00' b'\ x88 \ x1c' b'\ x00 \ x00' b'\ xa4' b'\ x00 \ x00' b',\ x1f' b'\ x00 \ x00' b'\ x12' b'\ x00 \ x00' b'\ xaa \ x0c' b'\ x00 \ x00' b'\ x9d \ x08' b'\ x00 \ x00' b'〜\ t' b'\ x00 \ x00' b'\ xf3 \ xfa' b'\ xff \ xff' b'#\ xfd' b'\ xff \ xff' b'\ xaa \ x07' b'\ x00 \ x00' b'\ xb3 \ x0f' b'\ x00 \ x00' b'\ xb4 \ x04' b'\ x00 \ x00' b'\ x94 \ x12' b'\ x00 \ x00' b'\ xcc \ x11' b'\ x00 \ x00' b'\ xa5 \ x06' b'\ x00 \ x00' b'\ x8c \ x06' b'\ x00 \ x00' b'\ xcb \ x00' b'\ x00 \ x00' b'\ x8d \ xf4' b'\ xff \ xff' b'\ xb7 \ xef' b'\ xff \ xff' b'\ x19 \ xf7' b'\ xff \ xff' b'-\ xf8' b'\ xff \ xff' b'S \ x02' b'\ x00 \ x00' b'v \ x01' b'\ x00 \ x00' b'\ x88 \ xfd' b'\ xff \ xff' b'U \ xfd' b'\ xff \ xff' b'<\ x07' b'\ x00 \ x00' b'j \ x00' b'\ x00 \ x00' b'\ x9f \ x00' b'\ x00 \ x00' b'O \ x01' b'\ x00 \ x00' b'n \ xfe' b'\ xff \ xff' b'\ x1d \ x02' b'\ x00 \ x00' b'\ xa7 \ x00' b'\ x00 \ x00' b'o \ xfd' b'\ xff \ xff' b'\ xa7 \ x02' b'\ x00 \ x00' b'\ xd3 \ n' b'\ x00 \ x00' b'\ x93 \ x06' b'\ x00 \ x00 \ x0f \ n' b'\ x00 \ x00' b'\ x05 \ x10' b'\ x00 \ x00' b'\ xa2 \ x07' b'\ x00 \ x00' b'?\ x05' b'\ x00 \ x00' b'3 \ x00' b'\ x00 \ x00' b'\ x9a \ x06' b'\ x00 \ x00' b'$ \ xfc' b'\ xff \ xff' b'&\ xf8' b'\ xff \ xff' b'\ xde \ xf9' b'\ xff \ xff' b'\ x87 \ xff' b'\ xff \ xff' b'\ xa1 \ x03' b'\ x00 \ x00' b'k \ xff' b'\ xff \ xff' b'\ x83 \ x01' b'\ x00 \ x00' b'{\ x00' b'\ x00 \ x00' b'k \ x02' b'\ x00 \ x00' b'S \ t' b'\ x00 \ x00' b'S \ x0e' b'\ x00 \ x00' b'\ xaa \ r' b'\ x00 \ x00' b'\ xbc \ xfe' b'\ xff \ xff' b'\ x9e \ xfd' b'\ xff \ xff' b'V \ x00' b'\ x00 \ x00' b'= \ x0e' b'\ x00 \ x00' b'\ xa9 \ xfe' b'\ xff \ xff' b'R \ xff' b'\ xff \ xff' b'\ x12 \ xfe' b'\ xff \ xff' b'\ xb9 \ xff' b'\ xff \ xff' b'\ x89 \ x06' b'\ x00 \ x00' b'\ x03 \ n' b'\ x00 \ x00' b'\ xeb \ x04' b'\ x00 \ x00' b'\ xe5 \ x05' b'\ x00 \ x00' b'\ xc7 \ xfd' b'\ xff \ xff' b'\ xe9 \ xfa' b'\ xff \ xff' b'\ x94 \ xf4' b'\ xff \ xff' b'\ x14 \ xf3' b'\ xff \ xff' b'\ xe9 \ xff' b'\ xff \ xff' b'D \ x01 \ x00 \ x00' b'v \ x01' b'\ x00 \ x00' b'T \ x05' b'\ x00 \ x00' b'X \ x02' b'\ x00 \ x00' b'x \ x04' b'\ x00 \ x00' b'!\ xff' b'\ xff \ xff' b'U \ x05' b'\ x00 \ x00' b'a \ x00' b'\ x00 \ x00' b'\ x9d \ t' b'\ x00 \ x00' b'\ x91 \ n' b'\ x00 \ x00' b'\ xb5 \ x05' b'\ x00 \ x00' b':\ xff' b'\ xff \ xff' b'C \ xfb' b'\ xff \ xff' b'\ xec \ t' b'\ x00 \ x00' b'u \ xfb' b'\ xff \ xff' b'\ x8b \ x03' b'\ x00 \ x00' b'\ xdf \ xf9' b'\ xff \ xff' b'\ xed \ xfc \ xff \ xff \ xf2 \ xf1 \ xff \ xff \ x1b \ x02 \ x00 \ x00 \ x15 \ x02 \ x00 \ x00 \ xc1 \ x00 \ x00' b'\ x00 \ xa1 \ x08 \ x00 \ x00n \ xfc \ xff \ xff4 \ x07 \ x00 \ x00 \ xdd \ x0b \ x00 \ x00 \ xf1 \ x0b \ x00 \ x00G \ n \ x00 \ x00 \ x00 \ x12 \ x0b \ x00 \ x00M \ x0f \ x00 \ x00 \ x97 \ x11 \ x00 \ x00 \ xf8 \ n \ x00 \ x00 \ xd0 \ x08 \ x00 \ x00 \ xbc \ x05 \ x00 \ x00E \ xfd \ xff \ xff \ xff \ xf2 \ t \ x00 \ x00 \\ x00 \ x00 \ x00 \ xc0 \ x06 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00R \ x06 \ x00 \ x00 \ x84 \ x84 \ x0c \ x00 \ x00 \ x1e \ x10 \ x00 \ x00 \ xba \ n \ x00 \ x00Y \ x1d \ x00 \ x00 \ xd2 \ x15 \ x00 \ x00 \ x03 \ r \ x00 \ x00 \ xb4 \ xf8 \ xff \ xffI \ x0b \ x00 \ x00l \ x0l \ x0f \ x00 \ x00C \ x03 \ x00 \ x00 \ xf3' b'\ xfa \ xff \ xffn \ xfe \ xff \ xff1 \ x08 \ x00 \ x00 = \ x04 \ x00 \ x00 \ xeb \ xfd \ xff \ xff \ xb5 \ xff \ xff \ xff \ xa3 \ xfe \ xff \ xff \ xc9 \ xfa \ xff \ xff $ \ t \ x00 \ x00 \ xd7 \ r \ x00 \ x00 \ xe7 \ n \ x00 \ x00 \ x9e \ x11 \ x00 \ x00 \ xa5 \ x0b \ x00 \ x00 \ xf3 \ x08 \ x00 \ x00q \ x0b \ x00 \ x00r \ x08 \ x00 \ x00 \ xbf \ x04 \ x00 \ x00 <\ x06 \ x00 \ x00 \ x96 \ x96 \ x01 \ x00 \ x00 \ x8c \ r \ x00 \ x00w \ x0b \ x00 \ x00e \ x10 \ x00 \ x00 \ xce \ x0c \ x00 \ x00x \ x0c \ x00 \ x00 \ xb1 \ x01 \ x00 \ x00 \ x00 \ x9c \ x01 \ x00 \ x00 \ xbc \ x06 \ x06 \ x00 \ x00 \ xc5 \ x0c \ x00 \ x00j \ x16 \ x00 \ x00 \ x00 \ x0f \ x00 \ x00 \ xd7 \ t' b'\ x00 \ x00 \ xc1 \ t \ x00 \ x00 \ xb1 \ x04 \ x00 \ x00P \ n \ x00 \ x00L \ x0f \ x00 \ x00 \ x11 \ x10 \ x00 \ x00 \ x8e \ x0e \ x00 \ x00l \ t \ x00 \ x00 \ xcf \ x04 \ x00 \ x00 \ xd3 \ xfd \ xff \ xff \ xb1 \ t \ x00 \ x00 \ x1b \ x18 \ x00 \ x00 \ x93 \ x0c \ x00 \ x00 \ x00_ \ x06 \ x00 \ x00 \ xd0 \ x04 \ x00 \ x00 \ x1c \ x02 \ x00 \ x00T \ x06 \ x00 \ x00V \ x06 \ x00 \ x00g \ x04 \ x00 \ x008 \ xf6 \ xff \ xff(\ x07 \ x00 \ x00 \ x10 \ r \ x00 \ x00j \ t \ x00 \ x00 \ x11 \ x11 \ x00 \ x00 \ x8d \ x0b \ x00 \ x00 \ xf7 \ xfd \ xff \ xff` \ x02 \ x00 \ x00r \ n \ x00 \ x00 \ xef \ x0e \ x00 \ x00' b'\ n \ x10 \ x00 \ x00 \ xaa \ n \ x00 \ x00> \ x03 \ x00 \ x00 \ x04 \ x05 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00'

0 个答案:

没有答案