使用wiringPi2-python(非root)分段故障捕获GPIO

时间:2013-12-29 18:45:41

标签: python c raspberry-pi gpio

我正在使用wiringPi2-python将覆盆子GPIO引脚从低电平切换为高电平并返回。 一切正常但在它切换引脚的值后立即抛出Segmentation fault并且程序停止 我需要使用这种方法,因为这似乎是在没有sudo

的情况下访问GPIO引脚的唯一方法

在启动程序之前,我需要设置引脚输出并导出它们:

$ echo 17 > /sys/class/gpio/export
$ echo out > /sys/class/gpio/gpio17/direction

然后是一些python shell:

$ python
Python 2.7.3 (default, Jan 13 2013, 11:20:46) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import wiringpi2 as pi
>>> pi.wiringPiSetupSys()
0
>>> pi.digitalWrite(17, 1)
Segmentation fault
$

我试过这种方法,但它没有更好。该计划仍然停止。:

try:
        pi.digitalWrite(17, 0)
except:
        print('got an error')
print('just printing something to see if gets to end')

所以我的问题是如何正确捕获错误,所以我可以忽略它,因为代码实际上有效 Ps:这可能值得一个错误报告,但我想先了解它。

1 个答案:

答案 0 :(得分:0)

所以我想通了。我需要为digitalwrite制作另一个流程。在这种情况下,新创建的进程会停止,但程序的其余部分可以继续工作。

import wiringpi2 as pi
from multiprocessing import Process

def process(choice):
        if choice == "1":
                pi.digitalWrite(17, 1)
        else:
                pi.digitalWrite(17, 0)

if __name__ == '__main__':
        pi.wiringPiSetupSys()
        choice = raw_input(">")
        p = Process(target=process, args=(choice,))
        p.start()
        p.join()

print('just printing something to see if gets to end')