如何正确地将 C ioctl 调用转换为 python fcntl.ioctl 调用?

2024-02-13

以下是一个例子重置串行端口 http://www.roman10.net/how-to-reset-usb-device-in-linux/在 Linux 中我想翻译以下代码片段

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

转换为有效的 python 代码。这是我到目前为止所尝试过的

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

以错误结束'module' object has no attribute 'USBDEVFS_RESET'. The termios 文档 http://docs.python.org/2/library/termios.html#module-termios在这一点上不是很有帮助,因为它没有列出可能的属性termios。另请参阅fcntl 文档 http://docs.python.org/2/library/fcntl.html举个这样的例子termios财产。

如何正确地将 C 代码“转换”为 python2.7 代码?


我在寻找如何执行 USBDEVFS_RESET 时遇到了这一点,并认为我应该分享我对 _IO 的发现:https://web.archive.org/web/20140430084413/http://bugcommunity.com/wiki/index.php/Develop_with_Python#Introduction_to_ioctl_calls_in_python https://web.archive.org/web/20140430084413/http://bugcommunity.com/wiki/index.php/Develop_with_Python#Introduction_to_ioctl_calls_in_python

所以,到目前为止我所拥有的是以下内容:

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()

您可以获得busnum and devnum from lsusb.

编辑:上面的链接已失效,URL 已替换为最后存档的版本。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何正确地将 C ioctl 调用转换为 python fcntl.ioctl 调用? 的相关文章

随机推荐