工具:Python 3.2.3 with pyserial-2.6

API地址:http://pyserial.sourceforge.net/pyserial_api.html

关键点:

//Configure
try:
    relay=serial.Serial(port='COM5', baudrate='9600', bytesize=8,parity='N',stopbits=1)
except Exception as ex:
    print("Failed to create a serial port on port:", relay)
    raise ex
print(relay)
HEX Transfer
bytes.fromhex('FF 01 00 01 EE')
//Open port
try:
     if relay.isOpen()==False:
        relay.open()
    except Exception as ex:
        print("Failed to initial")
        raise ex
//Close port
relay.close()
//Write
relay.write(w_char)
//Read
relay.read(8)