【问题标题】:using wiringPi2-python (non root) segmentation fault catching for GPIO使用wiringPi2-python(非root)分段错误捕获GPIO
【发布时间】:2013-12-29 18:45:41
【问题描述】:

我正在使用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:这可能值得一个错误报告,但我想先了解它。

【问题讨论】:

  • 为什么这个标签是“C”?
  • 因为wiringpy2-python底层使用了C。我认为尝试和除外因此而不起作用。虽然不确定

标签: python c raspberry-pi gpio


【解决方案1】:

所以我想通了。我需要为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')

【讨论】:

    猜你喜欢
    • 2014-10-30
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多