Wiki source code of 1 Communication
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | = **1 General communication with PLC** = | ||
2 | |||
3 | == **1.1 Modbus RTU** == | ||
4 | |||
5 | ==== V-Box as master: ==== | ||
6 | |||
7 | Select the protocol “(% class="mark" %)Modbus RTU Slave(All Fuction)”, | ||
8 | |||
9 | (% class="mark" %)then set BaudRate,Port,Stop Bit,Data Bit,Parity | ||
10 | |||
11 | (% style="text-align:center" %) | ||
12 | [[image:1624342015914-382.png||height="486" width="800" class="img-thumbnail"]] | ||
13 | |||
14 | ==== V-Box as slave: ==== | ||
15 | |||
16 | Select the protocol “(% class="mark" %)**Modbus RTU Master”**, | ||
17 | |||
18 | (% class="mark" %)then set BaudRate,Port,Stop Bit,Data Bit,Parity | ||
19 | |||
20 | (% style="text-align:center" %) | ||
21 | [[image:1624348709622-711.png||queryString="width=809&height=493" height="488" width="800" class="img-thumbnail"]] | ||
22 | |||
23 | **Address List** | ||
24 | |||
25 | (% class="table-bordered" %) | ||
26 | |**Type**|**Register**|**Function code & Description** | ||
27 | |(% rowspan="11" %)Word|(% rowspan="3" %)3|04 (read input register: read current binary value in one or more input registers) | ||
28 | |06 (write single register: write a binary value to a holding register) | ||
29 | |10 (write values to multiple addresses ) | ||
30 | |(% rowspan="3" %)4|03 (read holding register: read current binary value in one or more holding registers) | ||
31 | |06 (write single register: write a binary value to a holding register) | ||
32 | |10 (write values to multiple addresses ) | ||
33 | |(% rowspan="3" %)W6|03 (read holding register: read current binary value in one or more holding registers) | ||
34 | |06 (write single register: write a binary value to a holding register) | ||
35 | |10 (write values to multiple addresses ) | ||
36 | |(% rowspan="2" %)W16|03 (read holding register: read current binary value in one or more holding registers) | ||
37 | |10 (write values to multiple addresses ) | ||
38 | |(% rowspan="11" %)Bit|(% rowspan="3" %)0|01 (Read coil state) | ||
39 | |05 (Force a single coil to force the on/off state of a logic coil) | ||
40 | |0F (Write multiple bits, ie write continuously) | ||
41 | |(% rowspan="3" %)1|02 (Read the input state) | ||
42 | |05 (Force a single coil to force the on/off state of a logic coil) | ||
43 | |0F (Write multiple bits) | ||
44 | |(% rowspan="3" %)W5|01 (Read coil state to obtain the current state of a set of logic coils) | ||
45 | |05 (Force a single coil to force the on/off state of a logic coil) | ||
46 | |0F (Write multiple bits) | ||
47 | |(% rowspan="2" %)W15|01 (Read coil state to obtain the current state of a set of logic coils) | ||
48 | |0F (Write multiple bits) | ||
49 | |||
50 | == **1.2 WECON LX3V PLC** == | ||
51 | |||
52 | Select the protocol (% class="mark" %)"WECON LX3V" | ||
53 | |||
54 | (% class="mark" %)then set BaudRate,Port,Stop Bit,Data Bit,Parity same with PLC. | ||
55 | |||
56 | (% class="mark" %)WECON PLC RS422 port,default communication parameters is 9600,1,7,ZEVEN | ||
57 | |||
58 | (% style="text-align:center" %) | ||
59 | [[image:1624348978863-771.png||queryString="width=812&height=494" height="487" width="800" class="img-thumbnail"]] | ||
60 | |||
61 | == **1.3 Siemens S7-1200** == | ||
62 | |||
63 | [[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]] | ||
64 | |||
65 | == **1.4 Allen-Bradley Ethernet DF1** == | ||
66 | |||
67 | [[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]] | ||
68 | |||
69 | == **1.5 Communication with S7-200 Smart** == | ||
70 | |||
71 | [[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]] | ||
72 | |||
73 | == **1.6 Delta DVP Ethernet** == | ||
74 | |||
75 | [[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]] | ||
76 | |||
77 | == **1.7 Delta DVP Serial Protocol** == | ||
78 | |||
79 | [[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]] | ||
80 | |||
81 | == **1.8 Communicate with Mitsubishi FX Series** == | ||
82 | |||
83 | [[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]] | ||
84 | |||
85 | == **1.9 Communicate with VB inverter** == | ||
86 | |||
87 | [[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]] | ||
88 | |||
89 | == **1.10 Omron CP1 Series** == | ||
90 | |||
91 | [[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]] | ||
92 | |||
93 | == **1.11 Omron CJ2M** == | ||
94 | |||
95 | [[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]] | ||
96 | |||
97 | = **2 User define protocol** = | ||
98 | |||
99 | More serial port script instruction,please refer manual:[[Lua script function>>doc:V-BOX.V-Net.1\.User 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]] | ||
100 | |||
101 | == **2.1 Read the temperature sensor.** == | ||
102 | |||
103 | the commond format is as beliow: | ||
104 | |||
105 | Send commond:01 03 02 00 00 03 04 73 | ||
106 | |||
107 | Feedback commond:01 03 06 07 F7 0E 7E B7 35 | ||
108 | |||
109 | Temperature data:07 F7,desimal = 2039,it means temperature is 20.39℃. | ||
110 | |||
111 | Humidity data:0E 7E,desimal = 3710, it mean humidity is 37.10%RH. | ||
112 | |||
113 | Script Demo: | ||
114 | |||
115 | ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-- | ||
116 | |||
117 | function com.main() | ||
118 | ~-~-dosomething | ||
119 | openPlc() | ||
120 | end | ||
121 | function openPlc() | ||
122 | if obj then | ||
123 | ~-~- If the serial port opened,then read/write data by timing | ||
124 | |||
125 | local wri = string.pack(">HHHH",0X0103,0X0200,0X0003,0X0473) | ||
126 | local arr = reading(wri) | ||
127 | ~-~- if arr ~~=nil then | ||
128 | ~-~- if arr[1] ==33 and arr[2] ==v[2][2] and arr[3] ==v[2][3] then | ||
129 | ~-~- local A = arr[4]*256 | ||
130 | ~-~- local B = A+arr[5] | ||
131 | ~-~- addr_setword(v[1],B) | ||
132 | ~-~- end | ||
133 | ~-~- end | ||
134 | |||
135 | else | ||
136 | ~-~-judge if there is a serial object, then initialize and open the serial port | ||
137 | local configs = { | ||
138 | name = 'COM1', ~-~-Select serial port COM1 | ||
139 | mode = 485, ~-~-Communication mode RS485 | ||
140 | baud_rate = 9600, ~-~-Baud rate 9600 | ||
141 | stop_bit = 1, ~-~-stop bit 1 | ||
142 | data_len = 8, ~-~-data length 8 | ||
143 | check_bit = 'NONE', ~-~-no parity | ||
144 | |||
145 | } | ||
146 | obj,err = serial.open(configs) ~-~-Open serial port | ||
147 | if not obj then | ||
148 | print("serial open :failed", err) ~-~- Open serial port failed | ||
149 | end | ||
150 | end | ||
151 | end | ||
152 | |||
153 | function reading(wri) | ||
154 | obj:flush() ~-~- Clear serial port | ||
155 | obj:write(wri) ~-~- Write serial port | ||
156 | print(string.byte(wri,1,#wri)) | ||
157 | obj:flush() ~-~- Clear serial port | ||
158 | local readed =obj:read(9,1000) ~-~- Read 9 bytes, timeout 1 second | ||
159 | if readed and readed ~~= "" then | ||
160 | ~-~- judge that a non-empty string is read | ||
161 | print(string.byte(readed,1,#readed)) | ||
162 | a={string.byte(readed, 1, #readed)} | ||
163 | if nil ~~= a[4] and nil ~~= a[5] and nil ~~= a[6] and nil ~~= a[7] then | ||
164 | a1=a[4]*256+a[5] | ||
165 | a2=a[6]*256+a[7] | ||
166 | addr_setword("@HDW0",a1) | ||
167 | addr_setword("@HDW1",a2) | ||
168 | end | ||
169 | return {string.byte(readed, 1, #readed)} | ||
170 | elseif readed == nil then | ||
171 | print("serial read err:", err or "") | ||
172 | return nil | ||
173 | end | ||
174 | return nil | ||
175 | end | ||
176 | |||
177 | ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- |