使用ioctl使用Python写入USB设备

Eri*_*ett 1 python usb

使用Python,我试图使用ioctl写入USB传感器.我有很多直接或通过pyusb从设备读取的例子,或简单的文件写入,但任何更复杂的东西都会从雷达中消失.

我需要使用control_transfer来编写功能报告消息

命令是 ioctl(devicehandle, Operation, Args)

我的问题是确定正确的操作.Args,我认为应该是一个包含设备功能报告的缓冲区?加上Mutable标志设置为true

任何帮助或建议都会受到极大的欢迎

我应该补充; 使用Python的原因是代码必须与设备无关.

Nim*_*lar 5

一个很好的例子是linuxdvb和V4l2的python绑定.http://pypi.python.org/pypi/linuxdvbhttp://pypi.python.org/pypi/v4l2但这些并不是非常pythonic.仅适用于Linux/Unix系统.

你必须在ARGSpython的帮助下将结构翻译成可以理解的东西ctype.该Operation值与中的值相同C.

对应一个C电话

struct operation_arg {
    int fields1;
    int fields2;
}

struct operation_arg Args; 
Args.field1 = data1;
Args.field2 = data2;

devicehandle = open("/dev/my_usb", O_RDWR); 

retval = ioctl(devicehandle, Operation, &Args);
/* check retval value */
Run Code Online (Sandbox Code Playgroud)

你必须在python中定义对应的Ctype struct operation_arg.它会提供这种代码

import ctypes
import linuxdvb
import fcntl

class operation_arg(ctypes.Structure):
    _fields_ = [
        ('field1', ctypes.c_int),
        ('field2', ctypes.c_int)
    ]

Args = operation_args()
Args.field1 = data1;
Args.field2 = data2;

devicehandle = open('/dev/my_usb', 'rw')

# try:
fcntl.ioctl(devicehandle, operation, Args)
# exception block to check error
Run Code Online (Sandbox Code Playgroud)