-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlmodbus.lua
1201 lines (876 loc) · 29.8 KB
/
lmodbus.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- supported ModBus transaction types
local types = {
RTU = 1,
TCP = 2,
}
-- supported ModBus functions
local funcs = {
READ_COILS = 0x01,
-- READ_DISCRETE_INPUTS = 0x02,
READ_HOLDING_REGISTERS = 0x03,
READ_INPUT_REGISTERS = 0x04,
-- WRITE_SINGLE_COIL = 0x05,
-- WRITE_SINGLE_REGISTER = 0x06,
-- READ_EXCEPTION_STATUS = 0x07,
-- DIAGNOSTICS = 0x08,
-- GET_COMM_EVENT_COUNTER = 0x0B,
-- GET_COMM_EVENT_LOG = 0x0C,
-- WRITE_MULTIPLE_COILS = 0x0F,
WRITE_MULTIPLE_REGISTERS = 0x10,
REPORT_SLAVE_ID = 0x11,
-- READ_FILE_RECORD = 0x14,
-- WRITE_FILE_RECORD = 0x15,
MASK_WRITE_REGISTER = 0x16,
-- READ_WRITE_MULTIPLE_REGISTERS = 0x17,
-- READ_FIFO_QUEUE = 0x18,
-- READ_DEVICE_ID = 0x2B,
}
local errors = {
[0x00] = "NO ERROR",
[0x01] = "ILLEGAL FUNCTION",
[0x02] = "ILLEGAL DATA ADDRESS",
[0x03] = "ILLEGAL DATA VALUE",
[0x04] = "SLAVE DEVICE FAILURE",
[0x05] = "ACKNOWLEDGE",
[0x06] = "SLAVE DEVICE BUSY",
[0x08] = "MEMORY PARITY ERROR",
[0x0A] = "GATEWAY PATH UNAVAILABLE",
[0x0B] = "GATEWAY TARGET DEVICE FAILED TO RESPOND",
}
local stat = {
COMPLETED = 'Completed',
NOT_COMPLETED = 'Not completed',
INVALID_PARAM = 'Invalid parameter',
CRC_ERROR = 'CRC error',
OVERFLOW = 'Overflow',
FUNC_NOT_SUPPORTED = 'Function not supported',
INVALID_REQUEST = 'Invalid request',
INVALID_RESPONSE = 'Invalid response',
}
-------------------------------------------------------------------------------
--- Utilities
-------------------------------------------------------------------------------
sprintf = string.format
function errorf(...)
--error(sprintf(...))
print(...)
end
-------------------------------------------------------------------------------
function band(x, y)
--return x & y
return bit.band(x,y)
end
-------------------------------------------------------------------------------
function bor(x, y)
--return x | y
return bit.bor(x,y)
end
-------------------------------------------------------------------------------
function bxor(x, y)
--return x ~ y
return bit.bxor(x,y)
end
-------------------------------------------------------------------------------
function blshift(x, n)
--return x << n
return bit.lshift(x,n)
end
-------------------------------------------------------------------------------
function brshift(x, n)
--return x >> n
return bit.rshift(x,n)
end
-------------------------------------------------------------------------------
function in_range(val, min, max)
return type(val) == 'number' and val >= min and val <= max
end
-------------------------------------------------------------------------------
function byte_at(str, ofs)
return str:byte(ofs + 1) -- ofs is zero-based
end
-------------------------------------------------------------------------------
function word_at(str, ofs)
ofs = ofs + 1 -- ofs is zero-based
return blshift(str:byte(ofs), 8) + str:byte(ofs + 1)
end
-------------------------------------------------------------------------------
function word_at_le(str, ofs)
-- little-endian
ofs = ofs + 1 -- ofs is zero-based
return blshift(str:byte(ofs + 1), 8) + str:byte(ofs)
end
-------------------------------------------------------------------------------
function mk_word(val)
return string.char(band(brshift(val, 8), 0xFF), band(val, 0xFF))
end
-------------------------------------------------------------------------------
function mk_word_le(val)
return string.char(band(val, 0xFF), band(brshift(val, 8), 0xFF))
end
-------------------------------------------------------------------------------
local crc16_tab = {
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040,
}
-------------------------------------------------------------------------------
function calc_crc16(str, size)
local crc = 0xFFFF
if not size then
size = #str
end
for i = 1, size do
local b = str:byte(i)
crc = bxor(brshift(crc, 8), crc16_tab[band(bxor(crc, b), 0xFF) + 1])
end
return crc
end
-------------------------------------------------------------------------------
function check_crc16(str)
local ofs = #str - 2
return calc_crc16(str, ofs) == word_at_le(str, ofs)
end
-------------------------------------------------------------------------------
function is_rtu(xact)
return xact.type == types.RTU or xact.type == 'RTU'
end
-------------------------------------------------------------------------------
function is_tcp(xact)
return xact.type == types.TCP or xact.type == 'TCP'
end
-------------------------------------------------------------------------------
adu_header_size_tab = {
RTU = 1, [types.RTU] = 1,
TCP = 7, [types.TCP] = 7,
}
function adu_header_size(xact)
return adu_header_size_tab[xact.type]
end
local func_ofs = adu_header_size
-------------------------------------------------------------------------------
crc_size_tab = {
RTU = 2, [types.RTU] = 2,
TCP = 0, [types.TCP] = 0,
}
function crc_size(xact)
return crc_size_tab[xact.type]
end
-------------------------------------------------------------------------------
function adu_extra_size(xact)
return adu_header_size(xact) + crc_size(xact)
end
-------------------------------------------------------------------------------
function min_request_size(xact)
-- Function code + application data header size
return 1 + adu_header_size(xact)
end
-------------------------------------------------------------------------------
function min_response_size(xact)
-- Function code + Exception code + application data header size
return 1 + 1 + adu_header_size(xact)
end
-------------------------------------------------------------------------------
function request_item_count(xact, req)
-- Quantity of Inputs/Outputs
return word_at(req, adu_header_size(xact) + 3)
end
local response_item_count = request_item_count
-------------------------------------------------------------------------------
function response_byte_count(xact, resp)
return byte_at(resp, adu_header_size(xact) + 1)
end
-------------------------------------------------------------------------------
function fix_mbap_length(str)
-- Set the length value in the MBAP header.
-- XXX Assume type == 'TCP'
return str:sub(1, 4) .. mk_word(#str - 6) .. str:sub(7, -1)
end
-------------------------------------------------------------------------------
function get_mbap_length(str)
-- Get the length value from the MBAP header.
-- XXX Assume type == 'TCP'
return word_at(str, 4)
end
-------------------------------------------------------------------------------
function new_adu(xact)
if is_rtu(xact) then
-- TODO check range
return string.char(xact.server_addr)
elseif is_tcp(xact) then
return
-- MBAP header
mk_word(xact.transact_id) ..
string.char(
-- 0 = MODBUS protocol
0, 0,
-- XXX Length. This value must be subsequently fixed by the call to
-- fix_mbap_length after we know the data size.
0, 0,
-- TODO check range
xact.server_addr
)
end
-- can't happen?
return ''
end
-------------------------------------------------------------------------------
function calc_byte_count(bit_count)
local byte_count, frac = math.modf(bit_count / 8)
if frac ~= 0.0 then
byte_count = byte_count + 1
end
return byte_count
end
-------------------------------------------------------------------------------
-- READ COILS
-------------------------------------------------------------------------------
local function build_request_read_coils(xact)
if not in_range(xact.bit_count, 1, 2000) then
errorf("'bit_count' value (%s) is not properly assigned or out of range 1..2000",
tostring(xact.word_count))
end
local req = new_adu(xact) ..
string.char(funcs.READ_COILS) ..
mk_word(xact.start_addr) ..
mk_word(xact.bit_count)
if is_rtu(xact) then
req = req .. mk_word_le(calc_crc16(req))
end
if is_tcp(xact) then
req = fix_mbap_length(req)
end
return req
end
-------------------------------------------------------------------------------
local function parse_request_read_coils(xact, req)
local req_size = adu_extra_size(xact) + 5
if #req < req_size then
return false, stat.NOT_COMPLETED
end
-- TODO check crc
xact.start_addr = word_at(req, adu_header_size(xact) + 1)
xact.bit_count = word_at(req, adu_header_size(xact) + 3)
return true, req_size
end
-------------------------------------------------------------------------------
local function build_response_read_coils(xact)
-- TODO check request, e.g. coil count
local byte_count = calc_byte_count(#xact.bits)
local resp = new_adu(xact) ..
string.char(funcs.READ_COILS) ..
string.char(byte_count)
local byte = 0
for i = 0, xact.bit_count - 1 do
if xact.bits[i + 1] == 1 then
byte = bor(byte, blshift(1, i % 8))
end
if (i ~= 0 and i % 8 == 0) or (i == xact.bit_count - 1) then
resp = resp .. byte
byte = 0
end
end
if is_rtu(xact) then
resp = resp .. mk_word_le(calc_crc16(resp))
end
if is_tcp(xact) then
resp = fix_mbap_length(resp)
end
return true, resp
end
-------------------------------------------------------------------------------
local function parse_response_read_coils(xact, resp)
local resp_lead_size = adu_header_size(xact) + 2
if #resp < resp_lead_size then
return false, stat.NOT_COMPLETED
end
local bc = response_byte_count(xact, resp)
local resp_size = resp_lead_size + bc + crc_size(xact)
if #resp < resp_size then
-- didn't get the whole response
return false, stat.NOT_COMPLETED
end
if is_rtu(xact) and not check_crc16(resp) then
return false, stat.CRC_ERROR
end
if is_tcp(xact) and get_mbap_length(resp) ~= bc + 3 then
return false, stat.INVALID_RESPONSE
end
if calc_byte_count(xact.bit_count) ~= bc then
-- the response data size does not match the request coil count
return false, stat.INVALID_RESPONSE
end
xact.bits = {}
local byte = 0
for i = 0, xact.bit_count - 1 do
if i % 8 == 0 then
byte = byte_at(resp, resp_lead_size + math.floor(i / 8))
end
xact.bits[i + 1] = band(byte, blshift(1, i % 8)) ~= 0 and 1 or 0
end
return true, resp_size
end
-------------------------------------------------------------------------------
-- READ HOLDING REGISTERS
-------------------------------------------------------------------------------
local function build_request_read_holding_registers(xact)
if not in_range(xact.word_count, 1, 125) then
errorf("'word_count' value (%s) is not properly assigned or out of range 1..125",
tostring(xact.word_count))
end
local req = new_adu(xact) ..
string.char(funcs.READ_HOLDING_REGISTERS) ..
mk_word(xact.start_addr) ..
string.char(0, xact.word_count)
if is_rtu(xact) then
req = req .. mk_word_le(calc_crc16(req))
end
if is_tcp(xact) then
req = fix_mbap_length(req)
end
return req
end
-------------------------------------------------------------------------------
local function parse_request_read_holding_registers(xact, req)
local req_size = adu_extra_size(xact) + 5
if #req < req_size then
return false, stat.NOT_COMPLETED
end
-- TODO check crc
xact.start_addr = word_at(req, adu_header_size(xact) + 1)
xact.word_count = word_at(req, adu_header_size(xact) + 3)
return true, req_size
end
-------------------------------------------------------------------------------
local function build_response_read_holding_registers(xact)
-- TODO check request, e.g. register count
local resp = new_adu(xact) ..
string.char(funcs.READ_HOLDING_REGISTERS) ..
string.char(#xact.words * 2)
for i = 1, #xact.words do
resp = resp .. mk_word(xact.words[i])
end
if is_rtu(xact) then
resp = resp .. mk_word_le(calc_crc16(resp))
end
if is_tcp(xact) then
resp = fix_mbap_length(resp)
end
return true, resp
end
-------------------------------------------------------------------------------
local function parse_response_read_holding_registers(xact, resp)
local resp_lead_size = adu_header_size(xact) + 2
if #resp < resp_lead_size then
return false, stat.NOT_COMPLETED
end
local bc = response_byte_count(xact, resp)
local resp_size = resp_lead_size + bc + crc_size(xact)
if #resp < resp_size then
-- didn't get the whole response
return false, stat.NOT_COMPLETED
end
if is_rtu(xact) and not check_crc16(resp) then
return false, stat.CRC_ERROR
end
if is_tcp(xact) and get_mbap_length(resp) ~= bc + 3 then
return false, stat.INVALID_RESPONSE
end
if xact.word_count * 2 ~= bc then
-- the response data size does not match the request register count
return false, stat.INVALID_RESPONSE
end
xact.words = {}
for i = 1, xact.word_count do
xact.words[i] = word_at(resp, resp_lead_size + (i - 1) * 2)
end
return true, resp_size
end
-------------------------------------------------------------------------------
-- READ INPUT REGISTERS
-------------------------------------------------------------------------------
local function build_request_read_input_registers(xact)
if not in_range(xact.word_count, 1, 125) then
errorf("'word_count' value (%s) is not properly assigned or out of range 1..125",
tostring(xact.word_count))
end
local req = new_adu(xact) ..
string.char(funcs.READ_INPUT_REGISTERS) ..
mk_word(xact.start_addr) ..
string.char(0, xact.word_count)
if is_rtu(xact) then
req = req .. mk_word_le(calc_crc16(req))
end
if is_tcp(xact) then
req = fix_mbap_length(req)
end
return req
end
-------------------------------------------------------------------------------
local function parse_request_read_input_registers(xact, req)
local req_size = adu_extra_size(xact) + 5
if #req < req_size then
return false, stat.NOT_COMPLETED
end
-- TODO check crc
xact.start_addr = word_at(req, adu_header_size(xact) + 1)
xact.word_count = word_at(req, adu_header_size(xact) + 3)
return true, req_size
end
-------------------------------------------------------------------------------
local function build_response_read_input_registers(xact)
-- TODO check request, e.g. register count
local req = new_adu(xact) ..
string.char(funcs.READ_INPUT_REGISTERS) ..
string.char(#xact.words * 2)
for i = 1, #xact.words do
resp = resp .. mk_word(xact.words[i])
end
if is_rtu(xact) then
resp = req .. mk_word_le(calc_crc16(resp))
end
if is_tcp(xact) then
req = fix_mbap_length(resp)
end
return true, resp
end
-------------------------------------------------------------------------------
local function parse_response_read_input_registers(xact, resp)
local resp_lead_size = adu_header_size(xact) + 2;
if #resp < resp_lead_size then
return false, stat.NOT_COMPLETED
end
local bc = response_byte_count(xact, resp)
local resp_size = bc + resp_lead_size + crc_size(xact)
if #resp < resp_size then
-- didn't get the whole response
return false, stat.NOT_COMPLETED
end
if is_rtu(xact) and not check_crc16(resp) then
return false, stat.CRC_ERROR
end
if is_tcp(xact) and get_mbap_length(resp) ~= bc + 3 then
return false, stat.INVALID_RESPONSE
end
if xact.word_count * 2 ~= bc then
-- the response data size does not match the request register count
return false, stat.INVALID_RESPONSE
end
xact.words = {}
for i = 1, xact.word_count do
xact.words[i] = word_at(resp, resp_lead_size + (i - 1) * 2)
end
return true, resp_size
end
-------------------------------------------------------------------------------
-- WRITE MULTIPLE REGISTERS
-------------------------------------------------------------------------------
local function build_request_write_multiple_registers(xact)
if not xact.words or not in_range(#xact.words, 1, 123) then
error("'words' value is not properly assigned or out of range 1..125")
end
local req = new_adu(xact)..
string.char(funcs.WRITE_MULTIPLE_REGISTERS) ..
mk_word(xact.start_addr) ..
mk_word(#xact.words) ..
string.char(#xact.words * 2)
for i = 1, #xact.words do
req = req .. mk_word(xact.words[i])
end
if is_rtu(xact) then
req = req .. mk_word_le(calc_crc16(req))
end
if is_tcp(xact) then
req = fix_mbap_length(req)
end
return req
end
-------------------------------------------------------------------------------
local function parse_request_write_multiple_registers(xact, req)
local req_lead_size = adu_header_size(xact) + 6
if #req < req_lead_size then
return false, stat.NOT_COMPLETED
end
local word_count = request_item_count(xact, req)
local bc = byte_at(req, adu_header_size(xact) + 5)
if word_count * 2 ~= bc then
return false, stat.INVALID_REQUEST
end
local req_size = req_lead_size + bc + crc_size(xact)
if #req < req_size then
return false, stat.NOT_COMPLETED
end
-- TODO check crc
xact.start_addr = word_at(req, adu_header_size(xact) + 1)
local word_count = word_at(req, adu_header_size(xact) + 3)
xact.words = {}
for i = 1, word_count do
xact.words[i] = word_at(req, 6 + (i - 1) * 2)
end
return true, req_size
end
-------------------------------------------------------------------------------
local function build_response_write_multiple_registers(xact)
-- TODO check request, e.g. register count
local resp = new_adu(xact)..
string.char(funcs.WRITE_MULTIPLE_REGISTERS) ..
mk_word(xact.start_addr) ..
mk_word(#xact.words)
if is_rtu(xact) then
resp = resp .. mk_word_le(calc_crc16(resp))
end
if is_tcp(xact) then
resp = fix_mbap_length(resp)
end
return true, resp
end
-------------------------------------------------------------------------------
local function parse_response_write_multiple_registers(xact, resp)
local resp_size = 5 + adu_extra_size(xact)
if #resp < resp_size then
return false, stat.NOT_COMPLETED
end
if is_rtu(xact) and not check_crc16(resp) then
return false, stat.CRC_ERROR
end
if is_tcp(xact) and get_mbap_length(resp) ~= 6 then
return false, stat.INVALID_RESPONSE
end
if xact.start_addr ~= word_at(resp, adu_header_size(xact) + 1) or
#xact.words ~= word_at(resp, adu_header_size(xact) + 3) then
return false, stat.INVALID_RESPONSE;
end
return true, resp_size
end
-------------------------------------------------------------------------------
-- REPORT SLAVE ID
-------------------------------------------------------------------------------
local function build_request_report_slave_id(xact)
local req = new_adu(xact).. string.char(funcs.REPORT_SLAVE_ID)
if is_rtu(xact) then
req = req .. mk_word_le(calc_crc16(req))
end
if is_tcp(xact) then
req = fix_mbap_length(req)
end
return req
end
-------------------------------------------------------------------------------
local function parse_request_report_slave_id(xact, req)
local req_size = adu_extra_size(xact) + 1
if #req < req_size then
return false, stat.NOT_COMPLETED
end
-- TODO check crc
return true, req_size
end
-------------------------------------------------------------------------------
local function build_response_report_slave_id(xact)
local resp = new_adu(xact)..
string.char(funcs.REPORT_SLAVE_ID) ..
string.char(#xact.slave_id) ..
xact.slave_id
if is_rtu(xact) then
resp = resp .. mk_word_le(calc_crc16(resp))
end
if is_tcp(xact) then
resp = fix_mbap_length(resp)
end
return true, resp
end
-------------------------------------------------------------------------------
local function parse_response_report_slave_id(xact, resp)
local resp_lead_size = adu_header_size(xact) + 2;
if #resp < resp_lead_size then
return false, stat.NOT_COMPLETED
end
local resp_size = response_byte_count(resp) + resp_lead_size + crc_size(xact)
if #resp < resp_size then
-- didn't get the whole response
return false, stat.NOT_COMPLETED
end
if is_rtu(xact) and not check_crc16(resp) then
return false, stat.CRC_ERROR
end
xact.slave_id = resp:sub(resp_lead_size, -1 - crc_size(xact))
return true, resp_size
end
-------------------------------------------------------------------------------
-- MASK WRITE REGISTER
-------------------------------------------------------------------------------
local function build_request_mask_write_register(xact)
local req = new_adu(xact)..
string.char(funcs.MASK_WRITE_REGISTER) ..
mk_word(xact.start_addr) ..
mk_word(xact.and_mask) ..
mk_word(xact.or_mask)
if is_rtu(xact) then
req = req .. mk_word_le(calc_crc16(req))
end
if is_tcp(xact) then
req = fix_mbap_length(req)
end
return req
end
-------------------------------------------------------------------------------
local function parse_request_mask_write_register(xact, req)
local req_start = adu_header_size(xact)
local req_size = req_start + 7 + crc_size(xact)
if #req < req_size then
return false, stat.NOT_COMPLETED
end
-- TODO check crc
xact.start_addr = word_at(req, req_start + 1)
xact.and_mask = word_at(req, req_start + 3)
xact.or_mask = word_at(req, req_start + 5)
return true, req_size
end
-------------------------------------------------------------------------------
local function build_response_mask_write_register(xact)
local resp = new_adu(xact)..
string.char(funcs.MASK_WRITE_REGISTER) ..
mk_word(xact.start_addr) ..
mk_word(xact.and_mask) ..
mk_word(xact.or_mask)
if is_rtu(xact) then
resp = resp .. mk_word_le(calc_crc16(resp))
end
if is_tcp(xact) then
resp = fix_mbap_length(resp)
end
return resp
end
-------------------------------------------------------------------------------
local function parse_response_mask_write_register(xact, resp)
local resp_start = adu_header_size(xact)
local resp_size = resp_start + 7 + crc_size(xact)
if #resp < resp_size then
return false, stat.NOT_COMPLETED
end
if is_rtu(xact) and not check_crc16(resp) then
return false, stat.CRC_ERROR
end
if xact.start_addr ~= word_at(resp, resp_start + 1) or
xact.and_mask ~= word_at(resp, resp_start + 3) or
xact.or_mask ~= word_at(resp, resp_start + 5) then
return false, stat.INVALID_RESPONSE
end
return true, resp_size
end
-------------------------------------------------------------------------------
local function parse_response_exception(xact, resp)
local resp_size = 2 + adu_extra_size(xact)
if #resp < resp_size then
return false, stat.NOT_COMPLETED
end
if is_tcp(xact) and get_mbap_length(resp) ~= 6 then
return false, stat.INVALID_RESPONSE
end
if is_rtu(xact) and not check_crc16(resp) then
return false, stat.CRC_ERROR
end
xact.error = byte_at(resp, adu_header_size(xact) + 1)
return true, resp_size
end
-------------------------------------------------------------------------------
function get_local_var(name)
local i = 1
while true do
local n, v = debug.getlocal(2, i)
if n == name then
return v
elseif not n then
break
end
i = i + 1
end
errorf("'%s' is not defined", name)
end
-------------------------------------------------------------------------------
local func_info = {}
for k, v in pairs(funcs) do
func_info[v] = {
name = k,
build_request = get_local_var('build_request_' .. k:lower()),
parse_request = get_local_var('parse_request_' .. k:lower()),
build_response = get_local_var('build_response_' .. k:lower()),
parse_response = get_local_var('parse_response_' .. k:lower()),
}
end