Changes for page User define
Last modified by Leo Wei on 2023/08/24 14:11
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -1,81 +1,0 @@ 1 -= {{id name="HUserdefine"/}}**User define** = 2 - 3 -More serial port script instruction,please refer manual:[[Lua script function>>url:https://docs.we-con.com.cn/bin/view/V-BOX/V-Net/Manual/04%20Lua%20Script/01%20Lua%20Functions/]]~-~-->[[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]] 4 - 5 -== {{id name="HReadthetemperatureA0sensor."/}}**Read the temperature sensor.** == 6 - 7 -the commond format is as beliow: 8 - 9 -Send commond:01 03 02 00 00 03 04 73 10 - 11 -Feedback commond:01 03 06 07 F7 0E 7E B7 35 12 - 13 -Temperature data:07 F7,desimal = 2039,it means temperature is 20.39℃. 14 - 15 -Humidity data:0E 7E,desimal = 3710, it mean humidity is 37.10%RH. 16 - 17 -Script Demo: 18 - 19 -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-- 20 - 21 -function com.main() 22 - ~-~-dosomething 23 - openPlc() 24 - end 25 - function openPlc() 26 - if obj then 27 - ~-~- If the serial port opened,then read/write data by timing 28 - 29 - local wri = string.pack(">HHHH",0X0103,0X0200,0X0003,0X0473) 30 - local arr = reading(wri) 31 - ~-~- if arr ~~=nil then 32 - ~-~- if arr[1] ==33 and arr[2] ==v[2][2] and arr[3] ==v[2][3] then 33 - ~-~- local A = arr[4]*256 34 - ~-~- local B = A+arr[5] 35 - ~-~- addr_setword(v[1],B) 36 - ~-~- end 37 - ~-~- end 38 - 39 - else 40 - ~-~-judge if there is a serial object, then initialize and open the serial port 41 - local configs = { 42 - name = 'COM1', ~-~-Select serial port COM1 43 - mode = 485, ~-~-Communication mode RS485 44 - baud_rate = 9600, ~-~-Baud rate 9600 45 - stop_bit = 1, ~-~-stop bit 1 46 - data_len = 8, ~-~-data length 8 47 - check_bit = 'NONE', ~-~-no parity 48 - 49 - } 50 - obj,err = serial.open(configs) ~-~-Open serial port 51 - if not obj then 52 - print("serial open :failed", err) ~-~- Open serial port failed 53 - end 54 - end 55 - end 56 - 57 - function reading(wri) 58 - obj:flush() ~-~- Clear serial port 59 - obj:write(wri) ~-~- Write serial port 60 - print(string.byte(wri,1,#wri)) 61 - obj:flush() ~-~- Clear serial port 62 - local readed =obj:read(9,1000) ~-~- Read 9 bytes, timeout 1 second 63 - if readed and readed ~~= "" then 64 - ~-~- judge that a non-empty string is read 65 - print(string.byte(readed,1,#readed)) 66 - a={string.byte(readed, 1, #readed)} 67 - if nil ~~= a[4] and nil ~~= a[5] and nil ~~= a[6] and nil ~~= a[7] then 68 - a1=a[4]*256+a[5] 69 - a2=a[6]*256+a[7] 70 - addr_setword("@HDW0",a1) 71 - addr_setword("@HDW1",a2) 72 - end 73 - return {string.byte(readed, 1, #readed)} 74 - elseif readed == nil then 75 - print("serial read err:", err or "") 76 - return nil 77 - end 78 - return nil 79 - end 80 - 81 -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-