Video tutorial: "Custom Communication for Motion Controllers in Positive Motion Technology"
The previous section discussed MODBUS communication for motion controllers. Today, we'll learn about custom communication for motion controllers.
The controller's serial port and network port use the MODBUS protocol for communication by default. If the other device does not support the MODBUS protocol, a custom communication mode (no protocol) can be enabled.
Both the serial port and the network port can be configured for custom communication.
The methods for enabling custom communication on serial ports and network ports are different, but both use commands to enable custom communication. The read and write commands used to read and write channel data are the same.
Use the SETCOM command to enable custom communication for the serial port, and use OPEN # to enable custom communication for the network port. Select the correct PORT channel to enable communication.
Custom read/write commands for serial and network ports: Read command: GET #; Send commands: PRINT # and PUTCHAR #.
The product tested in this article is the ZMC316 stand-alone pulse motion controller, a rapid development 16-axis motion control system from Zheng Motion Technology.
Before we begin our formal learning, let's first introduce the ZMC316 controller. It supports up to 24-axis linear interpolation, arbitrary circular interpolation, spatial circular interpolation, helical interpolation, electronic cams, electronic gears, synchronous following, virtual axis settings, etc.; and it can achieve real-time motion control by using an optimized network communication protocol.
The ZMC316 is debugged using the ZDevelop development environment, a convenient programming, compiling, and debugging environment. Applications can also be developed using software such as VC, VB, VS, C++Builder, and C#. During debugging, the ZDevelop software can be connected to the controller simultaneously; the program requires the dynamic library zmotion.dll to run.
The ZMC316 has 16 axes and supports up to 24 virtual axes. The ZMC316 can expand its axes via expansion modules.
The ZMC316 board comes with 24+15 general-purpose input ports, 8+15 general-purpose output ports (the first 15 axes have 1 additional input port and 2 additional output ports), 2 0-10V AD converters, and 2 0-10V DA converters.
The ZMC316 comes with one RS232 serial port, one RS485 port, one RS422 port, and one Ethernet port.
The ZMC316 comes with a CAN bus interface, supporting the connection of expansion modules via the ZCAN protocol.
The ZMC316 has a USB flash drive interface.
Each axis can output pulse frequency up to 8MHz.
ZMC316 Wiring Diagram
The following is the main content of this article.
Custom serial communication (no protocol )
1. How to enable
To enable custom serial communication, you need to select a port number. The default port number is port0 for RS232 and port1 for RS485. Use commands to configure the port to the custom serial communication mode.
In addition to configuring the basic communication parameters of the serial port, the SETCOM command can also select parameters to configure the communication protocol, enable the self-defined serial port communication mode (mode=0 for no protocol mode), and fill in the basic serial port parameters and the port number to be enabled.
Syntax: SETCOM (baudrate,databits,stopbits,parity,port[,mode] [,variable] [,timeout])
Enable Example:
RS232(PORT0):SETCOM (38400,8,1,0,0,0,2,1000)
RS485(PORT1):SETCOM (38400,8,1,0,1,0,2,1000)
2. Reading and writing methods
The same commands are used for custom communication via network port or serial port.
Read characters from a custom channel: GET #
Sending characters in a custom channel: PRINT #, PUTCHAR #
Both commands can only send one piece of data at a time. To send multiple pieces of data, an array can be used. The difference between PRINT # and PUTCHAR # is that the former sends a string, and can only send one piece of data at a time, and the array sent is in ASCII format; the latter sends only ASCII characters.
(1)GET #
Syntax 1: GET #PORT, VARIABLE
Syntax 2: GET #PORT, ARRAY[(startindex)] [,maxchares]
Syntax 3: charesget = GET #PORT, VARIABLE
Syntax 4: charesget = GET #PORT, ARRAY[(startindex)] [,maxchares]
port: Channel number
variable: The name of the variable that is stored.
startindex: The starting address of the array.
maxchares: The maximum number of characters that can be stored.
If syntax 1 and 2 are not read, the function will block. This function is generally called in a multitasking environment.
Syntax 3 and 4 will return the number of bytes read.
All four syntaxes can be used for TCP communication via serial port and network port. For UDP communication via network port, use syntax 4 and use an array to receive packets. The array length should not be smaller than the length of a single UDP packet.
Example 1: Reading characters
GET # 11,tempchar 'Retrieves a single character from custom network interface channel 11 into tempchar'
Read results:
Example 2: Reading an array
GET #11,array(0),5 'Retrieves a maximum of 5 characters into an array at a time; subsequent data overwrites previous data.'
Reading results:
Example 3: Reading an array (this syntax can only be used in UDP mode)
CHARES=GET #11, Data, 10 'Get data from channel 11 into an array, with the data length set to 10.
Reading results:
(2)PRINT #
PRINT # PORT, "string"
port: Channel number
Example 1: Sending a string
PRINT # 11, tempchar 'Send one string at a time'
Example 2: Sending an array of ASCII codes
PRINT # 11, Data 'Send one string at a time'
(3) PUTCHAR #
Syntax 1: PUTCHAR # PORT, character
Syntax 2: PUTCHAR # PORT, ARRAY(index, numes)
port: Channel number
index: The starting position for output
numes: The number of bytes output, in binary format.
Example 1: Sending characters in ASCII format is not allowed; strings cannot be sent directly.
PUTCHAR # 11, tempchar 'Multiple characters can be sent at once, separated by commas.'
Example 2: Sending an array of ASCII codes
PUTCHAR # 11, Data 'Send one string at a time'
3. Communication Routine
The controller uses a 485 bus to initiate custom communication to read data from the Mitsubishi absolute encoder.
'//*****The length of the useful string in the returned data frame is 8****'
global Dim CmdBuff(10) 'Send command array
global Dim GetBuff(30) 'The string to be received
for i=0 to 29
GetBuff(i)=0
next
global Dim TempChar 'Receives one byte
TempChar=0
global Dim GetNum 'Number of bytes to receive'
GetNum=0
global sanReturn 'The return value after the task is completed, for easy reading by the PC.'
sanReturn=0
SetCom(19200,8,1,2,1,0) 'Establish 485 connection
A new task must be created because the Get command will block the current task if it does not receive data.
RunTask 5, get_Char
delay (100)
The values in the command array are written according to the custom communication protocol requirements of the slave Mitsubishi.
'\x01\x31\x30\x32\x02\x39\x31\x03\x30\x32
The current slave address is 1. After changing the slave address, the verification code must be changed.
CmdBuff(0,$01,$31,$30,$32,$02,$39,$31,$03,$30,$32)
PutChar #1,CmdBuff 'Send data to the 485 serial port
TICKS=1000
while(1)
if GetNum=14 AND (GetBuff(2)=97 OR GetBuff(2)=65) then
"485 success"
Place the useful content of the returned data frame into the VR register for easy reading by the PC.
for i = 0 to 7
VR(100+i) = GetBuff(i+3)
next
sanReturn=1
exit while
elseif TICKS<0 then
"ERROR"
STOPTASK 5
exit while
endif
wend
end
global Sub Get_Char()
print "Entering the receiving function"
While 1
Get #1,TempChar 'Receive character returned from serial port
print "Number of characters received", GetNum TempChar
GetBuff(GetNum) = TempChar
GetNum=GetNum+1
'Check if the length of the returned data frames is correct'
if (GetNum = 14) Then
exit while 'Exceeds the length of the while loop'
endif
Wend
End Sub
Custom communication via network port 2
1. How to enable
When enabling custom communication, you need to enter the port number. First, you need to check the channel number of the controller's custom communication. You can check it by sending ?*port in the online command, as shown in the figure below. ECUSTOM is the custom network port channel.
After connecting to the controller via the network port, the status of each channel is printed as follows: unused channels are marked with a status of 0, and used channels are marked with a status of 1. For the serial port, the serial port channel status is always 1 regardless of whether it is used or not.
The third channel is the ETH network interface channel, which is occupied by the ZDevelop software.
Enable custom communication on the network port by selecting port 11. The status will then look like this:
Custom communication on the network port is enabled using the OPEN # command, which is used to select the master or slave device.
OPEN #PORT, "mode", portnum [, ipaddress]
port: Communication channel. See the PORT description. Select a custom network channel.
mode: master/slave, "TCP_CLIENT" - slave, "TCP_SERVER" - master, "UDP_CLIENT" - UDP slave, "UDP_SERVER" - UDP master.
portnum: TCP or UDP port number. The host port number is the local port number, and the slave port number is the remote port number.
ipaddress: The IP address of the other party, a string, which must be provided on the slave end.
A UDP_SERVER must receive data from the other party before it can send data back (unless the other party is explicitly specified using PORT_TARGET).
The local port number for UDP_CLIENT is random. It must be sent to the other party first so that the other party can know the port number. In this mode, packets that are not specified to the other party will be dropped.
Custom UDP communication requires firmware version 20170628 or higher for the 4-series controller; or firmware version 20170702 or higher for the XPLC series controller.
Before sending or receiving data, TCP must establish a reliable connection with the other party, which greatly improves the reliability of data communication.
UDP does not establish a connection before sending or receiving data. When it wants to transmit, it grabs data from the application and throws it onto the network as quickly as possible, which may result in packet loss. UDP programs have a simpler structure, and their packets are smaller, only 8 bytes, while TCP packets are 20 bytes.
2. Reading and writing methods
Read and write commands are the same as for serial ports. UDP reading can only use the GET # syntax 4.
3. TCP Communication Routine
The program uses a custom network port channel, port11, as follows.
OPEN #11, "TCP_SERVER", 945 'Use custom network interface channel 2, controller as master, port number 945
GLOBAL tempchar
GLOBAL CONST datamax=20 'Defines the size of the array space'
GLOBAL Data(datamax) 'Defines an array
GLOBAL datanum 'Defines the array number
datanum=0
ClearData() 'Clears the array
WHILE 1
tempchar = 0 'Clear the previous characters'
GET #11,tempchar 'Get a single character into tempchar'
PRINT datanum,tempchar 'Prints the ASCII code of the character.
Data(datanum) = tempchar 'Save to array
datanum = datanum + 1
IF datanum = datamax THEN 'Exceeds array space, clear the array'
datanum = 0
ClearData()
ENDIF
IF tempchar = 59 THEN 'Terminal bit;
PRINT #11,"ok" 'Send string
ENDIF
WEND
END
GLOBAL SUB ClearData()
FOR i = 0 TO datamax-1 'Clear the array space contents
Data(i) = 0
NEXT
END SUB
Running result:
4. UDP Communication Routine
The program uses a custom network port channel, port11, as follows.
OPEN #11, "UDP_SERVER", 945 'Use custom network interface channel 2, controller as master, port number 945
GLOBAL CHARES
GLOBAL CONST datamax=10 'Defines the size of the array space'
GLOBAL Data(datamax) 'Defines an array
ClearData() 'Clears the array
WHILE 1
CHARES=GET #11, Data, 10 'Get data from channel 11 into an array, with a data length of 10.
IF CHARES > 0 THEN
Data(CHARES) = 0 'Setting complete 0'
PRINT Data 'Print string'
ENDIF
WEND
END
GLOBAL SUB ClearData()
FOR i = 0 TO datamax-1 'Clear the array space contents
Data(i) = 0
NEXT
END SUB
Running result:
That concludes our discussion on custom communication 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.