house house - 1 year ago 84
Linux Question

How to pass a struct to linux device node in python?

There is a fpga device node in my /dev/. To communicate with its driver, I need to use the following C code.

struct pci_cmd {
unsigned int bar_id;
unsigned int command;
void *device_addr;
void *user_addr;
};

ssize_t f = open ("/dev/de4", O_RDWR);
unsigned short val;
struct pci_cmd read_cmd = { 0, 0, 0x2, &val };
read (f, &read_cmd, sizeof(val));


I found that there is struct class in python, but the
os.read(fd, n)
only accept two arguments. Any idea to do this in python?

Answer Source

You could use termios library of python to communicate if device node is tty I/O. Reference: https://docs.python.org/2/library/termios.html

Also, You could use os.popen() or os.system() and directly write the system command.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download