Examples#

Basic Client Example#

 1""" A basic client demonstrating how to use pysolarmanv5."""
 2from pysolarmanv5 import PySolarmanV5
 3
 4
 5def main():
 6    """Create new PySolarman instance, using IP address and S/N of data logger
 7
 8    Only IP address and S/N of data logger are mandatory parameters. If port,
 9    mb_slave_id, and verbose are omitted, they will default to 8899, 1 and 0
10    respectively.
11    """
12    modbus = PySolarmanV5(
13        "192.168.1.24", 123456789, port=8899, mb_slave_id=1, verbose=False
14    )
15
16    """Query six input registers, results as a list"""
17    print(modbus.read_input_registers(register_addr=33022, quantity=6))
18
19    """Query six holding registers, results as list"""
20    print(modbus.read_holding_registers(register_addr=43000, quantity=6))
21
22    """Query single input register, result as an int"""
23    print(modbus.read_input_register_formatted(register_addr=33035, quantity=1))
24
25    """Query single input register, apply scaling, result as a float"""
26    print(
27        modbus.read_input_register_formatted(register_addr=33035, quantity=1, scale=0.1)
28    )
29
30    """Query two input registers, shift first register up by 16 bits, result as a signed int, """
31    print(
32        modbus.read_input_register_formatted(register_addr=33079, quantity=2, signed=1)
33    )
34
35    """Query single holding register, apply bitmask and bitshift left (extract bit1 from register)"""
36    print(
37        modbus.read_holding_register_formatted(
38            register_addr=43110, quantity=1, bitmask=0x2, bitshift=1
39        )
40    )
41
42
43if __name__ == "__main__":
44    main()

Register Scan Example#

 1""" Scan Modbus registers to find valid registers"""
 2from pysolarmanv5 import PySolarmanV5, V5FrameError
 3import umodbus.exceptions
 4
 5
 6def main():
 7    modbus = PySolarmanV5(
 8        "192.168.1.24", 123456789, port=8899, mb_slave_id=1, verbose=False
 9    )
10
11    print("Scanning input registers")
12    for x in range(30000, 39999):
13        try:
14            val = modbus.read_input_registers(register_addr=x, quantity=1)[0]
15            print(f"Register: {x:05}\t\tValue: {val:05} ({val:#06x})")
16        except (V5FrameError, umodbus.exceptions.IllegalDataAddressError):
17            continue
18    print("Finished scanning input registers")
19
20    print("Scanning holding registers")
21    for x in range(40000, 49999):
22        try:
23            val = modbus.read_holding_registers(register_addr=x, quantity=1)[0]
24            print(f"Register: {x:05}\t\tValue: {val:05} ({val:#06x})")
25        except (V5FrameError, umodbus.exceptions.IllegalDataAddressError):
26            continue
27    print("Finished scanning holding registers")
28
29
30if __name__ == "__main__":
31    main()