From www.AA6E.net
Jump to: navigation, search
#!/usr/bin/python
#
# Orion restore state from file.
# Copyright (c) 2005 Martin S. Ewing, AA6E
# Requires pyserial package (pyserial.sourceforge.net)
# Developed under Python 2.3.4 and Fedora Linux FC3
#
import serial,sys,time,os
#
# Serial port for Orion
#
#DEVICE="/dev/ttyS0"
DEVICE="/dev/ham.orion"
#
IDENT="orestore.py 04/2005 AA6E"
#
# Global variables
#
ser = 0		# serial port object
#
# Orion basic I/O
#
def rd():
  global ser
  MAX=50; c=''; ans=''; i=0
  while c <> '\r':
    c = ser.read(1)
    # Timeout happens when Orion gets funky.  Next time should be ok.
    if c == '': break           #timeout? try again?
    ans += c
    i += 1
    if i > MAX : raise MyExcept, "serial read too long"
  return ans[:-1]       # trim off final \r
#
def wrt(s):
    global ser
    if ser.inWaiting(): ser.flushInput() # Orion i/o has slipped
    ser.write(s+'\r')
    return None
#
def oget(ss):   		# Get data from Orion
   MAX=5; t = ''; i=0;
   for i in range(MAX):
     wrt(ss)
     t = rd()
     if len(t)<len(ss) : continue       # read string too short
     if t[1:len(ss)] == ss[1:] : break  # resp. looks good
   return t[len(ss):]                   # return data part only
#
def oget_spl(ss):   		# Get data from Orion (special for ?AU etc)
   MAX=5; t = ''; i=0;
   for i in range(MAX):
     wrt(ss)
     t = rd()
     if len(t)<len(ss) : continue       # read string too short
     if t[1:len(ss)] == ss[1:] : break  # resp. looks good
   return t[len(ss)-1:]                   # return data part only, but keep one more to left
#
def oput(ss):			# Send a command to Orion
#   print ss
#   for i in range(0,len(ss)):
#	print "%x " % (ord(ss[i])),
#   print
#   time.sleep(.05)
   wrt(ss)
   t = rd()			# Do we get a response?
   if len(t) > 0: print "resp=",t
   return None
#
#  Main Entry
#
# Usage:   orestore.py filename
#		If filename is null or "-", use stdin.
#
print IDENT
#
# Open the Orion's channel
#
ser = serial.Serial(DEVICE,baudrate=57600,rtscts=1,timeout=0.2)
ser.flushInput()
ser.flushOutput()
#
# Input file
#
f = 0
if len(sys.argv) > 1 : 
  if sys.argv[1] != "-" :
    name = sys.argv[1]
    print "Opening input file %s" % name
    f = open(name, mode='r')
if f == 0 :
  f = sys.stdin
#
# Wake up the Orion
#
time.sleep(0.5)		# Allow some settling time if needed.
wrt("XX")               # Orion ID = syncs the serial port
msg1=rd()
wrt("?V")               # Firmware version number
msg2=rd()
print "Orion ID & firmware:",msg1,msg2
#
#fdump(f)
#
lines = f.readlines(-1)
for x in lines :
  if x.startswith("#") :
    print x.rstrip("\n")
  else :
    oput(x.rstrip("\n"))
f.close()
ser.flushInput()
ser.close()