如何将结构传递给 python 中的 linux 设备节点?

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

我的/dev/下有一个fpga设备节点。要与其驱动程序通信,我需要使用以下C代码。

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

我发现python中有结构class,但是os.read(fd, n)只接受两个参数。想在 python 中执行此操作吗?

如果设备节点是 tty I/O,您可以使用 python 的 termios 库进行通信。 参考:https://docs.python.org/2/library/termios.html

另外,您可以使用os.popen()或os.system(),直接写系统命令。