Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly convert a C ioctl call to a python fcntl.ioctl call?

Following an example on resetting a serial port in Linux I wanted to translate the following snippet

fd = open(filename, O_WRONLY);
ioctl(fd, USBDEVFS_RESET, 0);
close(fd);

into valid python code. Here is what I have tried so far

file_handler = open(self._port, 'w')
fcntl.ioctl(file_handler, termios.USBDEVFS_RESET)
file_handler.close()

which ends with an error 'module' object has no attribute 'USBDEVFS_RESET'. The termios documentation is not very helpful in this point, as it does not list the possible properties of termios. See also the fcntl documentation for an example of such a termios property.

How to I 'convert' the C code to python2.7 code correctly?

like image 668
Alex Avatar asked Jan 31 '13 12:01

Alex


People also ask

How to use fcntl in Python?

Method fcntl. lockf(fd, operation[, length[, start[, whence]]]) This method is used to perform as a wrapper around the locking calls. The operation argument is passed to lock or unlock the file.

What is an ioctl request?

In computing, ioctl (an abbreviation of input/output control) is a system call for device-specific input/output operations and other operations which cannot be expressed by regular system calls. It takes a parameter specifying a request code; the effect of a call depends completely on the request code.


2 Answers

ioctl-opt (pypi) is a small python module translating needed C preprocessor macros to python. For a simple usage example, see this hidraw implementation.

Note that defining ctype structures can be needed (depending on call type) so you can actually pass parameters.

Disclosure: I am the author of both modules.

like image 198
vpelletier Avatar answered Oct 18 '22 13:10

vpelletier


I came across this when looking how to do a USBDEVFS_RESET and thought I'd share what I found about _IO: https://web.archive.org/web/20140430084413/http://bugcommunity.com/wiki/index.php/Develop_with_Python#Introduction_to_ioctl_calls_in_python

So, what I have so far is the following:

from fcntl import ioctl

busnum = 1
devnum = 10

filename = "/dev/bus/usb/{:03d}/{:03d}".format(busnum, devnum) 

#define USBDEVFS_RESET             _IO('U', 20)
USBDEVFS_RESET = ord('U') << (4*2) | 20

fd = open(filename, "wb")
ioctl(fd, USBDEVFS_RESET, 0)
fd.close()

You can get the busnum and devnum from lsusb.

EDIT: above link was dead, URL was replaced to the last archived version.

like image 25
Tim Tisdall Avatar answered Oct 18 '22 14:10

Tim Tisdall