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

旧巷老猫 提交于 2019-12-04 03:57:27

I came across this when looking how to do a USBDEVFS_RESET and thought I'd share what I found about _IO: 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.

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.

The macro USBDEVFS_RESET is defined in a system header file somewhere.

You can search for it and replace termios.USBDEVFS_RESET with the actual value.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!