简体   繁体   English

Raspberry Pi无法在启动时运行脚本

[英]Raspberry Pi won't run script on boot

The program will use a barcode scanner to verify weather a coupon is good or not and will flash either a red or green light depending on if the coupon is valid. 该程序将使用条形码扫描仪来验证优惠券是否合格,并且将根据优惠券是否有效闪烁红色或绿色指示灯。

The way I want this to work is to turn on the Pi and then immediately be able to scan coupons. 我希望此方法的工作方式是打开Pi,然后立即能够扫描优惠券。 All the Pi has to do is begin the program once I turn on the Pi and remain on and that's all. 当我打开Pi并保持打开状态时,Pi所要做的就是开始程序。 I am using crontab to begin the following program: 我正在使用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()

This codes works fine if I run it through the terminal using "sudo python3 ...blah blah blah" , but I have used crontab to start the program at boot and I am getting no feedback from the LEDs. 如果我使用“ sudo python3 ... blah blah blah”通过终端运行此代码,则该代码运行良好,但是我已使用crontab在引导时启动程序,并且没有收到LED的反馈。 I know my crontab configuration is working because I have run other programs along side and they are working. 我知道我的crontab配置正在运行,因为我已经在旁边运行了其他程序并且它们正在运行。 Why won't the LEDs respond? LED为什么不响应?

The problem is that the barcode scanner is pretending to be a keyboard. 问题是条形码扫描仪冒充了键盘。 When you run it from the console, whatever the barcode scanner outputs will go to your program as it is the one currently reading on stdin for the console. 当您从控制台运行它时,无论条形码扫描器的输出是什么,它都会进入您的程序,因为它是当前在控制台的stdin上读取的内容。

When you run it in the background, through rc.local, cron, init script or whatever other method, your program will not be on the console, it will be in the background, so it will not "see" what the barcode scanner types into the keyboard. 当您通过rc.local,cron,init脚本或任何其他方法在后台运行该程序时,您的程序将不在控制台中,而是在后台,因此不会“看到”条形码扫描仪的类型进入键盘。

Probably the best short term solution for you is to enable auto-login on the console (the GUI has to be off) and call your program right after login. 最好的短期解决方案是在控制台上启用自动登录(必须关闭GUI)并在登录后立即调用程序。 Full explanation here , short version: 完整说明 ,简短版本:

Edit /etc/inittab and change the corresponding line to: 编辑/ etc / inittab并将相应的行更改为:

 1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1

Edit /home/pi/.bash_profile and add: 编辑/home/pi/.bash_profile并添加:

 sudo python3 bla bla bla

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM