Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.naic.edu/~phil/hardware/puppi/if_amp_ctrl_cmd.py
Дата изменения: Tue Mar 6 17:25:30 2012
Дата индексирования: Mon Feb 4 12:10:02 2013
Кодировка:
#!/usr/bin/python
# Command line client for PUPPI IF Amp
# Examples:
# >> ATNA13, << atnok, 6.5dB atten
# >> ATN?, attenuator actual atten config
# >> ATNR, read EEPROM config
# >> ATNW, write actual config to EEPROM
# >> ATNM0131, set multiple atten, A to 0.5dB, B to 15.5dB
# S2E test: 192.168.100.226, port1/6001 (TTL serial)
# IP: ifamp.puppi.naic.edu, MAC: 00:1A:B6:01:37:BA
# NAIC AO, lquintero, 5 Mar 2012

import sys
from optparse import OptionParser
from time import sleep
import socket

# command line options
usage = "%prog [options]"
description = "Client terminal for PUPPI IF Amplifier"
parser = OptionParser(usage=usage, description=description)
parser.add_option( "-v", "--verbose",
action="store_true", dest="verbose", default=False,
help = "verbose mode")
parser.add_option( "-i", "--ip-address",
dest = "ip_addr",
default = "192.168.100.226",
help = "device ip address")
parser.add_option( "-p", "--port",
dest = "ip_port",
default = 6001,
help="device ip port")
parser.add_option( "-t", "--timeout",
dest = "timeout",
default = 5,
help="socket timeout")

# main program
def main():
BUFFER_SIZE = 1024
global opts
(opts, args) = parser.parse_args()
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
s.settimeout( int(opts.timeout) )
try:
s.connect( (opts.ip_addr, int( opts.ip_port ) ) )
if opts.verbose:
print "-- socket: opening socket %s/%d " %(opts.ip_addr, opts.ip_port)
except: # Time out
print "-- socket: no connection to %s/%d " %(opts.ip_addr, opts.ip_port)
sys.exit()
print "Type exit to quit."
# commands capture
try:
while 1:
udc_command = raw_input('>> ')
if udc_command == 'exit':
print "-- UDC client exit.\n"
s.close()
sys.exit()
# empty lines are not sent
if len(udc_command)>0:
# send command
if opts.verbose:
print "-- socket: sending message: %s" %(udc_command)
try:
s.send( udc_command + '\r' )
except: # Time out
print "-- socket: no message sent."
sys.exit()

# receive message after some time...
sleep(1.0)
try:
udc_response = s.recv( BUFFER_SIZE ) # receive string
print "<< %s" % (udc_response)
#if opts.verbose: print " Message received: ", message,
except:
if opts.verbose:
print "-- socket: timeout after %d seconds" %(opts.timeout)
print "-- no response for: %s" %(udc_command)
#sys.exit()
except KeyboardInterrupt:
print "\n-- UDC client exit (keyboard interrupt).\n"

# stand alone program
if __name__ == "__main__":
main()