Changes for page 1 Communication
Last modified by Hunter on 2024/07/01 10:54
From version 260.1
edited by Hunter
on 2024/07/01 10:54
on 2024/07/01 10:54
Change comment:
There is no comment for this version
To version 259.1
edited by Theodore Xu
on 2023/08/23 19:35
on 2023/08/23 19:35
Change comment:
There is no comment for this version
Summary
-
Page properties (2 modified, 0 added, 0 removed)
Details
- Page properties
-
- Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. Hunter1 +XWiki.AiXia - Content
-
... ... @@ -2477,63 +2477,66 @@ 2477 2477 2478 2478 Script Demo: 2479 2479 2480 -{{code language="lua"}} 2481 -function com.main() 2482 - --dosomething 2483 - openPlc() 2484 -end 2480 +~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-- 2485 2485 2486 -function openPlc() 2487 - if obj then 2488 - -- If the serial port opened,then read/write data by timing 2489 - local wri = string.pack(">HHHH",0X0103,0X0200,0X0003,0X0473) 2490 - local arr = reading(wri) 2491 - -- if arr ~=nil then 2492 - if arr[1] ==33 and arr[2] ==v[2][2] and arr[3] ==v[2][3] then 2493 - local A = arr[4]*256 2494 - local B = A+arr[5] 2495 - -- addr_setword(v[1],B) 2496 - end 2497 --- end 2498 - else 2499 - --judge if there is a serial object, then initialize and open the serial port 2500 - local configs = { 2501 - name = 'COM1', --Select serial port COM1 2502 - mode = 485, --Communication mode RS485 2503 - baud_rate = 9600, --Baud rate 9600 2504 - stop_bit = 1, --stop bit 1 2505 - data_len = 8, --data length 8 2506 - check_bit = 'NONE', --no parity 2507 - } 2508 - obj,err = serial.open(configs) --Open serial port 2509 - if not obj then 2510 - print("serial open :failed", err) -- Open serial port failed 2511 - end 2512 - end 2482 +function com.main() 2483 + ~-~-dosomething 2484 + openPlc() 2513 2513 end 2486 + function openPlc() 2487 + if obj then 2488 + ~-~- If the serial port opened,then read/write data by timing 2514 2514 2490 + local wri = string.pack(">HHHH",0X0103,0X0200,0X0003,0X0473) 2491 + local arr = reading(wri) 2492 + ~-~- if arr ~~=nil then 2493 + ~-~- if arr[1] ==33 and arr[2] ==v[2][2] and arr[3] ==v[2][3] then 2494 + ~-~- local A = arr[4]*256 2495 + ~-~- local B = A+arr[5] 2496 + ~-~- addr_setword(v[1],B) 2497 + ~-~- end 2498 + ~-~- end 2515 2515 2516 - functionreading(wri)2517 - obj:flush()--Clearserial port2518 - obj:write(wri)--Writeserialport2519 - print(string.byte(wri,1,#wri))2520 - o bj:flush()--lear serialport2521 - localreaded=obj:read(9,1000)--Read9 bytes,timeout 1 second2522 - ifreadedandreaded~=""then2523 - --judgethaton-emptystringis read2524 - print(string.byte(readed,1,#readed))2525 - a={string.byte(readed, 1, #readed)}2526 - ifnil~=a[4] and nil ~= a[5] and nil ~= a[6] and nil ~= a[7] then2527 - a1=a[4]*256+a[5]2528 - a2=a[6]*256+a[7]2529 - addr_setword("@HDW0",a1)2530 - addr_setword("@HDW1",a2)2500 + else 2501 + ~-~-judge if there is a serial object, then initialize and open the serial port 2502 + local configs = { 2503 + name = 'COM1', ~-~-Select serial port COM1 2504 + mode = 485, ~-~-Communication mode RS485 2505 + baud_rate = 9600, ~-~-Baud rate 9600 2506 + stop_bit = 1, ~-~-stop bit 1 2507 + data_len = 8, ~-~-data length 8 2508 + check_bit = 'NONE', ~-~-no parity 2509 + 2510 + } 2511 + obj,err = serial.open(configs) ~-~-Open serial port 2512 + if not obj then 2513 + print("serial open :failed", err) ~-~- Open serial port failed 2514 + end 2531 2531 end 2532 - return {string.byte(readed, 1, #readed)} 2533 - elseif readed == nil then 2534 - print("serial read err:", err or "") 2516 + end 2517 + 2518 + function reading(wri) 2519 + obj:flush() ~-~- Clear serial port 2520 + obj:write(wri) ~-~- Write serial port 2521 + print(string.byte(wri,1,#wri)) 2522 + obj:flush() ~-~- Clear serial port 2523 + local readed =obj:read(9,1000) ~-~- Read 9 bytes, timeout 1 second 2524 + if readed and readed ~~= "" then 2525 + ~-~- judge that a non-empty string is read 2526 + print(string.byte(readed,1,#readed)) 2527 + a={string.byte(readed, 1, #readed)} 2528 + if nil ~~= a[4] and nil ~~= a[5] and nil ~~= a[6] and nil ~~= a[7] then 2529 + a1=a[4]*256+a[5] 2530 + a2=a[6]*256+a[7] 2531 + addr_setword("@HDW0",a1) 2532 + addr_setword("@HDW1",a2) 2533 + end 2534 + return {string.byte(readed, 1, #readed)} 2535 + elseif readed == nil then 2536 + print("serial read err:", err or "") 2537 + return nil 2538 + end 2535 2535 return nil 2536 - end 2537 - return nil 2538 -end 2539 -{{/code}} 2540 + end 2541 + 2542 +~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-