SuperDave156
Posts: 19
Joined: Sat Jan 05, 2013 12:56 pm

Pi3 fails to read keyboard input but Pi2 does!

Sun Jul 17, 2016 12:31 am

Hi all,

I have a python program that runs on startup from etc/rc.local
It reads keyboard input for 5 seconds to allow me to abort before the main program starts.
This has worked perfectly on all Pi versions up to Pi3.
When run on the Pi3 it crashes with
oldterm = termios.tcgetattr(fd)
termios error: (25, 'inappropriate ioctl for device')

If I use the Pi3 card in a Pi2 it also crashes with the same error.
If I use a Pi2 card in a Pi2 (or earlier) it works fine.

I am running an os generated by Noobs on a card purchased last week and
have just run apt-get update and apt-get upgrade on it.

Is there a fix for this or could someone on the development team take a look please?
Thanks,
David Taylor


This is my startup program:

Code: Select all

#!/usr/bin/python
#
# startup.py
# Pi3 fails to read keyboard input but Pi2 does!
# run this program from etc/rc.local on the Pi3 version of the os and it will fail
# run this program from etc/rc.local on the Pi2 version of the os and it will work

import os, sys, time
import termios, fcntl


def GetKey():
	fd = sys.stdin.fileno()
	oldterm = termios.tcgetattr(fd)
	newattr = termios.tcgetattr(fd)
	newattr[3] = newattr[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
	newattr[6][TERMIOS.VMIN] = 1
	newattr[6][TERMIOS.VTIME] = 0
	termios.tcsetattr(fd, termios.TCSANOW, newattr)
	oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
	fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
	try:
		try:
			c = sys.stdin.read(1)
#			print "Got character", repr(c)
			return c
		except IOError: pass
	finally:
		termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
		fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)


if __name__ == "__main__":

	# give us a chance to quit before launching
	print '###  Press q to prevent launch or any other key to launch  ###'
	bLaunch = True 
	TERMIOS = termios
	i = 0
	z = 5
	while i < z:
		print z - i,	
		i = i + 1
		ch = GetKey()
		if ch != None:
			if ch == 'q':
				print
				print 'Launch has been stopped by pressing ' + ch
				bLaunch = False
			break
		time.sleep(1)
			
	if bLaunch == True:
		print
		print "Launch the main loop here"

exit(0)
Last edited by SuperDave156 on Sun Jul 17, 2016 6:53 pm, edited 1 time in total.

User avatar
davidcoton
Posts: 5026
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: Pi3 fails to read keyboard input but Pi2 does!

Sun Jul 17, 2016 12:56 pm

Please edit your post to put the program in code tags so that the indentation is preserved. This is essential if you want help with Python code as the indentation carries meaning.
Signature retired

SuperDave156
Posts: 19
Joined: Sat Jan 05, 2013 12:56 pm

Re: Pi3 fails to read keyboard input but Pi2 does!

Sun Jul 17, 2016 6:52 pm

Thanks for pointing that out.
The code is not the issue. It works perfectly on a Pi2.
There seems to have been a change between the Pi2 and Pi3 within the operating system that has caused this problem. I am hoping that someone will be able to shed some light on it. Anyone who reads the keyboard from within a python program running from startup is stuck at Pi2 for the time being.

Return to “Troubleshooting”