Implementation \ in the user programΒΆ

The Communication Module is now ready for Modbus client operation. Now the blocks for starting the requests to the servers have to be implemented into the user program.

For this purpose, start the CoDeSys programming software and create a new project or open a corresponding existing project. At first integrate the used Ethernet Communication Module in the PLC configuration. Insert an instance of the function block ETH_MOD_MAST: ETH_MOD_MAST ETH_MOD_MAST in your project now.

Assign the slot number (module number) of the used Ethernet Communication Module to the block input Slot. If the internal Ethernet Communication Module is used, this corresponds to module number 0. When using external Communication Modules, these Communication Modules are numbered consecutively from right to left. The first external Communication Module has the module number 1.

The IP address of the first sensor is 10.49.91.251. This IP address has to be applied to input IP_ADR of the ETH_MOD_MAST block. Each byte in IP_ADR represents one octet of the address. Thus, for our example the value 16#0A315BFB (hexadecimal) or 171006971 (decimal) has to be assigned to IP_ADR.

Since the temperatures shall be read from the sensors in the form of a word, the value 3 has to be assigned to FCT and 1 has to be specified for NB. At input ADDR, the register address in the server has to be specified from which the word should be read. Starting from the assumption that the current temperature is stored in the sensor under register address 0, you have to enter the value 0 here.

Furthermore, the data read from the first sensor shall be stored in the variable Data_MM1 in the operand memory of the controller. Thus, block input DATA has to be wired with this operand via the ADR block.

The Modbus request to a server is started with a FALSE->TRUE edge at input EN. The release of input EN is controlled by a step chain depending on the outputs DONE and ERR (available for exactly one PLC cycle after the task execution).

Represented in FBD, this results in the following program part for the first server.

../_images/6ce8f283629cfc150a331390266678c8

Now insert a corresponding block for the second server. The assignments for the block inputs are almost identical. Only the IP address (16#0A315BFC), the operand used to store the read values (Data_MM2) and the step chain are different.

../_images/1f104100e38bcca70a33139000eba54e
../_images/60e5ffbee38bccf50a33139000eba54e

If required, the program additionally has to be expanded by an evaluation of possible errors. If an error occurs during the processing of a Modbus request, this is indicated at output ERR of the corresponding block ETH_MOD_MAST: ETH_MOD_MAST ETH_MOD_MAST. The general Modbus processing status can be displayed and evaluated using Function Block ETH_MOD_INFO: ETH_MOD_INFO ETH_MOD_INFO.

Download the project to the controller. After this, implementation of the Modbus client is completed.