Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 import argparse
00025 import multicast
00026 import serial
00027 import sys
00028 import time
00029
00030
00031 def get_option_parser():
00032 parser = argparse.ArgumentParser()
00033 parser.add_argument("--group",
00034 help="multicast IP", metavar="IP", default="224.1.1.1")
00035 parser.add_argument("--port", type=int,
00036 help="multicast port", metavar="PORT", default=20001)
00037 parser.add_argument("--device",
00038 help="multicast device", metavar="ETH", default="eth0")
00039 parser.add_argument("--serial-port",
00040 help="serial port", metavar="SER", default=None)
00041 parser.add_argument("--baud", type=int,
00042 help="serial baud rate", metavar="RATE", default=57600)
00043 parser.add_argument("--verbose", action="store_true",
00044 help="echo received messages to stdout")
00045 return parser
00046
00047
00048 def main():
00049 args, _ = get_option_parser().parse_known_args()
00050
00051
00052
00053 while True:
00054 try:
00055 receiver = multicast.MulticastUDPReceiver(args.device, args.group, args.port)
00056 print "Created multicast receiver on %s:%s, device %s." % (args.group, args.port, args.device)
00057 break
00058 except multicast.Receiver.InterfaceNotFound:
00059 print "Unable to find network interface %s, retrying." % args.device
00060 time.sleep(1.0)
00061
00062 ser = None
00063 if args.serial_port:
00064 print "Will transmit to %s at %d baud." % (args.serial_port, args.baud)
00065 else:
00066 print "No serial port set, listening only."
00067
00068 try:
00069 while True:
00070 s = receiver.read(10240)
00071 if args.serial_port and not ser:
00072 try:
00073 ser = serial.Serial(port=args.serial_port, baudrate=args.baud, timeout=0)
00074 print "Opened serial port."
00075 except Exception as e:
00076 ser = None
00077 print "Error opening serial port: %s" % str(e)
00078 if ser:
00079 ser.write(s)
00080 ser.flush()
00081 if args.verbose:
00082 sys.stdout.write(str(s).encode("string_escape"))
00083 sys.stdout.flush()
00084
00085 except:
00086 if ser:
00087 ser.close()
00088 print "Closed serial port."
00089 raise