Skip to content

Commit 9213794

Browse files
committed
Merge pull request #2 from McNeight/getch-cp
added cross-platform support for getch.
2 parents 6a4de87 + 17b4954 commit 9213794

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

TerminalPython.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,32 @@
22
##You can use this as a start to customize the interface for your needs.
33
##NOTE: Change Line 14 to have the proper COM port or it will not work!
44
##import serial lib
5-
import msvcrt
5+
##import msvcrt
66
import serial
77
import io
88
import time
99

10+
try:
11+
from msvcrt import getch
12+
except ImportError:
13+
''' we're not on Windows, so we try the Unix-like approach '''
14+
15+
def getch( ):
16+
import sys, tty, termios
17+
fd = sys.stdin.fileno( )
18+
old_settings = termios.tcgetattr(fd)
19+
try:
20+
tty.setraw(fd)
21+
ch = sys.stdin.read(1)
22+
finally:
23+
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
24+
return ch
25+
1026
##open serial port
1127
print ("Welcome to WDC's 65xx Serial Monitor in Python");
1228
print ("Press the reset button on your board to start reading serial data from the board");
1329
## For now the Serial Port Needs to be set manually. Change the line below to your COMXX port
14-
ser = serial.Serial("COM74", 9600,timeout=1)
30+
ser = serial.Serial("/dev/tty.usbserial-A703XC1I", 9600,timeout=1)
1531
ser.flushInput()
1632
ser.flushOutput()
1733
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))
@@ -30,7 +46,7 @@
3046
readser = 0
3147
print ("Enter a command. For a list of commands press h");
3248
print hello;
33-
writedata = msvcrt.getch()
49+
writedata = getch()
3450
ser.write(writedata)
3551
readser = 1
3652
elif "BB:AAAA" in hello:

0 commit comments

Comments
 (0)