如何将结构传递给python中的linux设备节点?
问题描述:
我的/ dev /中有一个fpga设备节点。要与其驱动程序通信,我需要使用以下C代码。如何将结构传递给python中的linux设备节点?
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中有struct结构类,但os.read(fd, n)
只接受两个参数。任何想法在Python中做到这一点?
答
如果设备节点是tty I/O,您可以使用python的termios库进行通信。 参考:https://docs.python.org/2/library/termios.html
此外,您可以使用os.popen()或os.system()并直接写入系统命令。
这不是一个tty I/O。这是一个字符设备。问题是设备驱动程序需要struct pci_cmd信息。 – house