Video tutorial: "Video Tutorial: Forward Motion Technology, Motion Controller, MODBUS Communication"
Material preparation
1 computer
2. Two controllers
3. One +24V DC power supply
4. Several drivers and motors
5. One switch
6. Several controller wiring terminals
7. Several network cables
8. Several connecting wires
Note: I/O devices, extension templates, etc., can be selected according to requirements.
Controller wiring configuration
Introduction to MODBUS Communication Protocol
The MODBUS protocol is a universal standard protocol that is supported by almost all control devices and intelligent instruments. This allows devices from different manufacturers to communicate with each other and connect to the same control network.
MODBUS has two serial transmission modes: RTU mode and ASCII mode. RTU mode transmits binary data, while ASCII mode transmits ASCII code data. RTU mode has a faster communication speed than ASCII mode.
MODBUS has evolved to include several protocol modes: the default protocol for both the serial port and network port of the controller is the MODBUS protocol, with the serial port using the MODBUS_RTU protocol and the network port using the MODBUS_TCP protocol.
The controller supports serial ports in MODBUS_RTU mode: RS232, RS485, and RS422. When data is received from the serial port, it will directly control the device based on the data in the message.
The controller supports the following network protocols: MODBUS_TCP interface and Ethernet interface.
The network port is mainly used for communication with PCs and human-machine interfaces.
The controller's serial and Ethernet ports read and write data under the MODBUS protocol, using MODBUS-related commands. Data is stored in MODBUS registers during transmission. Alternatively, the serial or Ethernet port can be configured to a protocol-free mode using commands, employing custom communication commands for reading and writing data.
The interface configurations of the ZMC316 series pulse controller are as follows:
The communication interfaces include RS232, RS485, RS422, Ethernet, USB flash drive, and CAN interface.
It comes with 16 pulse axes, and the front panel includes 15 pulse interfaces and 3 encoder interfaces. The 16th pulse axis is controlled by the OUT output port.
It supports expansion to 24 pulse axes using expansion modules, and supports up to 24-axis linear interpolation, arbitrary circular interpolation, spatial circular arc, helical interpolation, electronic cam, electronic gear, synchronous following, virtual axis settings, etc.; it adopts an optimized network communication protocol to achieve real-time motion control.
Applications can be developed using ZDevelop software or software such as VC, VB, VS, C++, LabVIEW, C#, etc.
01 Description of Type III Serial Ports
RS232 serial ports are commonly used to connect to PCs or human-machine interfaces.
RS485 serial ports are commonly used for communication and control with control devices and intelligent instruments.
RS422 serial port is only available on a few controller models.
RS232
The controller's RS232 interface can function as either a MODBUS master or slave, supporting one master to send data and one slave to receive data. When acting as a master, it can connect to drives, frequency converters, temperature controllers, etc., for data reading and writing control. When acting as a slave, it can connect to a human-machine interface (HMI) to monitor operating status, commonly used for connecting to a PC or HMI.
The controller's RS232 interface uses a DB9 male connector, and the two devices are connected using the corresponding connecting cable.
The standard RS232 wiring only requires three wires: two data signals TXD and RXD, and one ground wire EGND. The data signals TXD and RXD are cross-connected, and then EGND is connected together.
Wiring reference is as follows:
RS485
It primarily provides online connectivity for multiple communication devices in master/slave mode, theoretically supporting up to 128 nodes. When acting as a master station, it can connect to drives, frequency converters, temperature controllers, etc., for data reading and writing control; when acting as a slave station, it can communicate with a PLC and connect to a human-machine interface to monitor operating status.
The RS485 interface uses differential transmission, which determines whether the voltage level is high or low by judging the voltage difference between A and B.
The controller's RS485 interface uses a simplified wiring method, as shown in the diagram below. The controller's 485A, 485B, and EGND ground wires are connected to the A, B, and ground wires of the first slave station, respectively, and then to the A, B, and ground wires of the second slave station (A to A, B to B, signals share a common ground). Furthermore, a 120-ohm resistor must be connected in parallel between the controller and the last slave station's 485A and 485B to prevent signal reflection. Shielded twisted-pair cables must be used to avoid signal interference, and the distance between each node branch line should be less than 3 meters.
RS422
The controller's RS422 interface can function as a MODBUS master or slave, supporting 1 transmit and 10 receive.
The controller's RS422 interface uses a simple wiring method, which is a four-wire system. Five wires need to be connected: RX+/RX- (receive signal), TX+/TX- (transmit signal), and one signal ground wire.
Compared to RS485 and RS232, the RS422 interface of the controller has higher wiring costs and is easier to make mistakes in wiring. There are very few controller models that include the 422 interface.
02 Serial Port Parameters
Serial port connection: The serial port can usually be selected as RS232 or RS485. Regardless of the type of serial port, the default parameters and operation commands are the same, except for the port number and wiring method.
The default serial port parameters are: baud rate 38400, 8 data bits, 1 stop bit, and no parity. The serial port parameters are configured using the SETCOM command. The SETCOM parameters will revert to their default values after the controller is powered on again, so please include the SETCOM settings at the beginning of your program.
The MODBUS protocol station number ADDRESS for all serial ports of the controller is 1-127, with a default of 1.
All devices on a MODBUS network must select the same transmission mode and serial port parameters; otherwise, they cannot communicate.
The controller defaults to RS232 serial port PORT0 (COM0), RS485 serial port PORT1 (COM1), and RS422 serial port PORT2 (COM2). You can view the output by sending "?*setcom" in the online command window or in the "Controller Status" window.
In addition to configuring the basic parameters of serial port communication, the SETCOM command can also select parameters to configure the communication protocol, set the controller as a master or slave device (default is slave), and enable a custom communication mode (i.e., mode=0 no protocol mode).
Syntax: SETCOM (baudrate,databits,stopbits,parity,port[,mode] [,variable] [,timeout])
mode parameter selection protocol:
1. When communicating with the touchscreen, the mode parameter selects the protocol: 4
Port0: setcom(9600,8,1,0,0,4,2,1000)
Port1: setcom(9600,8,1,0,1,4,2,1000)
2. When the controller acts as a master station via RS485, the mode parameter should select protocol 14.
Port1: setcom(38400,8,1,0,1,14,2,1000)
03 Network Port Parameters
The controller's network port supports the MODBUS-TCP protocol. The factory default IP address is 192.168.0.11. The IP address can be modified using the IP_ADDRESS command or in the IP address modification window. Once modified, the change is permanent.
The controller has at least two network ports, which can be viewed by sending the command "?*port" online. As shown in the figure below, the port number of the network port is 502.
ZMC432 channel
ZMC316 Channel
Different controllers can be interconnected via Ethernet ports. There is only one Ethernet cable interface, which can be used with a switch. The prerequisite for interconnection is that both controllers have CONNECT channels. The connection is enabled using the MODBUSM_DES2 command.
04 Serial Port/Network Port Read/Write
MODBUS communication uses function codes to tell the addressed terminal what function to perform, such as running commands, reading monitoring status, modifying parameters, reading parameters, etc. The MODBUS protocol defines the relevant function codes.
The controller can also directly use MODBUS communication function codes. For ease of use, some commonly used function codes are directly encapsulated as Basic instructions, as shown in the table below.
The connection methods for serial port and network port communication are different, but the same set of instructions is used for reading and writing, and the data is stored in the MODBUS register.
1. MODBUS Register
MODBUS communication data is stored in MODBUS registers. The controller's MODBUS registers conform to the MODBUS standard communication protocol and are divided into two categories: bit registers and word registers.
Bit register: MODBUS_BIT (Boolean).
Word registers include MODBUS_REG (16-bit integer), MODBUS_LONG (32-bit integer), MODBUS_IEEE (32-bit floating-point), and MODBUS_STRING (8-bit byte).
In the controller, the MODBUS word registers occupy the same system variable space. One LONG register occupies two REG addresses, and the other IEEE register also occupies two REG addresses. When using them, care should be taken to stagger the word register number addresses.
Therefore, it is important to note that the MODBUS_REG, MODBUS_LONG, and MODBUS_IEEE addresses must not overlap in user applications.
The controller's MODBUS storage space is arranged as follows:
2. MODBUS Read/Write Related Commands
3. MODBUS read/write syntax is the same, as shown below:
MODBUSM_REGGET (startreg, num, local_reg)
startreg: The starting register number on the other end, starting from 0.
num: Number of registers
local_reg: Value retrieved from the local system MODBUS register, starting number.
MODBUSM_REGSET (startreg, num, local_reg)
startreg: The starting register number on the other end, starting from 0.
num: Number of registers
local_reg: Value retrieved from the local system MODBUS register, starting number.
Example of use:
MODBUSM_REGGET(0,10,100) 'Copies bit registers 0-9 from the peer's registers to registers 100-109 in the local communication location.
MODBUSM_REGSET(0,10,100) 'Sets local bit registers 100~109 to registers 0~9 on the communication peer.
4. Establish MODBUS communication
(1) Serial port
MODBUSM_DES (address[,port],[timer],[resendset])
ADDRESS1 = MODBUSM_DES([port])
address: Modbus protocol station number at the other end
port: The port number of the current Modbus master communication.
timer: Message timeout setting, default is 1000ms.
resendset: Timeout message retransmission setting, 0 - do not retransmit, 1 - retransmit SEND command, 2 - retransmit both SEND and MODBUSM commands.
(2) Network port
MODBUSM_DES2 (id,port,"desipaddress",[timer],[resendset])
id: The MODBUS slave ID of the other controller, default 1
port: Supports two modes, ?*PORT confirms the channel number and mode.
When using ETH, it acts as the master channel of MODBUS_TCP.
When using CONNECT, it serves as a controller interconnect channel.
desipaddress: A string representing the IP address of the remote controller.
timer: Message timeout setting, default 1000ms
resendset: Timeout message retransmission setting, 0 - no retransmission, 1 - retransmit after SEND command timeout, 2 - retransmit after both SEND and MODBUSM commands timeout.
05 Serial Communication Example
The controller uses RS485 for communication between the master and slave stations.
The project contains three files:
ModbusRead.bas: Establishes a communication connection, allowing the master station to read data from the slave station. The online command `RUN` starts the task; the write task must be stopped before starting. The syntax for starting is: `RUN "ModbusRead", 1`.
ModbusSet.bas: Establishes a communication connection and copies data from the master station to the slave station. The online command `RUN` starts the task; stop the reading task before starting. The syntax is: `RUN "ModbusSet", 2`.
ModbusIni.bas: Parameter initialization, sets RS485 master/slave information, number of slave nodes, etc. Sets the task to run automatically upon power-on.
The example program sets up one slave station, but supports setting up multiple slave stations. The number of slave stations can be modified by modifying the slaveCount parameter.
To facilitate data observation, both the master station and the slave station use a controller, and the two controllers are connected via RS485.
Note that the master controller's RS485 needs to be in Modbus Master mode, as shown in the diagram below. The slave controller's RS485 needs to be in Modbus Slave mode.
ModbusIni.bas file program
'********************Variable Definition*********************
Global slaveCount
slaveCount = 1 'Number of slave stations'
Because the slave address (ADDRESS) cannot be 0 (0 is the broadcast address), the slaveIDs start from 1, so the array size is increased by 1.
Global slaveID(slaveCount+1)
For i = 1 to slaveCount 'Index starts from 1'
slaveID(i) = i 'Slave ID starts from 1, usually set by the DIP switch or slave configuration software.
Next
Global keepReading, modbusErrorStatus, writeSlaveID
writeSlaveID = 1 'Users can select which slave station to write data to via a PC program.
keepReading = 1 'This is a flag variable for continuous reading; the user does not need to modify it, and it is used as a condition for the while loop.'
modbusErrorStatus = 0 'A flag variable to determine whether communication is normal'
'************************Serial Port Parameter Settings*********************
'SETCOM (baudrate,databits,stopbits,parity,port [,mode] [,variable] [,timeout])
The parameters are as follows: baud rate, data bits, stop bits, parity bits, and serial port number (default is port 0 for RS232 and port 1 for RS485).
The last three default parameters are: MODBUS mode (14 represents the controller as the master station, 4 is the default for the slave station), MODBUS register settings (2 is the default, MODBUS registers are independent and not mapped to VR or TABLE), and communication timeout threshold.
SETCOM(38400,8,1,0,1,14,2,1000)
ModbusRead.bas file program
Mutual exclusion prevents simultaneous read and write operations; multiple Modbus commands running concurrently will cause communication errors.
STOP "modbusSet.bas"
'Used as a success flag variable for Modbus communication'
modbusErrorStatus = 0 ' 0 indicates normal communication, 1 indicates abnormal communication'
while keepReading
For i = 1 to slaveCount
MODBUSM_DES(slaveID(i),1) 'Communicates with slave station 1
'MODBUSM_REGGET (0, 8, 10)' Copy data from registers 0-7 of slave station 1 to registers 10-17 of master station.
MODBUSM_REGGET(0,8,slaveID(i)*10) 'Get the value of the peer's register
WAIT UNTIL MODBUSM_STATE <> 1 'Waiting for the message to end
IF MODBUSM_STATE=0 THEN
`slaveID(i) `, indicating that node i is communicating normally.
Else
`slaveID(i) ` indicates a communication error with node `i`.
modbusErrorStatus = 1
You can add the user's error handling procedure here.
Endif
Delay(100) 'Communication delay between two nodes
Next
Wend
ModbusSet.bas file program
Mutual exclusion prevents simultaneous read and write operations.
keepReading = 0
STOP "ModbusRead.bas"
modbusErrorStatus = 0
If the check for slave ID is cancelled (If), then the master register data corresponding to all slaves needs to be written in advance.
FOR i = 1 To slaveCount
IF slaveID(i) = writeSlaveID THEN 'Iterate through the slave nodes, the writeSlaveID parameter selects which slave node to write the master data to.
MODBUSM_DES(slaveID(i), controllerPort) 'Establish master-slave connection'
'MODBUSM_REGSET(10, 2, 110)', the data in master station registers 110-111 is passed to slave station 1 registers 10-110.
MODBUSM_REGSET(10,2,100 + slaveID(i)*10)
WAIT UNTIL MODBUSM_STATE <> 1 'Waiting for the message to end
IF MODBUSM_STATE=0 THEN
`slaveID(i) `, indicating that node i is communicating normally.
ELSE
modbusErrorStatus = 1
`slaveID(i) ` indicates a communication error with node `i`.
You can add the user's error handling procedure here.
ENDIF
DELAY(100) 'Communication delay between two nodes'
ENDIF
NEXT
Whether to resume the loop reading task depends on the application.
DELAY (3000)
'RUN "ModbusRead.bas"
Running effect
1. MODBUSM_REGGET read: Reads 8 data entries (0-7) from the slave MODBUSM_REG register to the master MODBUSM_REG register (10-17).
Data from the slave station:
The initial data on the main station was all 0. After reading the main station data, 8 data points from the slave station were read.
2. Write MODBUSM_REGSET: Write two data entries (110-111) from the master station's MODBUSM_REG register to the slave station's MODBUSM_REG register (10-11).
Main site data:
The data read from the slave station is as follows: Two data points were read from the master station.
06 Network Port Communication Routine
Two controllers are interconnected via a switch network port.
Master controller IP: 192.168.0.11
Slave controller IP: 192.168.0.36
Connect these two controllers using ZDevelop software. The wiring reference is as follows. Use ETH network port channel 5. After downloading the program, check the running effect.
Main program
MODBUS_REG(0)=0
MODBUSM_des2($1 , 5, "192.168.0.36" ) 'Establish a connection between the master and slave ends; the controller IPs cannot be the same.'
WHILE 1
FOR i = 0 TO 10
MODBUS_REG(0) = i
MODBUSM_REGSET(0,1,0) 'Master-side data is written to the slave end'
MODBUS_REG(0) = 99
MODBUSM_REGGET(0,1,0) 'Read data from master and slave ends'
IF MODBUS_REG(0) <> i THEN 'Check if MODBUSM_REGGET(0,1,0) was successfully read
PRINT "modbus_reg(0)=" MODBUS_REG(0), "modbusm_state=" MODBUSM_STATE
ENDIF
NEXT
WEND
END
Slave program
ADDRESS = 1 'Slave station number'
MODBUS_REG(0) = 0
WHILE 1
IF MODBUS_REG(0) <> 0 THEN
PRINT MODBUS_REG(0)
ENDIF
WEND
END
Running result:
The master first sends data to the slave. After modifying the data on the master, it retrieves data from the slave. If the data retrieval is successful, the IF condition is not met, and the print statement is not executed.
The slave receives data from the master, changes the value of MODBUS_REG(0), and prints it in a loop.
07 Network Interface Interconnection Routine
Two controllers are interconnected via a switch network port.
Master controller IP: 192.168.0.11
Slave controller IP: 192.168.0.36
Connect the two controllers using ZDevelop software, employing CONNECT channel 20. Both controllers must have a CONNECT channel for normal communication. After downloading the program, check the running effect.
Main program
MODBUS_REG(0)=0
MODBUSM_des2($1 , 20, "192.168.0.36" ) 'Establish a connection between the master and slave ends; the controller IPs cannot be the same.'
WHILE 1
FOR i = 0 TO 10
MODBUS_REG(0) = i
MODBUSM_REGSET(0,1,0) 'Master-side data is written to the slave end'
MODBUS_REG(0) = 99
MODBUSM_REGGET(0,1,0) 'Read data from master and slave ends'
IF MODBUS_REG(0) <> i THEN 'Check if MODBUSM_REGGET(0,1,0) was successfully read
PRINT "modbus_reg(0)=" MODBUS_REG(0), "modbusm_state=" MODBUSM_STATE
ENDIF
NEXT
WEND
END
Slave program
ADDRESS = 1 'Slave station number'
MODBUS_REG(0) = 0
WHILE 1
IF MODBUS_REG(0) <> 0 THEN
PRINT MODBUS_REG(0)
ENDIF
WEND
END
Running result:
Similar to the network communication routine, the master first sends data to the slave. After modifying the master's data, it retrieves data from the slave. If the data retrieval is successful, the IF condition is not met, and the print function is not executed.
The slave receives data from the master, changes the value of MODBUS_REG(0), and prints it in a loop.
That concludes our discussion of the MODBUS communication mechanism for the Zheng Motion Technology motion controller. For more learning videos and detailed explanations with pictures, please follow our WeChat official account " Zheng Motion Assistant ".
This article is original content from Zheng Motion Technology. We welcome everyone to reprint it for mutual learning and to jointly improve China's intelligent manufacturing level. Copyright belongs to Zheng Motion Technology. Please indicate the source if you reprint this article.