File size: 2,297 Bytes
a2a15a2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
#!/usr/bin/python3
#
# $Id: monitor.py,v 1.7 2021/08/20 20:06:23 stefan Exp stefan $
#
# Arduino Monitor V0.1 for Stefan's tinybasic.
#
# This is a very basic serial monitor with a few special features.
# it connects to the Arduino serial port and interacts with
# Stefan's Tinybasic.
#
# Special commands
# <cntrl> L loads a program into the Arduino - literally typing it in
# <cntrl> S saves a program from the Arduino - collects the output of LIST
# to save type <cntrl S>, enter the file name and then type list
# end save mode with another <cntrl S>, then hand edit the output
# file to remove the "list" and "Ready"
# <cntrl> D ends the monitor
#
# Set the port here
#
#port = '/dev/cu.usbserial-1420'
port = '/dev/cu.wchusbserial1420'
#port = '/dev/cu.wchusbserial1410'
#port = '/dev/cu.usbserial-1410'
#port = '/dev/cu.usbmodem14201'
#port = '/dev/cu.usbmodem14101'
#port = '/dev/cu.usbserial-0001'
#port = '/dev/ttyUSB0'
#port = '/dev/cu.usbserial-A900f3Ty'
import sys
import serial
import threading
from time import sleep
import readchar
# timeout is needed to terminate properly
ser = serial.Serial(port, 9600, timeout = 0.3, xonxoff = True)
#
# The upload function
#
def sendline(l):
sleep(0.1)
for ch in l:
sleep(0.01)
chp = ch.encode('utf-8')
ser.write(chp)
cont = True;
saveflag = False;
def loadfile():
fn = input("Loading from file: ")
f = open(fn, "r")
for line in f:
sendline(line)
#
# The reader thread
#
def readfunction():
while cont:
ch = ser.read()
chp = ch.decode('utf-8')
if ( chp == "\n"):
sys.stdout.write("\r");
if ( chp == "\r"):
sys.stdout.write("\r");
sys.stdout.write("\n");
if ( chp == "\x7f" ):
sys.stdout.write("\b")
sys.stdout.write(chp)
sys.stdout.flush()
if (saveflag):
if (chp != "\r"):
f2.write(chp)
reader = threading.Thread(target=readfunction)
reader.start()
#
# The write loop
#
while cont:
ch = readchar.readchar()
chp = ch.encode('utf-8')
if (chp == b'\x04'):
cont = False
elif (chp == b'\x0c'):
loadfile()
elif (chp == b'\x13'):
if (not saveflag):
print("Save on")
fn = input("Save to file: ")
f2 = open(fn, "w")
saveflag = True
else:
saveflag = False
print("Save off")
f2.close()
else:
ser.write(chp)
reader.join()
|