寻求USBRelay4型USB四路继电器Python控制指令及datasheet
问题:Python控制USBRelay4 4路USB继电器遇到指令问题
我尝试用Python控制一款12V USB 4路继电器(设备识别为USBRelay4),但遇到了技术障碍——找不到这款设备的datasheet,所以没有正确的控制指令可用。
已进行的尝试
- 电脑将设备识别为HID(人机接口设备)
- 试过多种指令,但都没生效
- 运行了以下代码,但继电器不响应指令,出现错误
运行的代码
import hid def list_hid_devices(): for device in hid.enumerate(): keys = list(device.keys()) keys.sort() for key in keys: print(f"{key}: {device[key]}") print() def send_hid_command(vendor_id, product_id, command): try: device = hid.device() device.open(vendor_id, product_id) print(f"Manufacturer: {device.get_manufacturer_string()}") print(f"Product: {device.get_product_string()}") print(f"Serial No: {device.get_serial_number_string()}") device.write(command) response = device.read(64) print(f"Response: {response}") device.close() except Exception as e: print(f"Error: {e}") print(" HID devices:") list_hid_devices() VENDOR_ID = 0x16C0 PRODUCT_ID = 0x05DF relay_commands = [ [0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], ] for command in relay_commands: send_hid_command(VENDOR_ID, PRODUCT_ID, command)
终端输出
Manufacturer: www.dcttech.com
Product: USBRelay4
Serial No:
Error: read error
Manufacturer: www.dcttech.com
Product: USBRelay4
Serial No:
Error: read error
提问
这款继电器的正确控制指令是什么?哪里可以获取它的datasheet?
答案
针对USBRelay4(VID:0x16C0, PID:0x05DF)这类dcttech出品的HID继电器,正确的控制指令格式通常是8字节数组,规则如下:
- 第1字节:固定为
0x01(报告ID,这款设备无需用0x00) - 第2字节:控制位,每一位对应一路继电器状态:
0x01= 打开第1路0x02= 打开第2路0x04= 打开第3路0x08= 打开第4路- 组合值如
0x0F=打开所有4路,0x00=关闭所有路
- 第3-8字节:固定为
0x00
修正后的指令示例:
relay_commands = [ [0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], # 打开第1路 [0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], # 关闭所有路 [0x01, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], # 打开所有路 ]
另外,你遇到的read error是因为这款设备不会返回响应数据,可去掉device.read(64)代码避免异常,修改后的函数:
def send_hid_command(vendor_id, product_id, command): try: device = hid.device() device.open(vendor_id, product_id) print(f"Manufacturer: {device.get_manufacturer_string()}") print(f"Product: {device.get_product_string()}") device.write(command) print(f"Sent command: {command}") device.close() except Exception as e: print(f"Error: {e}")
关于datasheet获取:
- 这款设备属于dcttech的USBRelay系列,可前往品牌官方支持页面查找文档
- GitHub上有大量针对该设备的开源项目,搜索“USBRelay4 0x16C0 0x05DF”可找到附带指令说明的资料
内容的提问来源于stack exchange,提问作者ukser
相关产品推荐
相关产品推荐

