From www.AA6E.net
Jump to: navigation, search
#!/usr/bin/python
#
# Orion serial command tester
# Copyright (c) 2004 Martin S. Ewing, AA6E
# Requires pyserial package (pyserial.sourceforge.net)
# Developed under Python 2.2.3 and Fedora Linux FC1
#
import serial,sys,time
#
# Serial port for Orion
#
DEVICE="/dev/ham.orion"
#
IDENT="OT.py 12/2004 AA6E"
#
#
# Global variables
#
ser = 0		# serial port object
#
ATTN= { '0':' off', '1':' 6dB', '2':'12dB', '3':'18dB' }
AGC = { 'O':' off', 'F':'fast', 'M':' med', 'S':'slow', 'P':'prog' }
STEP= { '1':'   1', '10':'  10', '100':' 100', '1000':'  1k', \
        '5000':'  5k', '10000':' 10k', '100000':'100k' }
ONOFF= { '0':'off', '1':' on' }
MODE= { '0':'USB', '1':'LSB', '2':'UCW', '3':'LCW', '4':' AM', '5':' FM', \
        '6':'FSK' }
#
#
# 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
   wrt(ss)
   t = rd()			# Do we get a response?
   if len(t) > 0: print "resp=",t
   return None
#
#
#  Main Entry
#
print IDENT
#
# Open the Orion's channel
#
ser = serial.Serial(DEVICE,baudrate=57600,rtscts=1,timeout=0.2)
ser.flushInput()
ser.flushOutput()
#
# Wake up the Orion
#
wrt("XX")               # Orion ID = syncs the serial port
msg1=rd()
wrt("?V")               # Firmware version number
msg2=rd()
print "Initialization:",msg1,msg2
#
while True :
  print "Enter a command string:";
  x=sys.stdin.readline();
  x = x.rstrip()
  print "trying", x
  oput(x)
#
ser.flushInput()
ser.close()