hagershorn
Posts: 25
Joined: Thu Oct 31, 2019 5:33 pm

EOFError:EOF when reading a line

Mon Nov 30, 2020 5:50 pm

I know that this has been raised before, but I didn't understand the responses.

The programme works in the terminal but when I try to run it automatically the boot won't load the programme past the second line in the main while loop. addstar = input() giving the EOFError:when reading a line

The input is from a usb NFC tag reader which acts like a keyboard. The programme depends on the tag value.

A 'words of one syllable advice would be so appreciated'

Code: Select all

!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
#_IP=$(hostname -I) || true
#if [ "$_IP" ]; then
#  printf "My IP address is %s\n" "$_IP"
#fi
python3 /home/pi/starbox/starbox.py &
exit 0

The main programme follows

starbox.py

Code: Select all

#A programme for giving a child merit stars
import os
import time
import board
import busio as io
from adafruit_motorkit import MotorKit
from adafruit_motor import stepper
from adafruit_servokit import ServoKit
from adafruit_ht16k33 import segments

i2c = io.I2C(board.SCL, board.SDA)

    
def openwindow():
    kit = ServoKit(channels=16)
    kit.continuous_servo[1].throttle = -1
    kit.servo[0].angle = 0
    kit.continuous_servo[1].throttle = 0
    
def closewindow():
    kit = ServoKit(channels=16)
    kit.continuous_servo[1].throttle = 1
    kit.servo[0].angle = 110
    kit.continuous_servo[1].throttle = 0

    
def paperscroll():
    motorkit = MotorKit()      
    for i in range(1000):
        motorkit.stepper1.onestep(style = stepper.SINGLE)
        time.sleep(0.001)
        motorkit.stepper1.release()
#    papercount = 0
    
def addstarfunction():
#   default to adding just one star
    addstar = "0007363189" 
#   get the existing no of stars and how many on the current row
    f1 = open("/home/pi/starbox/starcount.txt", "r")
    starcount = int(f1.read())
    f1.close()
    f2=open("/home/pi/starbox/linetotal.txt","r")
    linecount = int(f2.read())
    f2.close()
    print(linecount)
    print (starcount)

