如何正确地将C ioctl调用转换为python fcntl.ioctl调用? [英] How to properly convert a C ioctl call to a python fcntl.ioctl call?

查看:211
本文介绍了如何正确地将C ioctl调用转换为python fcntl.ioctl调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重置串行端口为例>在Linux中,我想翻译以下代码段

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

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

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

,以错误'module' object has no attribute 'USBDEVFS_RESET'结尾. termios文档在这一点上不是很有帮助没有列出termios的可能属性.有关此类termios属性的示例,另请参见 fcntl文档.

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.

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

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

推荐答案

我在寻找如何进行USBDEVFS_RESET时遇到了这个问题,并认为我会分享有关_IO的发现: .com/wiki/index.php/Develop_with_Python#Introduction_to_ioctl_calls_in_python> http://bugcommunity.com/wiki/index.php/Develop_with_Python#Introduction_to_ioctl_calls_in_python

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

您可以从lsusb获取busnumdevnum.

这篇关于如何正确地将C ioctl调用转换为python fcntl.ioctl调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