def setup_readers(args): """Setup reader.""" global magnumReader, midniteReader if not args.ignoremagnum: magnumReader = magnum.Magnum(device=args.device, packets=args.packets, timeout=args.timeout, cleanpackets=args.cleanpackets) if not args.ignoreclassic: midniteReader = Midnite.Midnite(host=args.classichost, port=args.classicport, unit=args.classicunit, timeout=args.timeout)
"--timeout", default=0.005, type=float, help="Timeout for serial read (default: %(default)s)") seldom_used.add_argument( "-l", "--listen", default="ALL", help="IP address on which the server listens (default: %(default)s)") seldom_used.add_argument( "-nc", "--nocleanup", action="store_false", help="Suppress clean up of unknown packets (default: %(default)s)", dest='cleanpackets') args = parser.parse_args() if args.listen.upper() == 'ALL': args.listen = '' print("Options:{}".format( str(args).replace("Namespace(", "").replace(")", ""))) try: magnumReader = magnum.Magnum(device=args.device, packets=args.packets, timeout=args.timeout, cleanpackets=args.cleanpackets) except Exception as e: print("Error detected attempting to connect to Magnum network") traceback.print_exc() exit(2) run(addr=args.listen, port=args.port)
# run the program with --help for details of options. import argparse import json import time from magnum import magnum parser = argparse.ArgumentParser(description="Magnum Data Extractor Example") parser.add_argument("-d", "--device", default="/dev/ttyUSB0", help="Serial device name (default: %(default)s)") parser.add_argument( "-i", "--interval", default=60, type=int, dest='interval', help="Interval, in seconds between logging (default: %(default)s)") args = parser.parse_args() reader = magnum.Magnum(device=args.device) while True: start = time.time() devices = reader.getDevices() print(json.dumps(devices, indent=2)) duration = time.time() - start delay = args.interval - duration if delay > 0: time.sleep(delay)
def main(): global args parser = argparse.ArgumentParser( description="Magnum Data Dump", epilog="Refer to https://github.com/CharlesGodwin/pymagnum for details" ) parser.add_argument("-d", "--device", default="/dev/ttyUSB0", help="Serial device name (default: %(default)s)") parser.add_argument( "-i", "--interval", default=0, type=int, dest='interval', help= "Interval, in seconds, between dump records, in seconds. 0 means once and exit. (default: %(default)s)" ) parser.add_argument( '-v', "--verbose", action="store_true", default=False, help="Display options at runtime (default: %(default)s)") seldom = parser.add_argument_group("Seldom used") seldom.add_argument( "--packets", default=50, type=int, help="Number of packets to generate in reader (default: %(default)s)") seldom.add_argument("--timeout", default=0.005, type=float, help="Timeout for serial read (default: %(default)s)") seldom.add_argument( "--trace", action="store_true", default=False, help="Add most recent raw packet info to data (default: %(default)s)") seldom.add_argument( "-nc", "--nocleanup", action="store_false", help="Suppress clean up of unknown packets (default: %(default)s)", dest='cleanpackets') args = parser.parse_args() if args.verbose: print('Version:{0}'.format(__version__)) print("Options:{}".format( str(args).replace("Namespace(", "").replace(")", ""))) magnumReader = magnum.Magnum(device=args.device, packets=args.packets, timeout=args.timeout, cleanpackets=args.cleanpackets) if args.interval != 0 and args.verbose == True: print("Dumping every:{1} seconds. Using: {0} ".format( args.device, args.interval)) while True: start = time.time() devices = magnumReader.getDevices() if len(devices) != 0: try: alldata = OrderedDict() alldata["datetime"] = datetime.now( get_localzone()).replace(microsecond=0).isoformat() alldata["device"] = 'MAGNUM' magnumdata = [] for device in devices: data = OrderedDict() data["device"] = device["device"] data["data"] = device["data"] magnumdata.append(data) alldata["data"] = magnumdata print( json.dumps(alldata, indent=None, ensure_ascii=True, allow_nan=True, separators=(',', ':'))) except: traceback.print_exc() if args.interval == 0: break interval = time.time() - start sleep = args.interval - interval if sleep > 0: time.sleep(sleep)
def main(): global args parser = argparse.ArgumentParser( description="Magnum RS485 Reader Test", fromfile_prefix_chars='@', prog="Magnum Test", epilog="Use `python -m serial.tools.list_ports` to identify devices.") parser.add_argument("-d", "--device", default="/dev/ttyUSB0", help="Serial device name (default: %(default)s)") group = parser.add_argument_group("seldom used arguments") group.add_argument( "-n", "--packets", default=50, type=int, help="Number of packets to generate (default: %(default)s)") group.add_argument( "-t", "--timeout", default=0.001, type=float, help= "Timeout for serial read - float between 0 and 1 second (default: %(default)s)" ) group.add_argument( "-nc", "--nocleanup", action="store_true", help="Suppress clean up of unknown packets (default: %(default)s)", dest='cleanpackets') args = parser.parse_args() if args.timeout < 0 or args.timeout > 1.0: parser.error( "argument -t/--timeout: must be a number (float) between 0 and 1 second. i.e. 0.005" ) if args.packets < 1: parser.error("argument -n/--packets: must be greater than 0.") args.cleanpackets = not args.cleanpackets print('Version:{0}'.format(__version__)) print("Options:{}".format( str(args).replace("Namespace(", "").replace(")", ""))) try: reader = magnum.Magnum(device=args.device, packets=args.packets, timeout=args.timeout, cleanpackets=args.cleanpackets) except: print("Error detected attempting to open serial device") traceback.print_exc() exit(2) try: start = time.time() packets = reader.getPackets() duration = time.time() - start unknown = 0 formatstring = "Length:{0:2} {1:10}=>{2}" for packet in packets: if packet[0] == magnum.UNKNOWN: unknown += 1 print( formatstring.format(len(packet[1]), packet[0], packet[1].hex().upper())) format1 = "Packets:{0} of {1} with {2} UNKNOWN, in {3:2.2f} seconds" format2 = "Packets:{0} in {3:2.2f} seconds" format = format1 if unknown > 0 else format2 print(format.format(len(packets), args.packets, unknown, duration)) except: print("Error detected attempting to read network data - test failed") traceback.print_exc() exit(2)