在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如果在没有"()“的情况下进行回调,则会得到另一个错误:
wpi.wiringPiISR(4, wpi.INT_EDGE_BOTH, my_int)
> TypeError: in method 'wiringPiISR', argument 3 of type 'void (*)(void)'我做错什么了?
发布于 2013-11-07 10:21:35
我对C不太在行,但据我从源代码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)""'");
}因此,我建议显式地返回my_int()函数中的my_int或1。现在,python对已到达函数代码末尾但没有返回值的函数返回None。
修改后的代码:
#!/usr/bin/env python2
import wiringpi2
import time
def my_int():
print('Interrupt')
return True
# setup
wiringpi2.wiringPiSetupGpio()
# set up pin 4 as input
wiringpi2.pinMode(4, 0)
# enable pull up down for pin 4
wiringpi2.pullUpDnControl(4, 1)
# attaching function to interrupt
wiringpi2.wiringPiISR(4, wiringpi2.INT_EDGE_BOTH, my_int)
while True:
time.sleep(1)
print('Waiting...')编辑:您似乎错误地初始化了wiringpi2。详细信息请查看教程:http://raspi.tv/2013/how-to-use-wiringpi2-for-python-on-the-raspberry-pi-in-raspbian
https://stackoverflow.com/questions/19831498
复制相似问题