Changes for page 1 Communication
Last modified by Hunter on 2024/07/01 10:54
From 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. AiXia1 +XWiki.Hunter - Content
-
... ... @@ -2477,66 +2477,63 @@ 2477 2477 2478 2478 Script Demo: 2479 2479 2480 -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-- 2481 - 2480 +{{code language="lua"}} 2482 2482 function com.main() 2483 - ~-~-dosomething 2484 - openPlc() 2482 + --dosomething 2483 + openPlc() 2484 +end 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 2485 2485 end 2486 - function openPlc() 2487 - if obj then 2488 - ~-~- If the serial port opened,then read/write data by timing 2489 2489 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 2499 2499 2500 - lse2501 - ~-~-judge ifthereisa serialobject, then initializeand open theserial port2502 - localconfigs = {2503 - ame = 'COM1', ~-~-Selecterial portCOM12504 - mode= 485, ~-~-CommunicationmodeRS4852505 - baud_rate =600,~-~-Baudrate96002506 - stop_bit=1,-~-stopbit12507 - ata_len= 8, ~-~-datalength82508 - check_bit = 'NONE', ~-~-noparity2509 - 2510 - }2511 - obj,err=serial.open(configs) ~-~-Open serial port2512 - if not obj then2513 - print("serial open :failed", err) ~-~- Openserial port failed2514 - nd2516 +function reading(wri) 2517 + obj:flush() -- Clear serial port 2518 + obj:write(wri)-- Write serial port 2519 + print(string.byte(wri,1,#wri)) 2520 + obj:flush() -- Clear serial port 2521 + local readed =obj:read(9,1000) -- Read 9 bytes, timeout 1 second 2522 + if readed and readed ~= "" then 2523 + -- judge that a non-empty string is read 2524 + print(string.byte(readed,1,#readed)) 2525 + a={string.byte(readed, 1, #readed)} 2526 + if nil ~= a[4] and nil ~= a[5] and nil ~= a[6] and nil ~= a[7] then 2527 + a1=a[4]*256+a[5] 2528 + a2=a[6]*256+a[7] 2529 + addr_setword("@HDW0",a1) 2530 + addr_setword("@HDW1",a2) 2515 2515 end 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 2532 + return {string.byte(readed, 1, #readed)} 2533 + elseif readed == nil then 2534 + print("serial read err:", err or "") 2539 2539 return nil 2540 - end 2541 - 2542 -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- 2536 + end 2537 + return nil 2538 +end 2539 +{{/code}}