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

后端 未结 3 1858
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-02 08:11

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 cod

3条回答
  •  隐瞒了意图╮
    2021-01-02 08:39

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

提交回复
热议问题