nxmodbus.client_tcp module

NextModbusTcp.__init__(server_host, server_port=502, 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:
  • server_host – Hostname of the TCP server

  • server_port – Port number of the TCP server (default modbus TCP port set as 502)

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

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

class nxmodbus.client_tcp.NextModbusTcp(server_host, server_port=502, offset=0, debug=False)[source]

Bases: object

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

client

Modbus TCP client used for the communication with the gateway.

Type:

ModbusClient

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_tcp_read_param.py' from a CLI after installing
# nxmodbus package with 'pip install nxmodbus'

import sys
import os

sys.path.append(os.path.abspath('..'))
from nxmodbus.client_tcp import NextModbusTcp
from nxmodbus.proptypes import PropType

ADDRESS_OFFSET = 0                                      # the modbus address offset as set inside the Next system
INSTANCE = 0                                            # The instance of the requested device
SERVER_HOST = "192.168.0.100"                           # ipv4 address of nx-interface
SERVER_PORT = 502                                       # listening port of nx-interface

if __name__ == "__main__":

    nextModbus = NextModbusTcp(SERVER_HOST, SERVER_PORT, ADDRESS_OFFSET, 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:

True if write successful else None

Return type:

bool

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_tcp_write_param.py' from a CLI after installing
# nxmodbus package with 'pip install nxmodbus'

import sys
import os

sys.path.append(os.path.abspath('..'))
from nxmodbus.client_tcp import NextModbusTcp
from nxmodbus.proptypes import PropType

ADDRESS_OFFSET = 0                                      # the modbus address offset as set inside the Next system
INSTANCE = 0                                            # The instance of the requested device
SERVER_HOST = "192.168.0.100"                           # ipv4 address of nx-interface
SERVER_PORT = 502                                       # listening port of nx-interface

if __name__ == "__main__":

    nextModbus = NextModbusTcp(SERVER_HOST, SERVER_PORT, ADDRESS_OFFSET, debug=False)

    value = True  # Earthing scheme disable check at TRUE
    ok = nextModbus.write_parameter(nextModbus.addresses.device_address_system + INSTANCE,
                                    nextModbus.addresses.system_earthingscheme_disablecheck,
                                    value,
                                    PropType.BOOL)
    if ok:
        print('Disable check written successfully')
    else:
        print('Error when writing disable check')

    value = 10  # Brightness level at 10
    ok = nextModbus.write_parameter(nextModbus.addresses.device_address_nextgateway + INSTANCE,
                                    nextModbus.addresses.nextgateway_hmidisplay_brightness,
                                    value,
                                    PropType.UINT)
    if ok:
        print('Brightness written successfully')
    else:
        print('Error when writing brightness')

    value = "12345"  # Unlock code
    ok = nextModbus.write_parameter( nextModbus.addresses.device_address_nextgateway + INSTANCE,
                                    nextModbus.addresses.nextgateway_hmidisplay_unlockcode,
                                    value,
                                    PropType.STRING)
    if ok:
        print('Unlock code written successfully')
    else:
        print('Error when writing unlock code')

    value = 50.2  # Nominal frequency of the tri-phased inverters
    ok = nextModbus.write_parameter(nextModbus.addresses.device_address_system,
                                    nextModbus.addresses.system_triphaseinverter_nominalfrequency,
                                    value,
                                    PropType.FLOAT)
    if ok:
        print('Nominal frequency written successfully')
    else:
        print('Error when writing nominal frequency')