12 - 11 - 2019

ModBus

Modbus:

Modbus is a very important Client/Server type protocol, wide used in industrial automation. The structure of the communication frame is:

  • TransactionID - 2 bytes, every sent packet is idetified with unical ID
  • ProtocolID - 2 bytes, it is usualy 0;
  • Length - 2 bytes, the length of the next bytes in the packet;
  • UnitID - 1 byte, every connected device to the ModBus server need to has an unical ID;
  • FC - 1 byte, the code of the ModBus command;
  • Data - number of bytes is different for every command;

ModBus function codes:

Every device Client or Server has 4 separated memories. Every memory starts from address Zero. Various ModBus commands can only access the memory for which they are intended.

List of the ModBus memories:

  • Coils
  • DiscreteInputs
  • HoldingRegisters
  • InputRegisters


Block ModBusClient

License: Industrial

All the block settings are in the Setings parameter. Its elements are as follows:

  • Communication - TCP/IP or ComPort communication can be selected. In case of TCP/IP, the settings are IP address and IP Port of the ModBus server, in case of ComPort communication - ComPort and a BaudRate. In TCP/IP communication DeviceID is the same for all ModBus commands. In ComPort communication, DeviceID can be set from the bottom grid and can be different for the different commands, because on a RS485 bus there may be a large number of devices with different DeviceIDs, which to be controled from one ModBusClient block.
  • Use zero based addressing - for some device manufactures, address 0001 is intepreted as address 0000. For example, for Advantech devices, the parameter has to be checked;
  • CRC - the checked parameter adds 2 bytes for CRC at the end of the communication frame;
  • Packets with header - for some devices, the communication header can be removed from the communication frame of the packet. The header means TransactionID(2 bytes), ProtocolID(2 bytes), Length(2 bytes);
  • WithLOG - when the parameter is checked, all the communication packets are written at the application LOG file: c:\AltonaLab\LogFiles\. Please use this parameter only to investigate the communication issues;

ModBus commands settings:
"Add" button opens the interface of the command:

  • Name - description of the command;
  • Block's In/Out prefix - each new added command adds a group of inputs or outputs to the block.  The names of the group of Inputs/Outputs begin with the same text prefix;
  • ModBus command - combo box to select a ModBus command;
  • Starting address - starting address of the ModBus memmory, been affected from the command;
  • Num In/Out - number of inputs/outputs of the command;
  • Scan rate [milliseconds] - every new added command adds a digital input of the Block with Command prefix and suffix _SendCommand. If this input is connected, the command will be send only if the input becomes to high level. If the input is not connected, the command will be send per Scan rate milliseconds;
  • Swap Hi/Lo data bytes - for some device manufactures, Hi and Low bytes of the data types as WORD has to be swapped. This affects only bytes in Data of the communication frame and not affects the frame header;

There are some restrictions between Modbus command and Data type as:

  • For commands for Coil and Discrete inputs, only Boolean data type can be set;
  • For Holding registers and Input registers memories, the data type can be Boolean, Word, Float, Double, Int;

Block's outputs for system use:

  • CommunicationError - in case the packet is not send or there is not respond from sent packet, the output becomes to a high level;
  • DeviceExceptionCode - if the sent command is wrong, the ModBus Server responds with error code;
  • DeviceExceptionCode - the corresponding text of the received error code;

Every added command adds a group of Inputs\Outputs of the block. Two of them are with special purpose:

  • Digital input with name of a Command prefix and a sufix _SendCommand - if the input is connected and becomes to high value, the command will be send;
  • Digital output with name of a Command prefix and a sufix OnCommand - becomes at high level when the respond of the command is received and if there are outputs of the block associated with the command - are updated;

 

Block ModBusServer

License: Industrial

The block can open a TCP/IP port or serial ComPort and an unlimited number of devices can connect to it using ModBus protocol.

All the block settings are available at the Setings parameter.

Server settings are:

  • Server memory - four separated Server memories, each with an Starting address and Memory size;
  • Communication - can be set as TCP/IP or ComPort;
  • DeviceID - the ID of the ModBus Server at Ethernet or RS485 network;
  • Use zero based addressing - for some device manufactures, address 0001 is intepreted as address 0000. For example, for Advantech devices, the parameter has to be checked;
  • WithLOG - when the checkbox is checked, all the communication packets are written at the application LOG file: c:\AltonaLab\LogFiles\. Please use this parameter only to investigate the communication issues;
  • The bottom grid is used to add read/write operations to the Server memory. This happens with button Add;

Read/Write operations to Server memories:

 

  • Every added Read operation appears as block's outputs, every Write operation appears as block's inputs;
  • Each operation adds to block two system inputs/outputs - a digital input with name formed with In/Out Prefix plus text _DoProcess, a digital output formed with In/Out Prefix plus text _OnReady. When the digital input becomes to high level, the values of the inputs related to the operation are saved to the server memory;
  • The outputs of the block, related to a Read operation, show the current status of the server memory. If a ModBus command is received, which affects a memory related to the outputs of the block, the outputs will be refreshed automaticaly;
  • For Write operations, values from related inputs are saved to the server memory;

Settings of the Read/Write operation are:

  • Name - name of the operation;
  • Block's In/Out prefix - every Read/Write operation appears as Block's Outputs/Inputs, which names are formed using In/Out prefix. Additional two digital system In/Out will be added to the block. The digital input is formed by the name Prefix plus _DoProcess, the digital output is formed by the name Prefix plus _OnReady. When the system digital input becomes to high value, the operation will be executed. The system digital output becomes to high value when in case of executed operation or if ModBus server receives a command, which changes the Server memory related to the data of the current operation;
  • Server memory - can be - Coils, DiscreteInputs, HoldingRegisters, InputRegisters;
  • Read/Write - for Coils and HoldingRegisters can be Read or Write, for DiscreteInputs and InputRegistes can be only Write;
  • Memory Data type - for Coils and DiscreteInputs can be only Boolean, for HoldingRegisters and InputRegisters can be Boolean, Word, Float, Double, Int. The setting determines how the memory is interpreted;
  • Block IO Data type - some Memory Data type as WORD can be operated as Analog or Integer block's inputs/outputs;
  • Starting address - starting address of the ModBus Server memory, been affected from the command;
  • Num In/Out - number of inputs/outputs of the operation;
  • Swap Hi/Lo data bytes - for some device manufactures, Hi and Low bytes of the data types as WORD has to be swapped. This affects only bytes in Data of the communication frame and not affects the frame header;
  • Dump memory color - the memory bytes affected from the operation are shown with this color at the Memory Dump interface;

Memory Dump:
If the block's input Dump becomes to high level, a Memory Dump interface will be open:

The interface shows byte content from all four server memories. Elements of the interface are:

  • Server memory combo box - one of the four server memories can be selected;
  • The grid shows all the Read/Write operations from the memory selected at the combo box above;
  • HEX Address - shows the addresses at bottom area in HEX or Decimal numbers;
  • HEX Memory - shows the memory bytes in HEX or Decimal numbers;
  • When one of the memory byte is clicked with mouse, the values of the data types located at the clicked address are shown at text controls as - Binary, Word, Float...
  • Affected memory bytes from Read/Write operations are shown with operation's Dump memory color setting;