#   start and fill the display
    counter = segments.Seg14x4(i2c,address=0x74)
    counter.fill(0)
    counter.brightness = .1
    counter.print(str(starcount)
# main input loop    
    while addstar != "0006742580":# safety check  ...580 exits
        addstar = input()

        counter = segments.Seg14x4(i2c,address=0x74)
        counter.fill(0)
        counter.brightness = 1.0
        message = "ADD STARS UP TO A TOTAL OF 10 IN THE ROW AND PRESENT THE TAG AGAIN"
        count = 0
        while count < len(message):
            counter.print(message[count])
            count += 1
            time.sleep(0.2)
        counter.fill(0)
        counter.print(starcount)

        print("input 1")

        if addstar == "0007363189":#add one star to the total. The user should stick one star to the paper
            openwindow()
#wait until the star has been stuck on then present the tag again
            addstar = input()
            starcount +=1
            linecount +=1
#            closewindow(kit)#close the window
        elif addstar == "0006734896":#add two stars to the total. The user should stick two stars to the paper
            openwindow()
#wait until the star has been stuck on then present the tag again
            addstar = input()
            starcount +=2
            linecount +=2
        elif addstar == "0006633466":#add 3 stars to the total. The user should stick three stars to the paper
            openwindow()
#wait until the star has been stuck on then present the tag again
            addstar = input()
            print("input 2")
            starcount +=3
            linecount +=3
        elif addstar == "0006653112":  # If a star is taken away we don't take one off the papercount or it will scroll in the wrong place
            starcount -=1
        elif addstar == "0006742580":
            exit()

        if linecount ==10:
            linecount = 0
            paperscroll()
        if linecount > 10:
            linecount=0
            counter.fill(0)
            message = "WAIT UNTIL THE PAPER HAS STOPPED MOVING, ADD THE EXTRA STARS AND PRESENT THE TAG AGAIN"
            count = 0
            while count < len(message):
                counter.print(message[count])
                count += 1
                time.sleep(0.2)
            counter.fill(0)
            paperscroll()
            addstar = input() #let more stars be stuck on
            print("input3")
        closewindow()#close the window

#write the new values to files
        f1=open("/home/pi/starbox/starcount.txt","w")
        f1.write(str(starcount))
        f1.close()
        f2=open("/home/pi/starbox/linetotal.txt","w")
        f2.write(str(linecount))
        f2.close()
# start the counter display

        counter.brightness = 1.0
        counter.fill(0)
        message = "EDDIE YOU NOW HAVE " +str(starcount)+ " STARS"
        count = 0
        while count < len(message):
            counter.print(message[count])
            count += 1
            time.sleep(0.2)
        counter.fill(0)
        counter.brightness = .1
        counter.print(str(starcount))
        print(linecount)
        print (str(starcount))
    
    
addstarfunction()


LocksmithsofBristol
Posts: 2
Joined: Mon Nov 30, 2020 6:14 pm

Re: EOFError:EOF when reading a line

Mon Nov 30, 2020 6:28 pm

Same problem here. Someone please help.

hippy
Posts: 13434
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: EOFError:EOF when reading a line

Mon Nov 30, 2020 6:37 pm

hagershorn wrote:
Mon Nov 30, 2020 5:50 pm
The programme works in the terminal but when I try to run it automatically the boot won't load the programme past the second line in the main while loop. addstar = input() giving the EOFError:when reading a line
I would guess that's something to do with a program started at boot not reading its input from the same thing as it will when started from a terminal session.

How exactly are you starting your program automatically ?

hagershorn
Posts: 25
Joined: Thu Oct 31, 2019 5:33 pm

Re: EOFError:EOF when reading a line

Mon Nov 30, 2020 7:18 pm

Thanks for your response.

by putting this line into rc.local. See top post. Then booting up.

Code: Select all

python3 /home/pi/starbox/starbox.py &
J

hagershorn
Posts: 25
Joined: Thu Oct 31, 2019 5:33 pm

Re: EOFError:EOF when reading a line

Mon Nov 30, 2020 7:37 pm

The programme lights a led display with the total merit stars shown, and then opens a window for Mum to stick more stars in, then closes the window, totals the stars and tells the child how many stars he now has in the led display. It works from IDE and terminal and works up to the point of input(). on boot.
J

bls
Posts: 2873
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA

Re: EOFError:EOF when reading a line

Mon Nov 30, 2020 7:40 pm

Where do you expect the input() function to read from? The python built-in input() function reads from standard input, and whe started in rc.local standard input isn't really pointing at anything useful.
Pi tools:
Quickly and easily build customized exactly as-you-want SSDs/SD Cards: https://github.com/gitbls/sdm
Easily run and manage your network's DHCP/DNS servers on a Pi: https://github.com/gitbls/ndm
Easy and secure IPSEC/IKEV2 VPN installer/manager: https://github.com/gitbls/pistrong
Lightweight Virtual VNC Config: https://github.com/gitbls/RPiVNCHowTo

hagershorn
Posts: 25
Joined: Thu Oct 31, 2019 5:33 pm

Re: EOFError:EOF when reading a line

Mon Nov 30, 2020 7:46 pm

The input is from a USB NFC tag reader which acts like a keyboard, putting its value into the "addstar"variable
J

bls
Posts: 2873
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA

Re: EOFError:EOF when reading a line

Mon Nov 30, 2020 7:59 pm

hagershorn wrote:
Mon Nov 30, 2020 7:46 pm
The input is from a USB NFC tag reader which acts like a keyboard, putting its value into the "addstar"variable
J
And what have you done to tell the python input() function where to get that input from?
Pi tools:
Quickly and easily build customized exactly as-you-want SSDs/SD Cards: https://github.com/gitbls/sdm
Easily run and manage your network's DHCP/DNS servers on a Pi: https://github.com/gitbls/ndm
Easy and secure IPSEC/IKEV2 VPN installer/manager: https://github.com/gitbls/pistrong
Lightweight Virtual VNC Config: https://github.com/gitbls/RPiVNCHowTo

hagershorn
Posts: 25
Joined: Thu Oct 31, 2019 5:33 pm

Re: EOFError:EOF when reading a line

Mon Nov 30, 2020 8:08 pm

er...nothing. It just worked in the IDE and then again in the terminal, but when boot gets to it it stops loading it, posts the error and carries on booting.
J

hagershorn
Posts: 25
Joined: Thu Oct 31, 2019 5:33 pm

Re: EOFError:EOF when reading a line

Mon Nov 30, 2020 8:09 pm

er...nothing. It just worked in the IDE and then again in the terminal, but when boot gets to it it stops loading it, posts the error and carries on booting.
J

bls
Posts: 2873
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA

Re: EOFError:EOF when reading a line

Mon Nov 30, 2020 8:16 pm

hagershorn wrote:
Mon Nov 30, 2020 8:09 pm
er...nothing. It just worked in the IDE and then again in the terminal, but when boot gets to it it stops loading it, posts the error and carries on booting.
J
You need to sort out how, when you run it interactively or with the IDE, the input() function reads from the tag reader, and then add something to your rc.local to do the same thing.

You haven't given enough information for any further guidance, especially to someone (me!) who doesn't know anything about your USB NFC tag reader.
Pi tools:
Quickly and easily build customized exactly as-you-want SSDs/SD Cards: https://github.com/gitbls/sdm
Easily run and manage your network's DHCP/DNS servers on a Pi: https://github.com/gitbls/ndm
Easy and secure IPSEC/IKEV2 VPN installer/manager: https://github.com/gitbls/pistrong
Lightweight Virtual VNC Config: https://github.com/gitbls/RPiVNCHowTo

hagershorn
Posts: 25
Joined: Thu Oct 31, 2019 5:33 pm

Re: EOFError:EOF when reading a line

Mon Nov 30, 2020 8:28 pm

Thank you bis. The tag reader merely puts a string into the input exactly the same way a keyboard does The string is 8 characters, all numbers but treated as characters.
I don't know how else I can manipulate input()
J

bls
Posts: 2873
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA

Re: EOFError:EOF when reading a line

Mon Nov 30, 2020 10:09 pm

So, when you run it interactively you don't have to do ANYTHING to make it work correctly? Seems strange, because the input() function reads from the terminal. If you can explain how it works interactively then we should be able to help. Without that, I'm out of ideas.
Pi tools:
Quickly and easily build customized exactly as-you-want SSDs/SD Cards: https://github.com/gitbls/sdm
Easily run and manage your network's DHCP/DNS servers on a Pi: https://github.com/gitbls/ndm
Easy and secure IPSEC/IKEV2 VPN installer/manager: https://github.com/gitbls/pistrong
Lightweight Virtual VNC Config: https://github.com/gitbls/RPiVNCHowTo

hagershorn
Posts: 25
Joined: Thu Oct 31, 2019 5:33 pm

Re: EOFError:EOF when reading a line

Mon Nov 30, 2020 11:00 pm

I really can't tell you any more. When I run the programme interactively in terminal, terminal just sits there waiting for the input. When I present a tag to the reader, terminal continues with the programme, adds up the stars and puts the right number in the led display. It runs a servo to open the window and waits while the star is stuck on and then when the tag is re-presented the servo closes the window.and terminal goes back to waiting for the tag input again. Every 10 stars on a row causes a stepper to wind the paper down to expose another row for more stars. All this happens quite happily interactively, but not on boot up auto. It's beyond me. :?

User avatar
Paeryn
Posts: 3541
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: EOFError:EOF when reading a line

Tue Dec 01, 2020 1:06 am

As has been said previously, when running your program from rc.local your program doesn't have anything as standard input. How is the system supposed to know that your program wants to read the tag reader (which is acting as a keyboard)? Keyboard input only gets sent to the program with active focus which a background process never (normally) has. You wouldn't want multiple programs all seeing and acting on the same keypresses at once.

What you need to do is find a way to get the output stream of the tag reader sent directly to your program's input stream, or find some way of making your program have input focus (if using the desktop one way would be to use the desktop's autostart to open a terminal running your program and making sure that your program forcibly makes itself the foreground window to ensure it gets keyboard input, not very flexible though).
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

