简体   繁体   English

C# 中的十六进制字符串表示法转换为 Python

[英]Hex string notation in C# conversion to Python

I am writing a code to retrieve data from a physiological monitor through a bluetooth to serial port in Python.我正在编写代码以通过蓝牙从生理监视器中检索数据到 Python 中的串行端口。 I need to send a Hex code to the monitor in order to receive the data.我需要向监视器发送一个十六进制代码才能接收数据。 I have successfully connected to the port but I'm having trouble sending the hex code.我已成功连接到端口,但无法发送十六进制代码。 The hex string was given to me but it is from C# and I don't know whether it needs to be converted so that Python can read it.十六进制字符串是给我的,但它来自 C# 我不知道是否需要转换它以便 Python 可以读取它。 When I execute the code below my error message states:当我执行下面的代码时,我的错误消息指出:

TypeError: fromhex() takes exactly one argument (61 given)

This is the string in C notation这是 C 表示法中的字符串

{0x7E,0x00,0x3A,0x00,0x00,0x00,0x00,0x80,0xDE,0xFB,0x2C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,#0x00,0x09,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x05,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x04,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE2,0x7E}

below is my program in Python3下面是我在 Python3 中的程序

import serial

ser = serial.Serial(
    port='/dev/cu.PARANISERIAL-GenericSer',
    baudrate=9600,
    timeout= 10,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS
)

message_bytes = bytes.fromhex(0x7E0x00/0x3A/0x00/0x00/0x00/0x00/0x80/0xDE/0xFB/0x2C/0x00/0x00/0x00/0x00/0x00/0x00/0x00/0x00/0x00/0x09/0x00/0x00/0x00/0x00/0xFF/0x00/0x00/0x00/0x00/0x00/0x00/0x00/0x00/0x00/0x00/0x00/0x00/0x00/0x00/0x00/0x01/0x05/0x00/0x0C/0x00/0x00/0x00/0x00/0x00/0x04/0x05/0x00/0x00/0x00/0x00/0x00/0x00/0x00/0xE2/0x7E)

ser.write(message_bytes)

print (ser.is_open)  # True for opened
if ser.is_open:
    while True:
            data = ser.read(10)
            print(data)
            print(str(data.decode()))
    else:
        print('no data')
        time.sleep(1)
else:
    print('z1serial not open')
s = ser.read(100)
print(str(s.decode()))
ser.close()
print(ser.isOpen())

Any guidance would be greatly appreciated.任何指导将不胜感激。 Thank you谢谢

bytes.fromhex will convert the string if all the non-hex characters are removed:如果删除了所有非十六进制字符, bytes.fromhex将转换字符串:

>> h = '0x7E,0x00,0x3A,0x00,0x00,0x00,0x00,0x80,0xDE,0xFB,0x2C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,#0x00,0x09,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x05,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x04,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE2,0x7E'

>>> bytes.fromhex(h.replace('0x', '').replace(',', '').replace('#', ''))
b'~\x00:\x00\x00\x00\x00\x80\xde\xfb,\x00\x00\x00\x00\x00\x00\x00\x00\x00\t\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x05\x00\x0c\x00\x00\x00\x00\x00\x04\x05\x00\x00\x00\x00\x00\x00\x00\xe2~'

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

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