def serial_scan_primary(): parser = argparse.ArgumentParser( description='Scan serial M-Bus for devices.') parser.add_argument('-d', action='store_true', help='Enable verbose debug') parser.add_argument('-b', '--baudrate', type=int, default=2400, help='Serial bus baudrate') parser.add_argument('-r', '--retries', type=int, default=5, help='Number of ping retries for each address') parser.add_argument('--echofix', action='store_true', help='Read and ignore echoed data from target') parser.add_argument('device', type=str, help='Serial device or URI') args = parser.parse_args() meterbus.debug(args.d) try: with serial.serial_for_url(args.device, args.baudrate, 8, 'E', 1, timeout=1) as ser: for address in range(0, meterbus.MAX_PRIMARY_SLAVES + 1): if ping_address(ser, address, args.retries, args.echofix): print( "Found a M-Bus device at address {0}".format(address) ) except serial.serialutil.SerialException as e: print(e)
def serial_request_single(): parser = argparse.ArgumentParser( description='Request data over serial M-Bus for devices.') parser.add_argument('-d', action='store_true', help='Enable verbose debug') parser.add_argument('-b', '--baudrate', type=int, default=2400, help='Serial bus baudrate') parser.add_argument('-a', '--address', type=str, default=meterbus.ADDRESS_BROADCAST_REPLY, help='Primary or secondary address') parser.add_argument('-r', '--retries', type=int, default=5, help='Number of ping retries for each address') parser.add_argument('-o', '--output', default="dump", help='Output format') parser.add_argument('--echofix', action='store_true', help='Read and ignore echoed data from target') parser.add_argument('device', type=str, help='Serial device, URI or binary file') args = parser.parse_args() meterbus.debug(args.d) frame = None try: mode = os.stat(args.device).st_mode if stat.S_ISREG(mode): with open(args.device, 'rb') as f: frame = meterbus.load(f.read()) else: frame = do_char_dev(args) except OSError: frame = do_char_dev(args) if frame is not None: print(serialize_frame(frame, args.output))
def serial_scan_secondary(): def sec_addr_valid(string): if False is meterbus.is_secondary_address(string): raise argparse.ArgumentTypeError( "Not a valid secondary address mask") return string parser = argparse.ArgumentParser( description='Scan serial M-Bus for devices.') parser.add_argument('-d', action='store_true', help='Enable verbose debug') parser.add_argument('-b', '--baudrate', type=int, default=2400, help='Serial bus baudrate') parser.add_argument('-a', '--address', type=sec_addr_valid, default="FFFFFFFFFFFFFFFF", help='Secondary address mask') parser.add_argument('-r', '--retries', type=int, default=5, help='Number of ping retries for each address') parser.add_argument('--echofix', action='store_true', help='Read and ignore echoed data from target') parser.add_argument('device', type=str, help='Serial device or URI') args = parser.parse_args() meterbus.debug(args.d) try: with serial.serial_for_url(args.device, args.baudrate, 8, 'E', 1, timeout=1) as ser: # Ensure we are at the beginning of the records init_slaves(ser, args.echofix) mbus_scan_secondary_address_range(ser, 0, args.address, args.echofix) except serial.serialutil.SerialException as e: print(e)
help='Serial bus baudrate') parser.add_argument('-d', action='store_true', help='Enable verbose debug') parser.add_argument('-n', '--newAddress', type=int, help='New Primary address') parser.add_argument('-r', '--retries', type=int, default=5, help='Number of ping retries for each address') parser.add_argument('device', type=str, help='Serial device or URI') args = parser.parse_args() meterbus.debug(args.d) try: with serial.serial_for_url(args.device, args.baudrate, 8, 'E', 1, timeout=1) as ser: if args.modern: if setG4modern(args.address): print( "Set M-Bus device at address {0} to G4 compatible modern output" .format(args.address)) if args.fast: if setG4fast(args.address):
def test_debug_set_true(self): meterbus.debug(True) self.assertEqual(meterbus.g.debug, True)
help='Enable verbose debug') parser.add_argument('-b', '--baudrate', type=int, default=2400, help='Serial bus baudrate') parser.add_argument('-a', '--address', type=str, default=meterbus.ADDRESS_BROADCAST_REPLY, help='Primary or secondary address') parser.add_argument('-r', '--retries', type=int, default=5, help='Number of ping retries for each address') parser.add_argument('-o', '--output', default="json", help='Output format') parser.add_argument('device', type=str, help='Serial device or URI') args = parser.parse_args() meterbus.debug(args.d) address = args.address try: if 0 <= int(args.address) <= 254: address = int(args.address) except ValueError: pass try: ibt = meterbus.inter_byte_timeout(args.baudrate) with serial.serial_for_url(args.device, args.baudrate, 8, 'E', 1, inter_byte_timeout=ibt, timeout=1) as ser: frame = None
def serial_request_multi(): parser = argparse.ArgumentParser( description='Request data over serial M-Bus for devices.') parser.add_argument('-d', action='store_true', help='Enable verbose debug') parser.add_argument('-b', '--baudrate', type=int, default=2400, help='Serial bus baudrate') parser.add_argument('-a', '--address', type=str, default=meterbus.ADDRESS_BROADCAST_REPLY, help='Primary or secondary address') parser.add_argument('-r', '--retries', type=int, default=5, help='Number of ping retries for each address') parser.add_argument('-o', '--output', default="json", help='Output format') parser.add_argument('--echofix', action='store_true', help='Read and ignore echoed data from target') parser.add_argument('device', type=str, help='Serial device or URI') args = parser.parse_args() meterbus.debug(args.d) address = args.address try: if 0 <= int(args.address) <= 254: address = int(args.address) except ValueError: pass try: ibt = meterbus.inter_byte_timeout(args.baudrate) with serial.serial_for_url(args.device, args.baudrate, 8, 'E', 1, inter_byte_timeout=ibt, timeout=1) as ser: frame = None if meterbus.is_primary_address(address): if False is ping_address(ser, address, args.retries): sys.exit(1) meterbus.send_request_frame_multi(ser, address, read_echo=args.echofix) frame = meterbus.load( meterbus.recv_frame(ser)) elif meterbus.is_secondary_address(address): if False is ping_address(ser, meterbus.ADDRESS_NETWORK_LAYER, args.retries, args.echofix): ping_address(ser, meterbus.ADDRESS_BROADCAST_NOREPLY, args.retries, args.echofix) meterbus.send_select_frame(ser, address, read_echo=args.echofix) frame = meterbus.load(meterbus.recv_frame(ser, 1)) # Ensure that the select frame request was handled by the slave assert isinstance(frame, meterbus.TelegramACK) req = meterbus.send_request_frame_multi( ser, meterbus.ADDRESS_NETWORK_LAYER, read_echo=args.echofix) try: frame = meterbus.load(meterbus.recv_frame(ser)) except meterbus.MBusFrameDecodeError: frame = None while frame and frame.more_records_follow: # toogle FCB on and off req.header.cField.parts[0] ^= meterbus.CONTROL_MASK_FCB req = meterbus.send_request_frame_multi( ser, meterbus.ADDRESS_NETWORK_LAYER, req, read_echo=args.echofix) next_frame = meterbus.load(meterbus.recv_frame(ser)) frame += next_frame if frame is not None: print(serialize_frame(frame, args.output)) except serial.serialutil.SerialException as e: print(e)