问题描述
我正在使用connectionPi2-python将树莓GPIO引脚从低切换到高,然后再切换回.切换引脚的值后,一切都可以正常进行,而Segmentation fault
会被抛出并且程序停止.
我需要使用这种方法,因为这似乎是不使用sudo
i'm using wiringPi2-python to switch a raspberry GPIO pin from low to high and back.Everything works BUT right after it switches the pin's value a Segmentation fault
is thrown and the program stops.
I need to use this approach because this seems to be the only way to access the GPIO pins without sudo
在启动程序之前,我需要设置引脚以进行输出和输出,这样:
before starting the program i need to setup the pins to output and export them so:
$ echo 17 > /sys/class/gpio/export
$ echo out > /sys/class/gpio/gpio17/direction
然后是一些python shell:
and then a bit of 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
$
我尝试了这种方法,但是并没有更好.程序仍然停止.:
i tried this approach, but it wasn't any better. The program still stopped.:
try:
pi.digitalWrite(17, 0)
except:
print('got an error')
print('just printing something to see if gets to end')
所以我的问题是如何正确捕获错误,所以我可以忽略它,因为代码可以正常工作.
附言:这可能值得一个错误报告,但我想先解决这个问题.
So my question is how can i catch the error properly, so i could just ignore it because the code actually works.
Ps: This is propably worth a bug report but i want to get my head around it first.
推荐答案
所以我想通了.我需要为digitalwrite
做另一个处理.在这种情况下,新创建的进程将停止,但程序的其余部分可以继续工作.
so i figured it out. i need to make another process for digitalwrite
. In that case the newly created process stops but the rest of the program can continue working.
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')
这篇关于使用connectionPi2-python(非根)分段故障捕获的GPIO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!