hagershorn
Posts: 25
Joined: Thu Oct 31, 2019 5:33 pm

Re: EOFError:EOF when reading a line

Tue Dec 01, 2020 9:35 am

Thank you everyone. You have all made it quite clear why it happens. I will just have to learn a bit more and work on it. I must try to find a way to run the programme after the boot process is finished.
Thanks again everyone.

J

twostage
Posts: 125
Joined: Sun May 07, 2017 6:31 pm
Location: Northumberland

Re: EOFError:EOF when reading a line

Wed Dec 02, 2020 10:30 am

I'd go for the desktop autostart as Paeryn suggested. Google raspberry pi autostart gui.

I think it will get focus automatically.

hagershorn
Posts: 25
Joined: Thu Oct 31, 2019 5:33 pm

Re: EOFError:EOF when reading a line

Wed Dec 02, 2020 3:54 pm

bis, thank you for your input. you said

Where do you expect the input() function to read from? The python built-in input() function reads from standard input, and whe started in rc.local standard input isn't really pointing at anything useful.

Perhaps you can also say how I make it point to something when it is booting up? My usb keyboard would be good.
J

User avatar
RaTTuS
Posts: 10828
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: EOFError:EOF when reading a line

Wed Dec 02, 2020 4:11 pm

I'd use a systemd .service things
like
viewtopic.php?f=29&t=7192&start=25#p898424
for input

