nxmodbus.client_rtu module

NextModbusRtu.__init__(serial_port, offset=0, debug=False)[source]

serial device must be configured with the configuration set with the Nx-Interface.

Moreover, a timeout of 1 second must be set.

Parameters:
  • serial_port – Instance of serial module

  • offset – The address offset as defined in the Next device

  • debug (boolean) – Activate debug traces for tx/rx frames

class nxmodbus.client_rtu.NextModbusRtu(serial_port, offset=0, debug=False)[source]

Bases: object

This class act as a Modbus master in order to communicate with the Next gateway (slave)

serial_port

Serial port used for communication with the gateway

Type:

serial.Serial

addresses

Instance of Addresses class grouping all device base addresses and all property modbus addresses

Type:

nxmodbus.addresses.Addresses

check_version()[source]

Read the NextGateway parameter which corresponds to the Object Model Version and compare it with the version of the generated file addresses.py

Note

A different major version means that the compatibility is broken and it is recommanded to update the addresses list. A different minor version means that small modifications have been made. Some addresses could be removed or added.

Returns:

True if the addresses class version is same as the nx-gateway (minor and major).

Return type:

bool

See also

None

read_parameter(slave_id, address, prop_type, string_size=0)[source]

Read a parameter from a targeted device according to the given property type.

Note

All parameters listed in the technical specification of the Next devices are accessible with the Modbus protocol.

Parameters:
  • slave_id (int) – Slave identifier number (targeted device)

  • address (int) – Register starting address, see “Technical specification - Next Modbus appendix” for the complete list of accessible register per device

  • prop_type (PropType) – Property type given by the enum found in proptypes.py

  • string_size (int) – When selecting String as prop_type, it is mandatory to give the string size.

Returns:

parameter read returned with the given property type.

Return type:

PropType

Example

# First check the version compatibility, then read the serial number, the modbus tcp port and earthing
# scheme relay status.
# Run this example within the 'examples/' folder using 'python ex_rtu_read_param.py' from a CLI after installing
# nxmodbus package with 'pip install nxmodbus'

import serial
import sys
import os

sys.path.append(os.path.abspath('..'))
from nxmodbus.client import NextModbus
from nxmodbus.proptypes import PropType

SERIAL_PORT_NAME = 'COM4'       # your serial port interface name
SERIAL_PORT_BAUDRATE = 9600     # baudrate used by your serial interface
ADDRESS_OFFSET = 0              # your modbus address offset as set inside the Next system
INSTANCE = 0                    # The instance of the requested device

if __name__ == "__main__":
    try:
        serial_port = serial.Serial(SERIAL_PORT_NAME, SERIAL_PORT_BAUDRATE, parity=serial.PARITY_EVEN, timeout=1)
    except serial.serialutil.SerialException as e:
        print("Check your serial configuration : ", e)
    else:
        nextModbus = NextModbus(serial_port, ADDRESS_OFFSET, debug=False)

        # check the version
        if not nextModbus.check_version():
            print("WARNING : The version is not correct")

        # Read the serial number
        read_value = nextModbus.read_parameter( nextModbus.addresses.device_address_nextgateway + INSTANCE,
                                                nextModbus.addresses.nextgateway_idcard_serialnumber,
                                                PropType.STRING,
                                                8)
        print('Serial number:', read_value)

        # Read the modbus TCP port used by the TCP modbus server
        read_value = nextModbus.read_parameter( nextModbus.addresses.device_address_nextgateway + INSTANCE,
                                                nextModbus.addresses.nextgateway_modbus_modbustcpport,
                                                PropType.UINT)
        print('Modbus TCP port:', read_value)

        # Read the Earthing relay status
        read_value = nextModbus.read_parameter( nextModbus.addresses.device_address_system,
                                                nextModbus.addresses.system_earthingscheme_relayisclosed,
                                                PropType.BOOL)
        print('Earthing scheme relay status:', read_value)
write_parameter(slave_id, address, value, prop_type)[source]

Write a parameter value into a targeted device.

Note

All parameters listed in the technical specification of the Next devices are accessible with the Modbus protocol.

Parameters:
  • slave_id (int) – Slave identifier number (targeted device)

  • address (int) – Register starting address, see “Technical specification - Next Modbus appendix” for the complete list of accessible register per device.

  • value – The value to write at the given address.

  • prop_type – Property type

Returns:

Quantity of written registers

Return type:

int

Example

# First check the version compatibility, then write the HMI display brightness, the GUI unlock code and
# the nominal frequency of the tri-phased inverters.
# Run this example within the 'examples/' folder using 'python ex_rtu_write_param.py' from a CLI after installing
# nxmodbus package with 'pip install nxmodbus'

import serial
import sys
import os

sys.path.append(os.path.abspath('..'))
from nxmodbus.client import NextModbus
from nxmodbus.proptypes import PropType

SERIAL_PORT_NAME = 'COM4'       # your serial port interface name
SERIAL_PORT_BAUDRATE = 9600     # baudrate used by your serial interface
ADDRESS_OFFSET = 0              # your modbus address offset as set inside the Next system
INSTANCE = 0                    # The instance of the requested device

if __name__ == "__main__":
    try:
        serial_port = serial.Serial(SERIAL_PORT_NAME, SERIAL_PORT_BAUDRATE, parity=serial.PARITY_EVEN, timeout=1)
    except serial.serialutil.SerialException as e:
        print("Check your serial configuration : ", e)
    else:
        nextModbus = NextModbus(serial_port, ADDRESS_OFFSET, debug=False)

        value = True  # Earthing scheme disable check at TRUE
        echo = nextModbus.write_parameter(  nextModbus.addresses.device_address_system + INSTANCE,
                                            nextModbus.addresses.system_earthingscheme_disablecheck,
                                            value,
                                            PropType.BOOL)
        assert echo == 1  # a value of 1 is expected on write action, represent the number of registers written
        print('Number of registers written:', echo)

        value = 20  # Brightness level at 10
        echo = nextModbus.write_parameter(  nextModbus.addresses.device_address_nextgateway + INSTANCE,
                                            nextModbus.addresses.nextgateway_hmidisplay_brightness,
                                            value,
                                            PropType.UINT)

        assert echo == 2  # a value of 2 is expected on write action, represent the number of registers written
        print('Number of registers written:', echo)

        value = "12345"  # Unlock code
        echo = nextModbus.write_parameter(  nextModbus.addresses.device_address_nextgateway + INSTANCE,
                                            nextModbus.addresses.nextgateway_hmidisplay_unlockcode,
                                            value,
                                            PropType.STRING)
        assert echo == 3  # a value of 3 is expected on write action, represent the number of registers written
        print('Number of registers written:', echo)

        value = 50.2  # Nominal frequency of the tri-phased inverters
        echo = nextModbus.write_parameter(  nextModbus.addresses.device_address_system,
                                            nextModbus.addresses.system_triphaseinverter_nominalfrequency,
                                            value,
                                            PropType.FLOAT)
        assert echo == 2  # a value of 3 is expected on write action, represent the number of registers written
        print('Number of registers written:', echo)