pyserial 2.7 and USB Relay module
Question: I've purchased two lctech-inc.com 011801 USB Relay Modules. I'm trying to
control them with python and pyserial. The module does show up as USB-SERIAL
CH340 (COM5). The support information says:

    
    
    Communication baud rate: 9600bps; Protocol: start: 0 x A0, 
    switch address: 0 x 01, operation data: 0 x 00 (off), 0 x 01 (on), 
    check code: on: A0 01 01 A2, off: A0 01 00 A1
    

I'm using the following python code to turn the relay on but it doesn't work:

    
    
        import sys
        import serial
        portName = "COM5"
        relayNum = "1"
        relayCmd = "on"
        #Open port for communication    
        serPort = serial.Serial(portName, 9600, timeout=1)
        #Send the command
        serPort.write("relay "+ str(relayCmd) +" "+ str(relayNum) + "\n\r")
        print "Command sent..."
        #Close the port
        serPort.close()
    

As long as I use the correct COM port, COM5, I do not get any errors.

Any suggestions? Any help would be greatly appreciated. TIA


Answer: It looks like you need to send the byte 0x01 to turn the relay on and 0x00 to
turn it off, not strings "on" and "off".

Try `serPort.write(0x01)` to turn the relay on.

Edit: It also looks like you might need to send the start byte 0xA0 first.

