2

Using Python, I am trying to write to a USB sensor using ioctl. I have loads of examples of reading from devices either directly or via pyusb, or simple file writes, but anything more complicated disappears off the radar.

I need to use a control_transfer to write Feature Report message

The command is ioctl(devicehandle, Operation, Args)

The issue I have is determining the correct Operation. The Args, I believe should be a buffer containing the Feature Report for the device? plus a Mutable flag set to true

Any help or advice would be greatly received

I should add; the reason for using Python is the code must be device independent.

2 Answers 2

6

A good example are the python binding for linuxdvb and V4l2. http://pypi.python.org/pypi/linuxdvb and http://pypi.python.org/pypi/v4l2 but these are not very pythonic. Only works with Linux/Unix system.

You have to translate the ARGS structure to something understandable by python with the help of ctype. The Operation value is the same as the one in C.

Corresponding to a C call

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 */

You'll have to define in python the Corresponding Ctype for the struct operation_arg. It will give this kind of code

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
Sign up to request clarification or add additional context in comments.

Comments

0

According to the documentation, ioctl() in the fcntl module is unix specific, so it will not work in Windows. There seems to be a Windows variant named DeviceIoControl() that works similarly.

IOCTLs are declared by the device driver or operating system, so I very much doubt that there are IOCTL operations that have the same operation id (IOCTL number) and same parameters on different operating systems.

For Linux, you can check the header files for specific device drivers or possibly some usb core header file for valid IOCTLs.

1 Comment

By device independent, I should have said within LINUX processor variants. The USB device supplier is not particularly helpful, but stracing has been quire useful in unravelling the situation. Unfortunately USB arguments are quite obscure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.