Python如何为GPIO读取匿名linux文件

2024-10-02 08:18:43 发布

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

正在处理新的linux gpio实现的python3实现。初始设置非常有效:

class gpioevent_request(ctypes.Structure):
    _pack_ = 1
    _fields_ = [
                ('lineoffset', ctypes.c_ulong),
                ('handleflags', ctypes.c_ulong),
                ('eventflags', ctypes.c_ulong),
                ('consumer_label', ctypes.c_char * 32),
                ('fd', ctypes.c_int),
                ]

class gpioevent_data(ctypes.Structure):
    _pack_ = 1
    _fields_ = [
                ('timestamp', ctypes.c_ulonglong),
                ('id', ctypes.c_ulong),
                ]

self.event = gpioevent_request()
self.eventData = gpioevent_data()
...
# Open /dev/gpiochipx with os.open and set up the event structure
...
fcntl.ioctl(self._fd, GPIO_GET_LINEEVENT_IOCTL, self.event)

当我试图读取由GPIO\u GET\u LINEEVENT\u IOCTL调用创建的匿名linux文件中存储的事件信息时,我的挑战就来了。你知道吗

如果我尝试操作系统读取我得到一个无效的参数响应:

os.read(self.event.fd, bytesToRead)

如果我尝试libc read,那么它将读取文件,但我的数据(timestamp和id)将全部为零:

import ctypes.util
libc = ctypes.CDLL(ctypes.util.find_library('c'))
self.f_read = libc.read
self.f_read(self.event.fd, ctypes.byref(self.eventData), ctypes.sizeof(self.eventData))

使用epoll似乎确实会在输入引脚的上升或下降边缘触发文件,但它会间歇性地工作:

p = select.epoll()
p.register(self.event.fd, select.EPOLLIN | select.EPOLLET | select.EPOLLPRI)

任何洞察都是非常感激的。你知道吗


Tags: 文件selfeventreadrequestlinuxctypesstructure
1条回答
网友
1楼 · 发布于 2024-10-02 08:18:43

我怀疑你根本不想_pack_你的ctypes.Structures。医生说

By default, Structure and Union fields are aligned in the same way the C compiler does it. It is possible to override this behavior be specifying a pack class attribute in the subclass definition.

因为这些是与libc调用共享的结构,所以您希望它们与用于查看它们的默认布局相同。你知道吗

相关问题 更多 >

    热门问题