Skip to content

Instantly share code, notes, and snippets.

@neckro
Created February 11, 2016 00:06
Show Gist options
  • Save neckro/81b9ac0bbc62c68232c2 to your computer and use it in GitHub Desktop.
Save neckro/81b9ac0bbc62c68232c2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import serial # pyserial
import fileinput
serial_opts = {
# built-in serial port is "COM1"
# USB serial port is "COM4"
"port": "COM1",
"baudrate": 9600,
"parity": serial.PARITY_EVEN,
"bytesize": serial.SEVENBITS,
"xonxoff": False,
"timeout": None
}
"""
NOTES:
Screen width: 40
Aux -> Setup
Password: 15732
-> Link:
DNC Timeout (sec): 20
ASCII Rewind: 26
ASCII Dialog Start: 17
ASCII Dialog Done: 19
ASCII Dialog Prompt: 3
ASCII Dialog Acknowledge: 62
RS-232C baud rate: 9600
Character Code: ASCII
RS-244 character set == EIA == ???
Parity: Even
Transmit delay: 0
Scan for ctrl-Z in download: Off
Send EOT upon download fail: Off
Download mem capacity as %: 30
Autoreload point as %: 25
"""
XON = chr(17)
XOFF = chr(19)
DIALOG_START = XON
DIALOG_END = XOFF
DIALOG_SEND = "S"
DIALOG_RECV = "R"
DIALOG_PROMPT = chr(3)
DIALOG_ACK = chr(62)
FILE_START = chr(18) # DC2?
FILE_END = chr(26) # ctrl-z
LINE_FEED = chr(10)
CARRIAGE_RETURN = chr(13)
port = serial.Serial(**serial_opts)
def main():
print " Status: Waiting for host..."
await(DIALOG_START)
cmd = port.read(1)
if cmd == DIALOG_SEND:
port.write(LINE_FEED)
# send response?
print " Status: Got download request"
print " Status: Starting dialog mode"
# put any dialog prompts here
print " Status: Ending dialog mode, sending file"
port.write(DIALOG_END)
send_file()
elif cmd == DIALOG_RECV:
# receive command?
pass
def send_file():
# remote gobbles the first line... why?
port.write(" "+LINE_FEED)
await(DIALOG_ACK)
b = 2
for line in fileinput.input():
line = line.strip()
print " File:", line
port.write(line + LINE_FEED)
b += len(line) + 1
await(DIALOG_ACK)
print " Status: End of file, total", b, "bytes"
fileinput.close()
port.write(FILE_END)
def prompt(text):
print " Prompt:", text
port.write(text + " ")
port.write(DIALOG_PROMPT)
response = await(CARRIAGE_RETURN)
port.write(CARRIAGE_RETURN + LINE_FEED)
print "Response:", response
return response
def await(char):
line = ""
while True:
c = port.read(1)
if c == char:
return line
print " RECV: %" % c.encode("dec")
else:
print " RECV: % (unexpected)" % c.encode("dec")
line += c
if __name__ == "__main__":
main()
print "\nPress a key to close."
try:
raw_input()
except e:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment