import time
import usb.core
import usb.util
PID = 0xea60
VID = 0x10c4
dev = usb.core.find(idVendor=VID, idProduct=PID)
if not dev:
print("CP2104 was not found :(")
exit(1)
print("Yeeha! Found CP2104")
reqType = 0x41
bReq = 0xFF
wVal = 0x37E1
while True:
wIndex = 0xffff
print("toggling On")
dev.ctrl_transfer(reqType, bReq, wVal, wIndex, [])
time.sleep(5)
print("toggling Off")
wIndex = 0x00ff
dev.ctrl_transfer(reqType, bReq, wVal, wIndex, [])
time.sleep(5)
用 zadig 给cp2104 强制装上 libusb-win32 驱动程序
运行D:\Python\Python37\python.exe cp2104.py
离线
debugdump@ubuntu:~$ sudo python3 /mnt/hgfs/D/cp2104.py
Yeeha! Found CP2104
toggling On
Traceback (most recent call last):
File "/mnt/hgfs/D/cp2104.py", line 22, in <module>
dev.ctrl_transfer(reqType, bReq, wVal, wIndex, [])
File "/usr/local/lib/python3.4/dist-packages/usb/core.py", line 1043, in ctrl_transfer
self.__get_timeout(timeout))
File "/usr/local/lib/python3.4/dist-packages/usb/backend/libusb1.py", line 883, in ctrl_transfer
timeout))
File "/usr/local/lib/python3.4/dist-packages/usb/backend/libusb1.py", line 595, in _check
raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 110] Operation timed out
在Ubuntu Linux 下跑会出现一个超时错误: [Errno 110] Operation timed out
离线
解决方案: 在 find() 后加 reset()
import time
import usb.core
import usb.utilPID = 0xea60
VID = 0x10c4dev = usb.core.find(idVendor=VID, idProduct=PID)
dev.reset()
if not dev:
print("CP2104 was not found ")
exit(1)
print("Yeeha! Found CP2104")reqType = 0x41
bReq = 0xFF
wVal = 0x37E1while True:
wIndex = 0xffff
print("toggling On")
dev.ctrl_transfer(reqType, bReq, wVal, wIndex, [])
time.sleep(5)
print("toggling Off")
wIndex = 0x00ff
dev.ctrl_transfer(reqType, bReq, wVal, wIndex, [])
time.sleep(5)
离线
感谢楼主, 测试py+cp2104测试成功.
离线