13

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anmol Sarma
  • 139
  • 1
  • 1
  • 6
  • Does os.write return the expected number of bytes you wanted to write? – hogliux Jan 29 '14 at 10:46
  • @hogliux, Yes the write is successful. – Anmol Sarma Jan 29 '14 at 11:04
  • Are you sure that the device actually returns something back? – jb. Jan 29 '14 at 11:08
  • I'm looking through the source code of `os.open`, `os.write` and `os.read` at http://hg.python.org/cpython/file/v2.7.3/Modules/posixmodule.c#l6428 and I cannot see why it shouldn't work. Are you printing the return-value of os.read? – hogliux Jan 29 '14 at 11:34
  • After writing data to the disk, the position of the file descriptor would be at the end of data (numerically equal to length of data). Since fd is at the end of file, empty string is returned. lseek should be used to move the file descriptor back to its initial position before reading the contents. – Vamsi Krishna Jan 29 '14 at 11:44
  • 1
    @VamsiKrishna, this would be true for a regular file on disk but not necessarily for device files. Also, the poster says that the C code works. – hogliux Jan 29 '14 at 11:47

3 Answers3

10

According to the os.write documentation:

Note: This function is intended for low-level I/O and must be applied to a file descriptor as returned by os.open() or pipe(). To write a “file object” returned by the built-in function open() or by popen() or fdopen(), or sys.stdout or sys.stderr, use its write() method.

You shouldn't be mixing and matching here. If you use the global function open() to open a file, then you must only use the file object's read() and write() methods. Conversely, if you use os.open() to open a file, then you must only use os.read() and os.write().

So, try replacing your call to open() with os.open(); or, keep the open() call, and replace os.write(dev, ...) with dev.write(...) and replace os.read(dev, ...) with dev.read(...).

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
5

Add an os.lseek() to seek back to the beginning of the string you wrote. Currently you wrote 16 bytes which advanced the pointer. When you read, you start reading at the current pointer so you need to back it up to the start of what you wrote.

This worked for me:

#!/usr/bin/python
import os

data = "xxxxxxxxxxxxxxxx"
dev = os.open("/dev/sdp1", os.O_RDWR)
os.write(dev,data)
os.lseek(dev,0,os.SEEK_SET)
print os.read(dev,16)
heemayl
  • 39,294
  • 7
  • 70
  • 76
ttwalkertt
  • 51
  • 1
  • 2
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.

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

Anmol Sarma
  • 139
  • 1
  • 1
  • 6