问题
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
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.
回答1:
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')
来源:https://stackoverflow.com/questions/20828758/using-wiringpi2-python-non-root-segmentation-fault-catching-for-gpio