modbus_tcp_server v1.0.2 ModbusDatabase

This module implements the Modbus Register Database.

Multiple modules should be used for each application, depending on the type of data the Modbus Server implementation supports.

The public interface for this module includes the ModbusDatabase.start_link/2 used to start the process, ModbusDatabase.get/2 used to retrieve values from the datbase, ModbusDatabase.set/2 used to set the value of one or more registers in the database, and ModbusDatabase.set_mask/2 used to update the value of a register based on an ‘AND’ mask and an ‘OR’ mask.

Examples

The follwing examples show how to create databases for Coils, Discrete Inputs, Holding Registers, and Input Registers.

iex> coil_database = %{0 => false, 1 => false, 2 => true, 4 => false}
%{0 => false, 1 => false, 2 => true, 4 => false}
iex> discrete_input_database = %{0 => false, 1 => false, 2 => true, 4 => false}
%{0 => false, 1 => false, 2 => true, 4 => false}
iex> holding_register_database = %{0 => 0, 1 => 32768, 2 => -100, 4 => 95}
%{0 => 0, 1 => 32768, 2 => -100, 4 => 95}
iex> input_register_database = %{0 => 0, 1 => 32768, 2 => -100, 4 => 95}
%{0 => 0, 1 => 32768, 2 => -100, 4 => 95}
iex>ModbusDatabase.start_link(
...> {:analog, holding_register_database},
...> :modbus_holding_register_database
...>)
iex>ModbusDatabase.start_link(
...> {:analog, input_register_database},
...> :modbus_input_register_database
...>)
iex>ModbusDatabase.start_link(
...> {:binary, discrete_input_database},
...> :modbus_discrete_input_database
...>)
iex>ModbusDatabase.start_link(
...> {:binary, coil_database},
...> :modbus_coil_database
...>)
iex> GenServer.stop(:modbus_holding_register_database)
:ok
iex> GenServer.stop(:modbus_input_register_database)
:ok
iex> GenServer.stop(:modbus_coil_database)
:ok
iex> GenServer.stop(:modbus_discrete_input_database)
:ok

Summary

Functions

The get function is used to retrieve a value from the database

This set function is used by the client to set new values in multiple addresses in the database

The set_mask function is used by the client to set new values in in a single addresses in the database

This function starts the Modbus Database process

Functions

get(pid, arg)

The get function is used to retrieve a value from the database.

The pid parameter is the pid of the requesting process.

The starting_address parameter is the first address to be read from the database.

The count parameter specifies the number of values to read from the database.

If the read is successful, the response will be in the form of a tuple where the first element is the atom :response. The remainder of the response varies depending on the function code in the request.

If the read is unsuccessful, the response will be in the form of a tuple where the first element is the atom :exception_response. The second element will be one of the Modbus exception atoms

  • :illegal_data_address
  • :illegal_data_value
set(pid, arg)

This set function is used by the client to set new values in multiple addresses in the database.

The pid parameter is the pid of the requesting process.

The starting_address parameter is the register address to be written to.

The count parameter is the number of registers that new values will be written to.

The values parameter contains the values to be written into the registers.

set_mask(pid, arg)

The set_mask function is used by the client to set new values in in a single addresses in the database.

The pid parameter is the pid of the requesting process.

The register parameter is the register address to be written to.

The and_mask parameter contains the ‘AND’ mask applied to the current value.

The or_mask parameter contains the ‘OR’ mask applied to the current value.

start_link(database, name)

This function starts the Modbus Database process.

The type parameter should be either the atom :binary or the atom :analog.

The database parameter is a map containing the register numbers as keys and the initial register values as the map values.

The name must be one of the 4 types of modbus database regions.

  • :modbus_coil_database
  • :modbus_discrete_input_database
  • :modbus_holding_register_database
  • :modbus_input_register_database