RaspberryPI Python WiringPi2中断语法

2024-06-03 04:26:17 发布

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

在python2.7/RaspberryPi上测试wiringPi2中断,似乎无法使其正常工作。在

使用以下代码,中断将生成分段错误。在

#!/usr/bin/env python2
import wiringpi2
import time

def my_int():
    print('Interrupt')

wpi = wiringpi2.GPIO(wiringpi2.GPIO.WPI_MODE_PINS)
wpi.pullUpDnControl(4,wpi.PUD_UP) 
wpi.wiringPiISR(4, wpi.INT_EDGE_BOTH, my_int())
while True:
    time.sleep(1)
    print('Waiting...')

Waiting...
Waiting...
Waiting...
Waiting...
Segmentation fault

如果我回拨时没有“()”则会收到另一个错误:

^{pr2}$

我做错什么了???在


Tags: 代码importgpiotimemyusr错误int
1条回答
网友
1楼 · 发布于 2024-06-03 04:26:17

我对C不是很在行,但据我从源代码https://github.com/Gadgetoid/WiringPi2-Python/blob/master/wiringpi_wrap.c所知,出现这个错误是因为以下代码(它检查函数是否返回void并显示错误):

int res = SWIG_ConvertFunctionPtr(obj2, (void**)(&arg3), SWIGTYPE_p_f_void__void);
if (!SWIG_IsOK(res)) {
  SWIG_exception_fail(SWIG_ArgError(res), "in method '" "wiringPiISR" "', argument " "3"" of type '" "void (*)(void)""'");
}

因此,我建议在我的函数中显式返回True或1。现在python对于已经到达函数代码末尾但没有返回值的函数返回None。在

修改代码:

^{pr2}$

编辑:似乎您错误地初始化了wiringpi2。请查看教程了解详细信息:http://raspi.tv/2013/how-to-use-wiringpi2-for-python-on-the-raspberry-pi-in-raspbian

相关问题 更多 >