Wiki source code of 08 Communication

Version 5.1 by Stone Wu on 2022/07/07 15:43

Show last authors
1 The VD2 series servo drive has Modbus communication function, which could cooperate with the host computer for parameter modification, parameter query, monitoring volume servo status query and control. The servo drive is used as a slave device.
2
3 = **Modbus communication** =
4
5 == **Hardware wiring** ==
6
7 The position of RS485 communication port (take VD2B as an example) is as the figure below.
8
9 (% style="text-align:center" %)
10 [[image:image-20220608154248-1.png]]
11
12 Figure 8-1 The position of RS485 communication port of VD2B drive
13
14 For the position of the RS485 communication port of other models, see __[[4.5 Communication signal wiring>>https://docs.we-con.com.cn/bin/view/Servo/2.%20User%20Manual/06%20VD2%20SA%20Series%20Servo%20Drives%20Manual%20%28Full%20V1.1%29/04%20Wiring/#HCommunicationsignalwiring]]__.
15
16 The servo drive adopts RS485 half-duplex communication mode. The 485 bus should adopt the hand-in-hand structure instead of the star structure or the bifurcated structure. The star structure or bifurcation structure will produce reflected signals, which will affect the 485 communication.
17
18 (% class="table-bordered" %)
19 |(% style="text-align:center; vertical-align:middle" %)[[image:image-20220611153134-1.png]]
20 |(((
21 ✎The wiring must use shielded twisted pair, stay away from strong electricity, do not run in parallel with the power line, let alone bundle it together!
22
23 ✎In a half-duplex connection, only one servo drive can communicate with the host computer at the same time. If two or more servo drives upload data at the same time, bus competition will occur. Not only will it lead to communication failure, it may also cause some components to generate large currents and damage the components.
24 )))
25
26 (% style="text-align:center" %)
27 [[image:image-20220608174415-1.png]]
28
29 Figure 8-2 RS485 communication network wiring diagram
30
31 The terminal of RS485 network should use a terminating resistors of 120Ω to weaken the reflection of the signal. Intermediate networks cannot use terminating resistors.
32
33 No point in the RS485 network can be directly grounded. All devices in the network must be well grounded through their own grounding terminals.
34
35 (% class="table-bordered" %)
36 |(% style="text-align:center; vertical-align:middle" %)[[image:image-20220611153144-2.png]]
37 |Under no circumstances can the grounding wire form a closed loop.
38
39 When wiring, consider the drive capability of the computer/PLC and the distance between the computer/PLC and the servo drive. If the drive capacity is insufficient, a repeater is needed.
40
41 = **Modbus communication protocol analysis** =
42
43 == **Modbus data frame format** ==
44
45 The VD2 series servo drives currently support the RTU communication format. The typical data frame format is shown in the table.
46
47 (% class="table-bordered" %)
48 |(% rowspan="2" style="text-align:center; vertical-align:middle; width:425px" %)**There should be a message interval not less than 3.5 characters at the beginning**|(% style="text-align:center; vertical-align:middle; width:166px" %)**Address**|(% style="text-align:center; vertical-align:middle; width:189px" %)**Function code**|(% style="text-align:center; vertical-align:middle; width:155px" %)**Data**|(% style="text-align:center; vertical-align:middle; width:158px" %)**CRC check code**
49 |(% style="text-align:center; vertical-align:middle; width:166px" %)1 byte|(% style="text-align:center; vertical-align:middle; width:189px" %)1 byte|(% style="text-align:center; vertical-align:middle; width:155px" %)N bytes|(% style="text-align:center; vertical-align:middle; width:158px" %)2 bytes
50
51 == **Description of supported function codes** ==
52
53 The host reads and writes data to the servo through Modbus RTU format (03, 06 function codes). The corresponding Modbus function codes are as follows:
54
55 (% class="table-bordered" %)
56 |(% style="text-align:center; vertical-align:middle" %)**Operate**|(% style="text-align:center; vertical-align:middle" %)**Command code**
57 |(% style="text-align:center; vertical-align:middle" %)Read 16-bit/32-bit function code|(% style="text-align:center; vertical-align:middle" %)0x03
58 |(% style="text-align:center; vertical-align:middle" %)Write 16-bit function code|(% style="text-align:center; vertical-align:middle" %)0x06
59 |(% style="text-align:center; vertical-align:middle" %)Write 32-bit function code|(% style="text-align:center; vertical-align:middle" %)0x10
60
61 **Read function code: 0x03**
62
63 Request format:
64
65 |(% rowspan="2" %)**Address**|(% rowspan="2" %)**Function code**|(% colspan="2" %)**Initial address**|(% colspan="2" %)**Number of reads**|(% rowspan="2" %)**CRC check code**
66 |**high byte**|**low byte**|**high byte**|**low byte**
67 |1 byte|03|1 byte|1 byte|1 byte|1 byte|2 bytes
68
69 Correct response format:
70
71 |(% rowspan="2" %)**Address**|(% rowspan="2" %)**Function code**|(% rowspan="2" %)**Number of bytes of returned data**|(% colspan="2" %)**Register 1**|(% rowspan="2" %)**…**|(% rowspan="2" %)**CRC check code**
72 |**high byte**|**low byte**
73 |1 byte|03|1 byte|1 byte|1 byte|…|2 bytes
74
75 **Write function code: 0x06**
76
77 Request format:
78
79 |(% rowspan="2" %)**Address**|(% rowspan="2" %)**Function code**|(% colspan="2" %)(((
80 **Register address**
81 )))|(% colspan="2" %)**Data**|(% rowspan="2" %)**CRC check code**
82 |**high byte**|**low byte**|**high byte**|**low byte**
83 |1 byte|06|1 byte|1 byte|1 byte|1 byte|2 bytes
84
85 Response format:
86
87 |(% rowspan="2" %)**Address**|(% rowspan="2" %)**Function code**|(% colspan="2" %)**Register address**|(% colspan="2" %)**Data**|(% rowspan="2" %)**CRC check code**
88 |**high byte**|**low byte**|**high byte**|**low byte**
89 |1 byte|06|1 byte|1 byte|1 byte|1 byte|2 bytes
90
91 If the setting is successful, the original is returned
92
93 |(% rowspan="2" %)**There should be a message interval not less than 3.5 characters at the beginning**|**Address**|**Function code**|**Data**|**CRC check code**
94 |1 byte|1 byte|N bytes|2 bytes
95
96 (% style="color:inherit; font-family:inherit; font-size:26px" %)**CRC check**
97
98 The servo uses a 16-bit CRC check, and the host computer must also use the same check rule, otherwise the CRC check will make mistake. When transmitting, the low bit is in the front and the high bit is at the back. The CRC code are as follows:
99
100 {{code language="LUA"}}
101 {
102
103     Uint16 crc = 0xffff;
104
105     Uint16 i;
106
107
108
109   while(uLen--)
110
111   {
112
113     crc ^=(Uint16) *pBuf++;
114
115     for(i=0; i<8; i++)
116
117     {
118
119       if(crc & 0x0001)
120
121 {
122
123 crc = (crc >> 1) ^ 0xa001;
124
125 }
126
127 else
128
129 {
130
131 crc = crc >> 1;
132
133 }
134
135
136
137     }
138
139   }
140
141   return crc;
142
143 }
144
145 return crc;
146
147 }
148 {{/code}}
149
150 == **Error response frame** ==
151
152 (% class="table-bordered" %)
153 |(% style="text-align:center; vertical-align:middle" %)**Address**|(% style="text-align:center; vertical-align:middle" %)**Function code**|(% style="text-align:center; vertical-align:middle" %)**Error code**|(% style="text-align:center; vertical-align:middle" %)**CRC check code**
154 |(% style="text-align:center; vertical-align:middle" %)1 byte|(% style="text-align:center; vertical-align:middle" %)Command code+0x80|(% style="text-align:center; vertical-align:middle" %)Error code|(% style="text-align:center; vertical-align:middle" %)2 bytes
155
156 When an error occurs, set the function code bit7 issued by the host to 1, and return (for example, 0x03 returns 0x83, 0x06 returns 0x86); the description of the error code are as follows.
157
158 (% class="table-bordered" %)
159 |(% style="text-align:center; vertical-align:middle" %)**Error code**|(% style="text-align:center; vertical-align:middle" %)**Coding description**
160 |(% style="text-align:center; vertical-align:middle" %)0x0001|(% style="text-align:center; vertical-align:middle" %)Illegal command code
161 |(% style="text-align:center; vertical-align:middle" %)0x0002|(% style="text-align:center; vertical-align:middle" %)Illegal data address
162 |(% style="text-align:center; vertical-align:middle" %)0x0003|(% style="text-align:center; vertical-align:middle" %)Illegal data
163 |(% style="text-align:center; vertical-align:middle" %)0x0004|(% style="text-align:center; vertical-align:middle" %)Slave device failure
164
165 == **Communication example** ==
166
167
168
169 **03 Function code read**
170
171 Read the monitoring volume U0-31 bus voltage, the Modbus register address corresponding to this variable is 7716 (0x1E24)
172
173 Request format:
174
175 |(% rowspan="2" %)**Address**|(% rowspan="2" %)**Function code**|(% colspan="2" %)**Register address**|(% colspan="2" %)**Data**|(% rowspan="2" %)**CRC check code**
176 |**high byte**|**low byte**|**high byte**|**low byte**
177 |01|03|1E|24|00|01|C2 29
178
179 The slave responds normally:
180
181 |(% rowspan="2" %)**Address**|(% rowspan="2" %)**Function code**|(% rowspan="2" %)**Number of bytes**|(% colspan="2" %)**Data**|(% rowspan="2" %)**CRC high byte**
182 |**high byte**|**low byte**
183 |01|03|02|0C|4F|FC B0
184
185 For example: The value read is 0x0C4F, which means that the voltage is 315.1V.
186
187
188
189 **06 Function Code Write**
190
191 P1-10 the maximum speed threshold is set to 3000rpm. This variable corresponds to the Modbus address: 266 (0x010A)
192
193 Request format:
194
195 |(% rowspan="2" %)**Address**|(% rowspan="2" %)**Function code**|(% colspan="2" %)**Register address**|(% colspan="2" %)**Data**|(% rowspan="2" %)**CRC check code**
196 |**high byte**|**low byte**|**high byte**|**low byte**
197 |01|06|01|0A|0B|B8|AF, 76
198
199 The slave responds normally:
200
201 |(% rowspan="2" %)**Address**|(% rowspan="2" %)**Function code**|(% colspan="2" %)**Register address**|(% colspan="2" %)**Data**|(% rowspan="2" %)**CRC check code**
202 |**high byte**|**low byte**|**high byte**|**low byte**
203 |01|06|01|0A|0B|B8|AF, 76
204
205
206
207 **10 Function code write**
208
209 P07-09 set the 1st segment position to 2000, and this variable corresponds to the Modbus address: 1801 (0x0709).
210
211 Request format:
212
213 |(% rowspan="2" %)**Address**|(% rowspan="2" %)**Function code**|(% colspan="2" %)**Initial address**|(% colspan="2" %)**Number of register**|(% rowspan="2" %)**Number of data**|(% colspan="2" %)**Data 1**|(% colspan="2" %)**Data 2**|(% colspan="2" %)**CRC check code**
214 |**high byte**|**low byte**|**high byte**|**low byte**|**high byte**|**low byte**|**high byte**|**low byte**|**high byte**|**low byte**
215 |01|10|07|09|00|02|04|00|00|07|D0|16|59
216
217 The slave responds normally:
218
219 |(% rowspan="2" %)**Address**|(% rowspan="2" %)**Function code**|(% colspan="2" %)**Register address**|(% colspan="2" %)**Data**|(% colspan="2" %)**CRC check code**
220 |**high byte**|**low byte**|**high byte**|**low byte**|**high byte**|**low byte**
221 |01|10|07|09|00|02|90|BE
222
223 = **Servo communication parameter setting** =
224
225 (% style="text-align:center" %)
226 [[image:image-20220608174504-2.png]]
227
228 Figure 8-3 Modbus communication parameter setting process
229
230 **(1) Set the servo address P12-1**
231
232 When multiple servos are in network communication, each servo can only have a unique address, otherwise it will cause abnormal communication and fail to communicate.
233
234 **(2) Set the serial port baud rate P12-2**
235
236 The communication rate of the servo and the communication rate of the host computer must be set consistently, otherwise the communication cannot be carried out.
237
238 **(3) Set the serial port data format P12-3**
239
240 The data bit check methods of servo communication are:
241
242 * Odd parity
243 * Even parity
244 * No parity
245 * The stop bit: 1 stop bit and 2 stop bits.
246
247 The data frame format of the servo and the host computer must be consistent, otherwise the communication cannot be carried out.
248
249 **(4) Set that whether the function code changed by Modbus communication is written into EEPROM in real time [P12-4]**
250
251 When the host computer modifies the servo function code through communication, it can choose to store it in EEPROM in real time, which has the function of power-off storage.
252
253 If the value of the function code only needs to be rewritten once, and the value is used later, the function of real-time writing of the function code to EEPROM can be enabled.
254
255 If you need to change the value of the function code frequently, it is recommended to turn off the function of real-time writing to EERPOM of function code, otherwise the EEPROM will be shortened due to frequent erasing and writing of the EEPROM.
256
257 (% class="table-bordered" %)
258 |(% style="text-align:center; vertical-align:middle" %)[[image:image-20220611153214-3.png]]
259 |After the EEPROM is damaged, the servo will have an non resettable fault!
260
261 **(5) Set the high and low order of the 32-bit monitoring data**
262
263 Part of the monitoring volume is 32-bit length and occupies 2 consecutive bias numbers. The user needs to set the order of the data high bit and low bit correctly, otherwise it will cause data reading and writing errors!
264
265 For example, U0-54 (position within 1 circle of absolute encoder) occupies two consecutive offset numbers, which are 0x1E3D and 0x1E3E respectively. Assuming the value of U0-54 is 0x12345678, the correct data sequence bit should be 0x1E3D=0x5678 , 0x1E3E=0x1234 (little endian mode: low byte first, high byte behind.)
266
267 The description of related function codes are as follows.
268
269 (% class="table-bordered" %)
270 |(% style="text-align:center; vertical-align:middle; width:121px" %)**Function code**|(% style="text-align:center; vertical-align:middle; width:205px" %)**Name**|(% style="text-align:center; vertical-align:middle; width:187px" %)(((
271 **Setting method**
272 )))|(% style="text-align:center; vertical-align:middle; width:186px" %)(((
273 **Effective time**
274 )))|(% style="text-align:center; vertical-align:middle; width:130px" %)**Default value**|(% style="text-align:center; vertical-align:middle; width:132px" %)**Range**|(% style="text-align:center; vertical-align:middle; width:252px" %)**Definition**|(% style="text-align:center; vertical-align:middle; width:85px" %)**Unit**
275 |(% style="text-align:center; vertical-align:middle; width:121px" %)P12-02|(% style="text-align:center; vertical-align:middle; width:205px" %)Baud rate|(% style="text-align:center; vertical-align:middle; width:187px" %)(((
276 Operation setting
277 )))|(% style="text-align:center; vertical-align:middle; width:186px" %)(((
278 Effective immediately
279 )))|(% style="text-align:center; vertical-align:middle; width:130px" %)2|(% style="text-align:center; vertical-align:middle; width:132px" %)0 to 5|(% style="width:252px" %)(((
280 0-2400bps
281
282 1-4800bps
283
284 2-9600bps
285
286 3-19200bps
287
288 4-38400bps
289
290 5-57600bp
291 )))|(% style="text-align:center; vertical-align:middle; width:85px" %)-
292 |(% style="text-align:center; vertical-align:middle; width:121px" %)P12-03|(% style="text-align:center; vertical-align:middle; width:205px" %)Serial data format|(% style="text-align:center; vertical-align:middle; width:187px" %)(((
293 Operation setting
294 )))|(% style="text-align:center; vertical-align:middle; width:186px" %)(((
295 Effective immediately
296 )))|(% style="text-align:center; vertical-align:middle; width:130px" %)0|(% style="text-align:center; vertical-align:middle; width:132px" %)0 to 3|(% style="width:252px" %)(((
297 0: 1 stop bit, no parity
298
299 1: 1 stop bit, odd parity
300
301 2: 1 stop bit, even parity
302
303 3: 2 stop bits, no parity
304 )))|(% style="text-align:center; vertical-align:middle; width:85px" %)-
305 |(% style="text-align:center; vertical-align:middle; width:121px" %)P12-04|(% style="text-align:center; vertical-align:middle; width:205px" %)Modbus communication data is written into EEPROM|(% style="text-align:center; vertical-align:middle; width:187px" %)(((
306 Operation setting
307 )))|(% style="text-align:center; vertical-align:middle; width:186px" %)(((
308 Effective immediately
309 )))|(% style="text-align:center; vertical-align:middle; width:130px" %)0|(% style="text-align:center; vertical-align:middle; width:132px" %)0 to 1|(% style="width:252px" %)(((
310 0: Do not write to EEPROM, and do not store after power failure;
311
312 1: Write to EEPROM, power-down storage.
313 )))|(% style="text-align:center; vertical-align:middle; width:85px" %)-
314
315 = **Modbus communication variable address and value** =
316
317 == **Variable address description** ==
318
319 Modbus registers are divided into two categories:
320
321 1. The first category is servo function code parameters (address: 0x0001 to 0x0D08), this part of the register is readable and writable (that is, 0x03 and 0x06 are supported);
322 1. The second category is the monitoring volume of the servo (address: 0x1E01 to 0x2010), this part of the register is only readable (0x03 function is supported).
323
324 **Servo function code representation: PXX-YY.**
325
326 * XX: represents the function code group number,
327 * YY: represents the bias within the function code group;;
328
329 During servo communication, the communication address of the function code is a 16-bit address, which is composed of the function code group number (high 8 bits) + group bias (low 8 bits), for example, the Modbus address corresponding to P12-1 (servo address) is 0x0C01.
330
331 **Servo monitor volume representation: Uxx-yy.**
332
333 * xx: represents the monitoring volume group number,
334 * yy: represents the bias within the monitoring volume group;
335
336 During Modbus communication, the starting address of the monitoring volume is 0x1E01, and the conversion relationship of the address is similar to the representation way of the function code.
337
338 For example, U0-01 (servo status) corresponds to the Modbus address is 0x1E01.
339
340 In order to facilitate actual use, this manual provides both decimal and hexadecimal address identification, it is shown in the following table:
341
342 (% class="table-bordered" %)
343 |(% style="text-align:center; vertical-align:middle" %)**Function code**|(% style="text-align:center; vertical-align:middle" %)(((
344 **Modbus address**
345
346 **(Hexadecimal)**
347 )))|(% style="text-align:center; vertical-align:middle" %)(((
348 **Modbus address**
349
350 **(Decimal)**
351 )))|(% style="text-align:center; vertical-align:middle" %)**Category**|(% style="text-align:center; vertical-align:middle" %)**Name**
352 |(% style="text-align:center; vertical-align:middle" %)P0-1|(% style="text-align:center; vertical-align:middle" %)0x0001|(% style="text-align:center; vertical-align:middle" %)1|(% style="text-align:center; vertical-align:middle" %)Basic settings|(% style="text-align:center; vertical-align:middle" %)Control mode
353
354 For detailed parameter addresses, please refer to __[["11.1 Lists of parameters".>>https://docs.we-con.com.cn/bin/view/Servo/2.%20User%20Manual/06%20VD2%20SA%20Series%20Servo%20Drives%20Manual%20%28Full%20V1.1%29/11%20Appendix/#HListsofparameters]]__
355
356 == **Variable value type description** ==
357
358 When writing function codes with signed numbers, you need to convert the pre-written data into hexadecimal complements. The conversion rules are as follows:
359
360 1. The data is positive or 0: complement code = original code
361 1. The data is negative: complement code = 0xFFFF-absolute value of data + 0x0001
362
363 For example:
364
365 * The 16-bit signed positive number +100, the original code is 0x0064, and the complement is: 0x0064.
366 * The 16-bit signed positive number -100, its hexadecimal complement is: 0xFFFF-0x0064 + 0x0001 = 0xFF9C.
367 * If it is an unsigned number, just pass it directly according to its original code. For example, if the decimal number is 32768, write 0x8000 directly.
368
369 == **Numerical unit description** ==
370
371 Some values have units and decimals, such as 0.1%, 0.1Hz, 0.01ms, and the corresponding value conversion is required when reading and writing. The methods are as follows:
372
373 1. When the unit is 0.1%: 1 represents 0.1%, 10 represents 1.0%, 1000 represents 100.0%. Therefore, writing 1000 means setting to 100.0%; on the contrary, if it is reading 1000, it means that the value is 100.0%;
374 1. When the unit is 0.01ms: 1 means 0.01ms, 50 means 0.5ms, 10000 means 100ms. Therefore, writing 1000 means setting to 10.00ms; on the contrary, if 1000 is read, it means 10.00ms;
375
376 The other units can be deduced by this, and integer remains unchanged.