python serial pass-through

Normally when the python through the serial transmission of serial data, is transmitted in the form of a string

str = ‘abcd’

ser.write (str.decode ()) # str given directly, needs to send a byte, i.e. b'abcd '

data = ser.readline()

If it is sent directly hexadecimal data, use the following method:

ser=serial.Serial('COM6',115200,timeout=1.5,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS)

senddata = '020000AA700019010D44435F5365745F574C4D6F64650201310301310402393503B8'

ser.write(bytes.fromhex(sendData))

 

Obtain raw data from the serial port (hex):

data = str(binascii.b2a_hex(ser.readline())).upper()

Guess you like

Origin www.cnblogs.com/yaner2018/p/11240333.html