or the autostart thingy if it is a x windows type thing

dont use rc.lcoal its not supposed to be abused like that ;)
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

hagershorn
Posts: 25
Joined: Thu Oct 31, 2019 5:33 pm

Re: EOFError:EOF when reading a line

Wed Dec 02, 2020 4:25 pm

Thank you RaTTuS but that is all above my head. J

bls
Posts: 2873
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA

Re: EOFError:EOF when reading a line

Wed Dec 02, 2020 6:33 pm

hagershorn wrote:
Wed Dec 02, 2020 3:54 pm
bis, thank you for your input. you said

Where do you expect the input() function to read from? The python built-in input() function reads from standard input, and whe started in rc.local standard input isn't really pointing at anything useful.

Perhaps you can also say how I make it point to something when it is booting up? My usb keyboard would be good.
J
It might be helpful if you can point us to the documentation for the device you're using, and where you got the details on reading from it using input().

Is that possible?
Pi tools:
Quickly and easily build customized exactly as-you-want SSDs/SD Cards: https://github.com/gitbls/sdm
Easily run and manage your network's DHCP/DNS servers on a Pi: https://github.com/gitbls/ndm
Easy and secure IPSEC/IKEV2 VPN installer/manager: https://github.com/gitbls/pistrong
Lightweight Virtual VNC Config: https://github.com/gitbls/RPiVNCHowTo

hagershorn
Posts: 25
Joined: Thu Oct 31, 2019 5:33 pm

Re: EOFError:EOF when reading a line

Sat Dec 05, 2020 3:45 pm

Hi.
There is no documentation with it (literally). When a tag is presented it send a string (str) to input() just as a keyboard does. It doesn't matter if it or the keyboard is plugged in. It still sends error eof

J

hagershorn
Posts: 25
Joined: Thu Oct 31, 2019 5:33 pm

Re: EOFError:EOF when reading a line

Sat Dec 05, 2020 3:47 pm

silly me! :oops: I hadn't imported sys. I have now and it works. The programme runs perfectly on boot up. :D :D

hagershorn
Posts: 25
Joined: Thu Oct 31, 2019 5:33 pm

Re: EOFError:EOF when reading a line

Sat Dec 05, 2020 3:58 pm

Bis,
The reader I used is this one." Neuftech ID Card Reader USB Contactless Card Reader Plug and Play "

I thank you for your responses. You are very patient with those of us who are still learning.

bls
Posts: 2873
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA

Re: EOFError:EOF when reading a line

Sat Dec 05, 2020 4:13 pm

hagershorn wrote:
Sat Dec 05, 2020 3:47 pm
silly me! :oops: I hadn't imported sys. I have now and it works. The programme runs perfectly on boot up. :D :D
Great that you got it sorted out!
Pi tools:
Quickly and easily build customized exactly as-you-want SSDs/SD Cards: https://github.com/gitbls/sdm
Easily run and manage your network's DHCP/DNS servers on a Pi: https://github.com/gitbls/ndm
Easy and secure IPSEC/IKEV2 VPN installer/manager: https://github.com/gitbls/pistrong
Lightweight Virtual VNC Config: https://github.com/gitbls/RPiVNCHowTo

Return to “Beginners”