您尚未登录。

楼主 #1 2019-03-02 09:57:49

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,236
积分: 9197

离线

楼主 #2 2019-03-02 10:00:50

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,236
积分: 9197

Re: Windows上用 Python 脚本控制 CP2104 GPIO 输出

2019-03-02_095855.png

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)
  1. 用 zadig 给cp2104 强制装上 libusb-win32 驱动程序

  2. 运行D:\Python\Python37\python.exe cp2104.py





离线

楼主 #3 2019-03-02 10:54:05

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,236
积分: 9197

Re: Windows上用 Python 脚本控制 CP2104 GPIO 输出

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





离线

楼主 #4 2019-03-02 10:55:51

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,236
积分: 9197

Re: Windows上用 Python 脚本控制 CP2104 GPIO 输出

https://stackoverflow.com/questions/38658907/trouble-using-pyusb-to-read-write-from-usb-device-timeouts

解决方案: 在 find() 后加 reset()

import time
import usb.core
import usb.util

PID = 0xea60
VID = 0x10c4

dev = usb.core.find(idVendor=VID, idProduct=PID)

dev.reset()

if not dev:
        print("CP2104 was not found sad")
        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)





离线

#5 2020-10-09 21:09:20

九重天
会员
注册时间: 2020-10-06
已发帖子: 25
积分: 17.5

Re: Windows上用 Python 脚本控制 CP2104 GPIO 输出

感谢楼主, 测试py+cp2104测试成功.

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn