如何在Python中的Linux设备文件上执行低级I/O? [英] How do I perform low level I/O on a Linux device file in Python?
问题描述
我有一个设备,该设备响应写入设备文件的命令返回一个字符串.我能够使用以下代码将命令写入设备并读取C中的返回字符串:
I have a device which returns a string in response to commands written to the device file. I am able to write commands to the device and read the return string in C with code that looks like:
int dev = open("/dev/USBDev251", O_RDWR);
write(dev, data, sizeof(data));
read(dev, buff, 16);
我正在尝试在Python中执行以下操作:
I am trying to do the same in Python with:
dev = os.open("/dev/USBDev251", os.O_RDWR)
os.write(dev, data)
os.read(dev, 16)
写入成功,但是仅返回一个空字符串.我在这里想念什么?
The write is successful, but only an empty string is returned. What am I missing here?
推荐答案
问题出在设备驱动程序上.在驱动程序file_operations
中注册的read()
方法调用copy_to_user()
,但随后返回0
,而不是复制到用户空间的字节数.
The problem turned out to be with the device driver. The read()
method registered with the driver's file_operations
invoked copy_to_user()
but then returned 0
instead of the number of bytes copied to userspace.
有效" 上方的C代码,因为它实际上并未检查read()
的返回值,并且buff
都已填充响应.
The C code above "worked" because it didn't actually check the return value of read()
and buff
was getting populated with the response.
这篇关于如何在Python中的Linux设备文件上执行低级I/O?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!