【发布时间】:2015-03-23 21:49:14
【问题描述】:
我购买了两个 lctech-inc.com 011801 USB 继电器模块。我正在尝试用 python 和 pyserial 来控制它们。该模块确实显示为 USB-SERIAL CH340 (COM5)。支持信息说:
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
我正在使用以下 python 代码打开继电器,但它不起作用:
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()
只要我使用正确的 COM 端口 COM5,就不会出现任何错误。
有什么建议吗?任何帮助将不胜感激。 TIA
【问题讨论】: