Python GPIO 写入设置 GPIO 电源输出失败
Python GPIO write to set GPIO power output failing
我正在尝试控制单板计算机 (Vocore v2) 运行 Open-Wrt Linux 上的 GPIO 引脚。该脚本试图通过文件系统控制 GPIO 引脚。然而,虽然引脚完美导出,但引脚的值(或功率)从未设置!有谁知道为什么powerPin()
从来不设置GPIO管脚的power值?我该如何解决这个问题?
谢谢。
我的 Python 脚本:
'''
This script is an introductory attempt to create a global Python GPIO control program.
The pinAction class requires several variables to be set:
[pin number]
[direction (read or write)]
[output value (if write is set)]
[read value (if read is set; updates on interval flag)]
[read update interval (if read is set, this is an integer value indicating the frequency of which pin values are read)]
'''
#Import all modules necessary for the script...
from subprocess import call
import sys
class pinAction:
def __init__(self,pin,direction):
#Set object properties...
self.pin = int(pin)
#Test direction argument...
self.direction = "in" if direction == "in" else "out" if direction == "out" else False
if not self.direction:
#Incorrect argument: terminate the program...
print "Error: pin direction must be 'in' or 'out'. Terminating program..."
sys.exit()
else:
call('echo '+self.direction+' > direction',shell=True)
#Initialize the GPIO pin through the file system...
call("cd /sys/class/gpio && echo "+str(self.pin)+" > export",shell=True)
print "Successfully initialized pin "+str(self.pin)+"."
def powerPin(self, power):
if self.direction == "out":
print "Setting pin power output..."
power = 0 if int(power) == 0 else 1 if int(power) == 1 else False
if not power:
print "Error: power argument must be an integer or string of '0' or '1'."
else:
call("echo "+str(power)+" > value && cat value",shell=True)
#call("ls && echo "+str(power)+" > value",shell=True)
else:
print "Error: pin is not an output pin!"
x = pinAction(1,"out")
#Set pin power to be on...
x.powerPin(1)
脚本在终端中输出以下内容:
Successfully initialized pin 1.
Setting pin power output...
1
问题是每次调用 call()
函数时,您当前的工作目录都会重置为脚本所在的目录。
在您的 powerPin()
函数中,更新行:
call("echo "+str(power)+" > value && cat value",shell=True)
成为
call("cd /sys/class/gpio/gpio{0} && echo {1} > value".format(str(self.pin), str(power)))
以便在目录 /sys/class/gpio/gpio${x}
:: 中创建值文件,其中 ${x}
是引脚编号
我正在尝试控制单板计算机 (Vocore v2) 运行 Open-Wrt Linux 上的 GPIO 引脚。该脚本试图通过文件系统控制 GPIO 引脚。然而,虽然引脚完美导出,但引脚的值(或功率)从未设置!有谁知道为什么powerPin()
从来不设置GPIO管脚的power值?我该如何解决这个问题?
谢谢。
我的 Python 脚本:
'''
This script is an introductory attempt to create a global Python GPIO control program.
The pinAction class requires several variables to be set:
[pin number]
[direction (read or write)]
[output value (if write is set)]
[read value (if read is set; updates on interval flag)]
[read update interval (if read is set, this is an integer value indicating the frequency of which pin values are read)]
'''
#Import all modules necessary for the script...
from subprocess import call
import sys
class pinAction:
def __init__(self,pin,direction):
#Set object properties...
self.pin = int(pin)
#Test direction argument...
self.direction = "in" if direction == "in" else "out" if direction == "out" else False
if not self.direction:
#Incorrect argument: terminate the program...
print "Error: pin direction must be 'in' or 'out'. Terminating program..."
sys.exit()
else:
call('echo '+self.direction+' > direction',shell=True)
#Initialize the GPIO pin through the file system...
call("cd /sys/class/gpio && echo "+str(self.pin)+" > export",shell=True)
print "Successfully initialized pin "+str(self.pin)+"."
def powerPin(self, power):
if self.direction == "out":
print "Setting pin power output..."
power = 0 if int(power) == 0 else 1 if int(power) == 1 else False
if not power:
print "Error: power argument must be an integer or string of '0' or '1'."
else:
call("echo "+str(power)+" > value && cat value",shell=True)
#call("ls && echo "+str(power)+" > value",shell=True)
else:
print "Error: pin is not an output pin!"
x = pinAction(1,"out")
#Set pin power to be on...
x.powerPin(1)
脚本在终端中输出以下内容:
Successfully initialized pin 1.
Setting pin power output...
1
问题是每次调用 call()
函数时,您当前的工作目录都会重置为脚本所在的目录。
在您的 powerPin()
函数中,更新行:
call("echo "+str(power)+" > value && cat value",shell=True)
成为
call("cd /sys/class/gpio/gpio{0} && echo {1} > value".format(str(self.pin), str(power)))
以便在目录 /sys/class/gpio/gpio${x}
:: 中创建值文件,其中 ${x}
是引脚编号