How do I perform low level I/O on a Linux device file in Python?

linux, posix, python

Solution

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.

The C code above "worked" because it didn't actually check the return value of `read()` and `buff` was getting populated with the response.

Problem

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

Original source