I tried to control a gripper using Modbus RS485 for the communication, the Modbus connected to X20CS1030 unit, I’ve never programmed a Modbus communication before, and I need some guide how to make the program, is there any example program that I can learn?
There is an example for this embedded in Automation Help. The library for this is called “DRV_mbus”
Automation Help: Modbus - Reading and writing variables
The sample itself can be added to an Automation Studio project in the Logical view through the toolbox by choosing the option “Library Samples”.
I found it and I already read it, but I still don’t understand how can I send data to Slave, where do I put the code? is it in datamod?
The registers are linked to PLC variables in the datamodule. It is explained more in the help for the library.
I still don’t understand how datamodule work, especially EventPV and LocalPV, can you help me?
EventPV is a PLC variable, that defines whether the line is executed or not. If left empty the line will be always executed.
LocalPV is the variable where the value to write or read is located.
In you example the first line writes 3 registers to station number 1 with the value from variable LocalPV4 to offset 0 (HoldingRegister 0) using Modbus Function 16(Preset multiple registers) when EventPV4 is set to true.
The second line reads 3 holding registers from station number 1 starting from offset 0 (HoldingRegister 0) using Modbus Function 3 (Read holding registers) when EventPV3 is set to true.
EventPV
EventPV = 0 Entry not processed (skipped)
EventPV = 1 Entry processed
If a response frame is obtained without an error, the associated EventPV is set to 0 once. If an EventPV is not specified, then this entry must be executed cyclically.The BOOL, SINT, and USINT variable types are supported as Event PV, with no restriction on entry length, and dynamic PVs (important: indicated by *) are also supported. If no EventPV is specified, this entry must be executed cyclically.
Station number (destination address) (USINT)
This entry defines the destination address of the slave station. Valid destination addresses are between 0 and 247, with 0 acting as a broadcast (message to all stations).DataPV
PV that is read from or written to depending on the specified function code. Variable types BOOL, SINT and USINT are supported for Modbus functions 1, 2, 5 and 15; variable types UINT and INT are supported for functions 3, 4, 6 and 16. Any length of the entry is possible; dynamic PVs can also be used (important: specify with *).When naming PVs, the guidelines listed under Names of process variables and Notes for using application modules must be observed.
Offset (UINT)
For the offset of Modbus variables, see the documentation for the Modbus slave.Length (UINT)
Number of coils or registers
Ah I understand now. So if I want to make multiple command, let say to Read Speed, Jog Positive and Jog Negative an Inverter that use Modbus RS485 as its communication protocol, I need to make 3 commands inside of datamodule with the format like the one in the example?
This is correct if all three are in separate registers. If for example the Jog Positive and Negative are in the same register but different bits in a WORD type register you only need 2 commands in the datamodule.
If you have multiple consequent registers to read you can read them to an array with a single command (and should, as this is faster and more efficient than using a separate command for each register).
And with the EventPV you can select, when each command is executed, or leave blank and they will be executed always.