Wiki source code of 1 Communication

Version 91.1 by Ben on 2022/08/24 16:22

Show last authors
1 = (% style="color:inherit; font-family:inherit" %)**WECON PLC**(%%) =
2
3 == **LX3V Serial Protocol** ==
4
5 Supported series: WECON LX2V/ LX2E/ LX3V/LX3VP/LX3VE/LX3VM
6
7 **1)PLC Settings**
8
9 |**Item**|**Settings**|**Note**
10 |Protocol|WECON LX2V/ LX2E/ LX3V/LX3VP/LX3VE/LX3VM|
11 |Connection|RS422/RS485|
12 |Baud rate|9600|
13 |Data bit|7|
14 |Parity|EVEN|
15 |Stop bit|1|
16 |PLC station No.|1|
17
18 **2)PLC Address List**
19
20 |**Type**|**Device registers**|**HMI registers**|**Format**|**Range**|**Note**
21 |(% rowspan="6" %)Bit|X|X|Xo|0~~303237|
22 |Y|Y|Y o|0~~303237|
23 |M|M|M d|0~~99999|
24 |T|T|T d|0~~99999|
25 |C|C|C d|0~~99999|
26 |S|S|S d|0~~99999|
27 |(% rowspan="8" %)Word|X|X|Xo|0~~303237|
28 |Y|Y|Y o|0~~303237|
29 |M|M|M d|0~~99999|
30 |T|T|T d|0~~99999|
31 |C|C|C d|0~~199|
32 |D|D|D d|0~~7999|
33 |S|S|S d|0~~99999|
34 |SD|SD|SD d|8000~~9999|
35
36 **3)Configure the communication protocol**
37
38 [[image:image-20220824152217-1.png]]
39
40 **4)hardware connection**
41
42 * **RS422**
43
44 [[image:image-20220824120614-5.png]]
45
46 * **RS485**
47
48 [[image:image-20220824120524-3.png]]
49
50 == **LX5V Serial Protocol** ==
51
52 This example introduces the establishment of serial port communication between V-BOX and LX5V. It is introduced through three parts: PLC software configuration, V-BOX software configuration, and hardware wiring.
53
54 **Software configuration of PLC**
55
56 **1)PLC programming software**
57
58 [[image:image-20220824142722-1.png]]
59
60 **2)New PLC project**
61
62 Click New Project and select the PLC model.
63
64 [[image:image-20220822172623-2.png]]
65
66 **3)Set Serial port parameters**
67
68 Follow the steps below to configure Serial port parameters
69
70 Baud rate:115200
71
72 Data bit:8
73
74 Stop bit:1
75
76 Parity:No verification
77
78 [[image:image-20220822172657-3.png]]
79
80 **4)Registers list**
81
82 [[image:image-20220822172730-4.png]]
83
84 **V-BOX software configuration**
85
86 **1)V-BOX programming software**
87
88 [[image:image-20220824104124-1.png]]
89
90 **2)V-NET add device**
91
92 Open V-NET client software → add device. As shown in the figure below, click "+" in the order of steps, and the Add Device window will pop up, enter the access key, password and remark, to add the device.
93
94 [[image:image-20220824104909-2.png]]
95
96 **3)Set communication port parameters**
97
98 Click the communication configuration button on the left to find the communication protocol with LX5V. After selecting the protocol, configure the communication parameters of the COM port.
99
100 Baud rate:115200
101
102 Data bit:8
103
104 Stop bit:1
105
106 Parity:No verification
107
108 [[image:image-20220824142130-1.png]]
109
110 **hardware connection**
111
112 **1)Hardware wiring diagram**
113
114 [[image:image-20220824142546-2.png]]
115
116 [[image:image-20220824142643-3.png]]
117
118 The above are all the steps for establishing serial port communication between V-BOX with LX5V PLC.
119
120 == **LX5V-N Ethernet protocol** ==
121
122 This example introduces the establishment of Ethernet communication between V-BOX and LX5V-N. It is introduced through three parts: PLC software configuration, V-BOX software configuration, and hardware wiring.
123
124 **Software configuration of PLC**
125
126 **1)PLC programming software**
127
128 [[image:image-20220824142722-1.png]]
129
130 **2)New PLC project**
131
132 Click New Project and select the PLC model.
133
134 [[image:image-20220824143613-1.png]]
135
136 **3)Set Ethernet port parameters**
137
138 Follow the steps below to configure Ethernet parameters
139
140 PLC IP address:192.168.39.200
141
142 default gateway:192.168.39.1
143
144 subnet mask:255.255.255.0
145
146 [[image:image-20220824143744-2.png]]
147
148 **4)Registers list**
149
150 [[image:image-20220822172730-4.png]]
151
152 (% id="cke_bm_22473S" style="display:none" %)** **(%%)**V-BOX software configuration**
153
154 **1)V-BOX programming software**
155
156 [[image:image-20220824104124-1.png]]
157
158 **2)V-NET add device**
159
160 Open V-NET client software → add device. As shown in the figure below, click "+" in the order of steps, and the Add Device window will pop up, enter the access key, password and remark, to add the device.
161
162 [[image:image-20220824104909-2.png]]
163
164 **3)Set communication Protocol**
165
166 Click Communication to configure the LX5V plc protocol and IP address.
167
168 [[image:image-20220824144619-1.png]]
169
170 **hardware connection**
171
172 **1)Hardware wiring diagram**
173
174 [[image:image-20220824151127-2.png]]
175
176 The above are all the steps for establishing ethernet communication between V-BOX with LX5V PLC.
177
178 = **Rockwell PLC** =
179
180 == (% id="cke_bm_5621S" style="display:none" %)** **(%%)**MicroLogix Protocol** ==
181
182 MicroLogix 1000/1100/1200/1400/1500; SLC 5/03 5/04 5/05 PLC-5
183
184 **1)PLC Settings**
185
186 |**Item**|**Settings**|**Note**
187 |Protocol|Allen-Bradlley DF1 Advanced|
188 |Connection|RS232|
189 |Baud rate|19200|
190 |Data bit|8|
191 |Parity|None|
192 |Stop bit|1|
193 |PLC station No.|1|
194
195 **2)Address List**
196
197 |**Type**|**Device registers**|**Format**|**Range**|**Note**
198 |(% rowspan="5" %)Bit|I|I d.d|0.0~~255.15|
199 |O|O d.d|0.0~~255.15|
200 |B|B nnhh.dd|0.0~~ffff.15|nn: block number (hex)
201 |S|S d.d|0.0~~255.15|
202 |N|N nnhh.dd|0.0~~ffff.15|nn: block number (hex)
203 |(% rowspan="9" %)Word|S|S d|0~~255|
204 |TS|TS nnhh|0~~ffff|(% rowspan="8" %)nn: block number (hex)
205 |TP|TP nnhh|0~~ffff
206 |CS|CS nnhh|0~~ffff
207 |CP|CP nnhh|0~~ffff
208 |N|N nnhh|0~~ffff
209 |C|C nnhh|0~~ffff
210 |T|T nnhh|0~~ffff
211 |R|R nnhh|0~~ffff
212
213 **3)Configure the communication protocol**[[image:image-20220824152928-1.png]]
214
215 **4)Configure the communication protocol**
216
217 [[image:image-20220824110724-1.png]]
218
219 == **CompactLogix Protocol** ==
220
221 (((
222 (((
223 This example introduces the establishment of Ethernet communication between Wecon V-box and CompactLogix L1769. It is introduced through three parts: PLC software configuration, V-box software configuration, and hardware wiring.
224 )))
225 )))
226
227 (((
228 (% class="wikigeneratedid" id="HSoftwareconfigurationofPLC" %)
229 (((
230 **Software configuration of PLC**
231 )))
232
233 (% class="wikigeneratedid" id="HPLCprogrammingsoftware" %)
234 (((
235 **1)PLC programming software**
236 )))
237
238 [[image:image-20220714162600-1.png]]
239
240 (% class="wikigeneratedid" id="HNewPLCproject" %)
241 (((
242 **2)New PLC project**
243 )))
244
245 (((
246 (((
247 Click New Project and select the PLC model.
248 )))
249 )))
250
251 (((
252 [[image:image-20220714162617-2.png]]
253 )))
254
255 (((
256
257
258 [[image:image-20220714162652-4.png]]
259 )))
260
261 (((
262 (% class="wikigeneratedid" id="HSetEthernetparameters" %)
263 (((
264 **3)Set Ethernet parameters**
265 )))
266
267 (((
268 (((
269 (((
270 Follow the steps below to configure Ethernet parameters
271 )))
272 )))
273 )))
274
275 (((
276 (((
277 (((
278 PLC IP address:192.168.40.101
279 )))
280 )))
281 )))
282
283 (((
284 (((
285 (((
286 default gateway:192.168.40.1
287 )))
288 )))
289 )))
290
291 (((
292 (((
293 (((
294 subnet mask:255.255.255.0
295 )))
296 )))
297 )))
298
299 (((
300 [[image:image-20220714162713-5.png]]
301 )))
302
303 (((
304
305 )))
306
307 (((
308 (% class="wikigeneratedid" id="HV-boxsoftwareconfiguration" %)
309 (((
310 **V-box software configuration**
311 )))
312
313 (% class="wikigeneratedid" id="HV-boxprogrammingsoftware" %)
314 (((
315 **1)V-box programming software**
316 )))
317
318 (% class="wikigeneratedid" %)
319 (((
320 [[image:image-20220714162725-6.png]]
321 )))
322
323 (% class="wikigeneratedid" id="HConfiguredevicenetwork" %)
324 (((
325 **2)Configure device network**
326 )))
327
328 (((
329 (((
330 (((
331 Click the Configuration Download button[[image:image-20220714115308-8.png||height="22" width="15"]], It is used to configure the device network, update the device time, obtain the device machine code, check the device network, etc.
332 )))
333
334 (((
335 In the example below, the IP address of V-Box is configured as 192.168.40.66, and the networking method is 4G connection
336 )))
337 )))
338 )))
339
340 (((
341 (((
342 [[image:image-20220714162737-7.png]]
343
344
345 )))
346 )))
347 )))
348 )))
349
350 (% class="wikigeneratedid" id="HV-netadddevice" %)
351 (((
352 **3)V-net add device**
353 )))
354 )))
355
356 (((
357 (((
358 Open VNET client software → add device. As shown in the figure below, click "+" in the order of steps, and the Add Device window will pop up, enter the device machine code/product code, device password, and device alias to add the device.   [[image:image-20220714162752-8.png]]
359 )))
360
361
362 )))
363
364 (((
365 (% class="wikigeneratedid" id="HSetcommunicationportparameters" %)
366 (((
367 **4)Set communication port parameters**
368 )))
369
370 (((
371 In the device list on the left, find the Vbox that communicates with the L1769, click it and select configuration,communication port configuration in the main interface to add a communication protocol.
372 )))
373
374 [[image:image-20220714162805-9.png]]
375 )))
376
377 (((
378 The communication port is selected as Ethernet, the device type is selected as Allen-Bradley, the protocol is Allen-BradleyMicro850_FreeTag EthemetIP, and the IP is the internal IP address of the L1769 PLC. The port number is 44818.
379 )))
380
381 [[image:image-20220714162814-10.png]]
382
383
384 (((
385 After adding the protocol Allen-BradleyMicro850_FreeTag, click[[image:image-20220714143838-2.png]]icon add label.[[image:image-20220714162827-11.png]]
386 )))
387
388 (% class="wikigeneratedid" id="HEngineeringproduction" %)
389 (((
390 **5)Engineering production**
391 )))
392
393 (% class="wikigeneratedid" %)
394 (((
395 (((
396 According to your own needs, use the V-NET client software to edit the required data monitoring points.
397 )))
398
399 [[image:image-20220714162841-12.png]]
400
401
402 (((
403 **hardware connection**
404 )))
405 )))
406
407 (((
408 **1)Hardware wiring diagram**
409 )))
410
411 (((
412 (((
413 This example introduces Wecon V-box with Allen-Bradley L1769 PLC to establish communication through Ethernet. The connection diagram is as follows:
414 )))
415
416 [[image:image-20220714162856-13.png]]
417
418 (((
419 The above are all the steps for establishing Ethernet communication between Wecon V-box with CompactLogix L1769 PLC.
420
421
422 == **Allen-Bradley Ethernet DF1** ==
423
424 [[download link>>https://ftp.we-con.com.cn/Download/WIKI/V-BOX/Demo/%E9%80%9A%E8%AE%AF%E6%A1%88%E4%BE%8B/Allen-Bradley%20Ethernet%20DF1.zip]]
425
426
427 = **Siemens PLC** =
428
429 == **S7-200 Smart Ethernet Protocol** ==
430
431 [[download link>>https://ftp.we-con.com.cn/Download/WIKI/V-BOX/Demo/%E9%80%9A%E8%AE%AF%E6%A1%88%E4%BE%8B/Communication%20with%20S7-200%20Smart.zip]]
432
433
434 == ** S7-300 Ethernet Protocol** ==
435
436 Supported Series: Siemens S7-300 series PLC
437
438 **1)PLC Setting**
439
440 |**Items**|**Settings**|**Note**
441 |Protocol|Simens S7-300 Ethernet|
442 |Connection|Ethernet|
443 |Port No.|102|
444 |PLC station No.|2|Need to be same as the PLC setting
445
446 **2)Address List**
447
448 |**Type**|**Device register**|**HMI register**|**Format**|**Range**|**Note**
449 |(% rowspan="4" %)Bit|I|I|I ddddd.o|0.0~~99999.7|
450 |Q|Q|Q ddddd.o|0.0~~99999.7|
451 |M|M|M ddddd.o|0.0~~99999.7|
452 |DB0.DB~~DB99.DB|DBxDBD|DBxDB nndddd.o|0.0~~99999999.7|(((
453 nn: block number;
454
455 dddd: address;
456 )))
457 |(% rowspan="8" %)Word|I|IW|IW ddddd|0~~99999|
458 |Q|QW|QW ddddd|0~~99999|
459 |(% rowspan="3" %)M|MB|MB ddddd|0~~99999|
460 |MW|MW ddddd|0~~99999|(((
461 MW0=MB(0~~1)
462
463 MW2=MB(2~~3)
464
465 Address value is a multiple of 2
466 )))
467 |MD|MD ddddd|0~~99999|(((
468 MD0=MB(0~~3)
469
470 MD4=MB(4~~7)
471
472 Address value is a multiple of 4
473 )))
474 |(% rowspan="3" %)DB0.DB~~DB99.DB|DBxDBB|DBxDBB nndddd|0~~99999999|(% rowspan="3" %)(((
475 nn: block number;
476
477 dddd: address
478 )))
479 |DBxDBW|DBxDBW nndddd|0~~99999999
480 |DBxDBD|DBxDBD nndddd|0~~99999999
481
482 **3)Configure the communication protocol**
483
484 [[image:image-20220824161345-1.png]]
485
486 **Cable Wiring**
487
488 [[image:image-20220824161450-2.png]]
489 )))
490 )))
491
492 == **S7-1200 Ethernet Protocol** ==
493
494 [[download link>>https://ftp.we-con.com.cn/Download/WIKI/V-BOX/Demo/%E9%80%9A%E8%AE%AF%E6%A1%88%E4%BE%8B/Communicate%20with%20S7-1200%20via%20Ehernet.zip]]
495
496 == ** S7-XXX Ethernet** ==
497
498 Supported Series: Siemens S7-400, S7-1200 and S7-1500
499
500 **HMI Setting**
501
502 |=**Items**|=**Settings**|=**Note**
503 |Protocol|Siemens S7-XXX|
504 |Connection|Ethernet|
505 |Port No.|102|
506 |Device No.|Slot (Default 1 for S7-1500/ 2 for S7-1200/ 3 for S7-400)|
507 |HMI No.|Rack (Default as 0)|
508
509 Slot from TIA Portal is equal to Device No. of PIStudio
510
511 Rack from TIA Portal is equal to HMI No. of PIStudio
512
513 (% style="text-align:center" %)
514 [[image:S7-XXXCommunication Settings.png]]
515
516 (% style="text-align:center" %)
517 [[image:S7-XXXCommunication SettingsPIStudio.png||alt="S7-XXXCommunication SettingsPI.png"]]
518
519 **Address List**
520
521 |**Type**|**Device register**|**HMI register**|**Format**|**Range**|**Note**
522 |(% rowspan="4" %)Bit|I|I|M d.o|(((
523 d:0~-~-9999
524
525 o:0-7
526 )))|
527 |Q|Q|Q d.o|(((
528 d:0~-~-9999
529
530 o:0-7
531 )))|
532 |M|M|M d.o|(((
533 d:0~-~-9999
534
535 o:0-7
536 )))|
537 |DB0.DB-DB99.DB|DBxDB|DBxDBnndddd.o|(((
538 nn:0-9999,
539
540 dddd:0-9999,
541
542 o:0-7
543 )))|(((
544 nn:DB No.
545
546 dddd:address value
547
548 o: digit address
549 )))
550 |(% rowspan="8" %)Word|M|MB|MB d|d:0-99999|
551 |M|MW|MW d|d:0-99999|(((
552 MW0=MB(0~~1)
553
554 MW2=MB(2~~3)
555
556 Address value is a multiple of 2
557 )))
558 |M|MD|MD d|d:0-99999|(((
559 MD0=MB(0~~3)
560
561 MD4=MB(4~~7)
562
563 Address value is a multiple of 4
564 )))
565 |I|IW|IW d|d:0-99999|
566 |Q|QW|QW d|d:0-99999|
567 |DB0.DB-DB99.DB|DBxDBB|DBxDBBnndddd|(((
568 nn:0-9999
569
570 dddd:0-9999
571 )))|(((
572 nn:DB No.
573
574 dddd:address value
575 )))
576 |DB0.DB-DB99.DB|DBxDBW|DBxDBWnndddd|(((
577 nn:0-9999
578
579 dddd:0-9999
580 )))|(((
581 nn:DB No.
582
583 dddd:address value
584
585 Address value is a multiple of 2
586 )))
587 |DB0.DB-DB99.DB|DBxDBD|DBxDBDnndddd|(((
588 nn:0-9999
589
590 dddd:0-9999
591 )))|(((
592 nn:DB No.
593
594 dddd:address value
595
596 Address value is a multiple of 4
597 )))
598
599 **Communication Settings**
600
601 Enable HMI Ethernet in [Project Settings];
602
603 (% style="text-align:center" %)
604 [[image:12.PLC Protocol_html_43b671f18153910d.png||data-xwiki-image-style-alignment="center" height="107" width="405" class="img-rounded img-thumbnail"]]
605
606 Set PLC IP in [Device IP] settings;
607
608 (% style="text-align:center" %)
609 [[image:12.PLC Protocol_html_f7b3eb9abda33833.png||data-xwiki-image-style-alignment="center" height="190" width="554" class="img-thumbnail"]]
610
611 **✎Note:**
612
613 * The S7-1200 supports simultaneous connection of three devices, so the driver supports simultaneous access to PLC by three touch screens.
614 * HMI access PLC, use S7 protocol, access PLC TSAP 02.01 (s7-1200 PROFINET interface only supports three connections, the default support), detailed reference to the system manual of S7-1200.
615
616 **Cable Wiring**
617
618 {{id name="OLE_LINK8"/}}
619
620
621 = **Mitsubishi PLC** =
622
623 == **Mitsubishi FX Series Protocol** ==
624
625 [[download link>>url:https://ftp.we-con.com.cn/Download/WIKI/V-BOX/Demo/%E9%80%9A%E8%AE%AF%E6%A1%88%E4%BE%8B/Communicate%20with%20Mitsubishi%20FX%20Series.zip]]
626
627 = **Delta PLC** =
628
629 == **DVP Ethernet Protocol** ==
630
631 [[download link>>https://ftp.we-con.com.cn/Download/WIKI/V-BOX/Demo/%E9%80%9A%E8%AE%AF%E6%A1%88%E4%BE%8B/Delta%20DVP%20Ethernet.zip]]
632
633 == **DVP Serial Protocol** ==
634
635 [[download link>>https://ftp.we-con.com.cn/Download/WIKI/V-BOX/Demo/%E9%80%9A%E8%AE%AF%E6%A1%88%E4%BE%8B/Delta%20DVP%20Ethernet.zip]]
636
637 = **Omron PLC** =
638
639 == **CP1 Series Protocol** ==
640
641 [[download link>>https://ftp.we-con.com.cn/Download/WIKI/V-BOX/Demo/%E9%80%9A%E8%AE%AF%E6%A1%88%E4%BE%8B/Communication%20with%20ORMON%20CP1%20Series.zip]]
642
643 == **CJ2M Series Protocol** ==
644
645 [[download link>>https://ftp.we-con.com.cn/Download/WIKI/V-BOX/Demo/%E9%80%9A%E8%AE%AF%E6%A1%88%E4%BE%8B/Communication%20with%20ORMON%20CJ2M.zip]]
646
647 = **Modbus RTU** =
648
649 == **V-BOX as master** ==
650
651 Select the protocol “(% class="mark" %)Modbus RTU Slave(All Fuction)”,
652
653 (% class="mark" %)then set BaudRate,Port,Stop Bit,Data Bit,Parity
654
655 (% style="text-align:center" %)
656 [[image:1624342015914-382.png||height="486" width="800" class="img-thumbnail"]]
657
658 == **V-BOX as slave** ==
659
660 Select the protocol “(% class="mark" %)**Modbus RTU Master”**,
661
662 (% class="mark" %)then set BaudRate,Port,Stop Bit,Data Bit,Parity
663
664 (% style="text-align:center" %)
665 [[image:1624348709622-711.png||queryString="width=809&height=493" height="488" width="800" class="img-thumbnail"]]
666
667 **Address List**
668
669 (% class="table-bordered" %)
670 |**Type**|**Register**|**Function code & Description**
671 |(% rowspan="11" %)Word|(% rowspan="3" %)3|04 (read input register: read current binary value in one or more input registers)
672 |06 (write single register: write a binary value to a holding register)
673 |10 (write values to multiple addresses )
674 |(% rowspan="3" %)4|03 (read holding register: read current binary value in one or more holding registers)
675 |06 (write single register: write a binary value to a holding register)
676 |10 (write values to multiple addresses )
677 |(% rowspan="3" %)W6|03 (read holding register: read current binary value in one or more holding registers)
678 |06 (write single register: write a binary value to a holding register)
679 |10 (write values to multiple addresses )
680 |(% rowspan="2" %)W16|03 (read holding register: read current binary value in one or more holding registers)
681 |10 (write values to multiple addresses )
682 |(% rowspan="11" %)Bit|(% rowspan="3" %)0|01 (Read coil state)
683 |05 (Force a single coil to force the on/off state of a logic coil)
684 |0F (Write multiple bits, ie write continuously)
685 |(% rowspan="3" %)1|02 (Read the input state)
686 |05 (Force a single coil to force the on/off state of a logic coil)
687 |0F (Write multiple bits)
688 |(% rowspan="3" %)W5|01 (Read coil state to obtain the current state of a set of logic coils)
689 |05 (Force a single coil to force the on/off state of a logic coil)
690 |0F (Write multiple bits)
691 |(% rowspan="2" %)W15|01 (Read coil state to obtain the current state of a set of logic coils)
692 |0F (Write multiple bits)
693
694 == **communicate with VB inverter** ==
695
696 [[download link>>url:https://ftp.we-con.com.cn/Download/WIKI/V-BOX/Demo/%E9%80%9A%E8%AE%AF%E6%A1%88%E4%BE%8B/Communicate%20with%20VB%20inverter.zip]]
697
698 = **User define protocol** =
699
700 More serial port script instruction,please refer manual:[[Lua script function>>doc:V-BOX.V-Net.Manual.04 Lua Script.01 Lua Functions.WebHome]]~-~-->[[3.Serial port operation>>url:http://docs.we-con.com.cn/wiki/vbox/view/HMI/04.Lua%20script%20%20function%20and%20operation/01.Lua%20script%20function/#H3.Serialportoperation]]
701
702 == **Read the temperature sensor.** ==
703
704 the commond format is as beliow:
705
706 Send commond:01 03 02 00 00 03 04 73
707
708 Feedback commond:01 03 06 07 F7 0E 7E B7 35
709
710 Temperature data:07 F7,desimal = 2039,it means temperature is 20.39℃.
711
712 Humidity data:0E 7E,desimal = 3710, it mean humidity is 37.10%RH.
713
714 Script Demo:
715
716 ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~--
717
718 function com.main()
719 ~-~-dosomething
720 openPlc()
721 end
722 function openPlc()
723 if obj then
724 ~-~- If the serial port opened,then read/write data by timing
725
726 local wri = string.pack(">HHHH",0X0103,0X0200,0X0003,0X0473)
727 local arr = reading(wri)
728 ~-~- if arr ~~=nil then
729 ~-~- if arr[1] ==33 and arr[2] ==v[2][2] and arr[3] ==v[2][3] then
730 ~-~- local A = arr[4]*256
731 ~-~- local B = A+arr[5]
732 ~-~- addr_setword(v[1],B)
733 ~-~- end
734 ~-~- end
735
736 else
737 ~-~-judge if there is a serial object, then initialize and open the serial port
738 local configs = {
739 name = 'COM1', ~-~-Select serial port COM1
740 mode = 485, ~-~-Communication mode RS485
741 baud_rate = 9600, ~-~-Baud rate 9600
742 stop_bit = 1, ~-~-stop bit 1
743 data_len = 8, ~-~-data length 8
744 check_bit = 'NONE', ~-~-no parity
745
746 }
747 obj,err = serial.open(configs) ~-~-Open serial port
748 if not obj then
749 print("serial open :failed", err) ~-~- Open serial port failed
750 end
751 end
752 end
753
754 function reading(wri)
755 obj:flush() ~-~- Clear serial port
756 obj:write(wri) ~-~- Write serial port
757 print(string.byte(wri,1,#wri))
758 obj:flush() ~-~- Clear serial port 
759 local readed =obj:read(9,1000) ~-~- Read 9 bytes, timeout 1 second
760 if readed and readed ~~= "" then 
761 ~-~- judge that a non-empty string is read
762 print(string.byte(readed,1,#readed))
763 a={string.byte(readed, 1, #readed)}
764 if nil ~~= a[4] and nil ~~= a[5] and nil ~~= a[6] and nil ~~= a[7] then
765 a1=a[4]*256+a[5]
766 a2=a[6]*256+a[7]
767 addr_setword("@HDW0",a1)
768 addr_setword("@HDW1",a2)
769 end
770 return {string.byte(readed, 1, #readed)}
771 elseif readed == nil then
772 print("serial read err:", err or "")
773 return nil
774 end
775 return nil
776 end
777
778 ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-