【发布时间】:2015-06-09 00:08:19
【问题描述】:
程序将使用条形码扫描仪来验证优惠券的好坏,并根据优惠券是否有效闪烁红灯或绿灯。
我希望它的工作方式是打开 Pi,然后立即能够扫描优惠券。 Pi 所要做的就是在我打开 Pi 并保持打开状态后启动程序,仅此而已。我正在使用 crontab 启动以下程序:
#Adam Giancola
#June 5th 2015
#This program will scan a bar code and if it matches a good bar code will flash a light
#green or red depending on the validity of the coupon.
import sys, select, os
import time
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
greenLED = 16
redLED = 12
GPIO.setup(greenLED, GPIO.OUT)
GPIO.setup(redLED, GPIO.OUT)
GPIO.output(greenLED, GPIO.LOW)
GPIO.output(redLED, GPIO.LOW)
goodBarCode = "0827112134023"
try:
#Flash LED to test if script is running at RPi boot
GPIO.output(greenLED, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(greenLED, GPIO.LOW)
time.sleep(0.5)
while(1):
userBarCode = input("")
if userBarCode == goodBarCode:
GPIO.output(greenLED, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(greenLED, GPIO.LOW)
time.sleep(0.5)
else:
GPIO.output(redLED, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(redLED, GPIO.LOW)
time.sleep(0.5)
except:
GPIO.cleanup()
如果我使用 "sudo python3 ...blah blah blah" 通过终端运行此代码,它可以正常工作,但我已使用 crontab 在启动时启动程序,但我没有从 LED 获得任何反馈。我知道我的 crontab 配置正在运行,因为我已经同时运行了其他程序并且它们正在运行。为什么 LED 不响应?
【问题讨论】:
-
为什么不创建一个 init.d 脚本来启动进程而不是 crontab?不过,您将不得不使用 screen 之类的东西或分叉该过程。
-
我怀疑你错过了告诉它使用什么解释器的 hashbang。试试
#!/usr/bin/env python可能吗? -
Nathan 我把这个添加到 py 脚本?
-
将它添加到 /etc/rc.local 而不是 cron 怎么样?
-
dastaan 我这样做了,我得到了相同的结果,它开始了,但我的代码似乎有问题,不允许程序继续运行。
标签: python linux raspberry-pi