C ioctl转换为python

2024-10-01 17:32:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个用C写的程序

#include <stdio.h>
#include <fcntl.h>

typedef struct
{
    unsigned regs_size;
    unsigned mem_size;
} info_t;

int main()
{
    int fd = open("/dev/devname", O_RDWR);
    info_t info;
    /* the following 2148038893 is the request number for my device */
    ioctl(f, 2148038893, &info);
    printf("%u, %u\n", info.regs_size, info.mem_size);
    return 0;
}

我想使用python3来操作这个设备,但我不知道如何将上面的代码转换成python3

import os, fcntl
fd = os.open('/dev/devname', os.O_RDWR)
fcntl.ioctl(fd, 2148038893, ...)

如何将信息结构传递到fcntl.ioctl


Tags: devinfosizeincludeosopenmemint
2条回答

有关ioctlhere,请参阅python文档。您可能希望创建一个bytes对象,并将其作为第三个参数传递

我需要一个python结构

import os, fcntl, struct
fd = os.open('/dev/devname', os.O_RDWR)
result = fcntl.ioctl(fd, 2148038893, struct.pack('=II', 0, 0))
regs_size = int.from_bytes(result[:4], byteorder='little')
mem_size = int.from_bytes(result[4:], byteorder='little')
print('%u%u' %(regs_size, mem_size))

相关问题 更多 >

    热门问题