-
Notifications
You must be signed in to change notification settings - Fork 27
/
lua-libmodbus.c
1423 lines (1229 loc) · 33.2 KB
/
lua-libmodbus.c
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
/*** lua bindings to libmodbus.
<p>Generally, this provides a very thin layer over libmodbus. Instead of
passing the context around to all your modbus_xxx functions, you simply
call them as member functions on the context returned by the new() functions.
@module libmodbus
@author Karl Palsson <[email protected]> 2016-2020
@license
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <sys/time.h>
#if defined(WIN32)
#include <winsock2.h>
#endif
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <modbus/modbus.h>
#include "compat.h"
/* unique naming for userdata metatables */
#define MODBUS_META_CTX "modbus.ctx"
typedef struct {
lua_State *L;
modbus_t *modbus;
size_t max_len;
/* only used for making tostring */
char *dev_host;
char *service;
int baud;
char parity;
int databits;
int stopbits;
/* used to prevent using tcp methods on a rtu context */
bool is_rtu;
} ctx_t;
/*
* Pushes either "true" or "nil, errormessage"
* @param L
* @param rc rc from modbus_xxxx function call
* @param expected what rc was meant to be
* @return the count of stack elements pushed
*/
static int libmodbus_rc_to_nil_error(lua_State *L, int rc, int expected)
{
if (rc == expected) {
lua_pushboolean(L, true);
return 1;
} else {
lua_pushnil(L);
// TODO - insert the integer errno code here too?
lua_pushstring(L, modbus_strerror(errno));
return 2;
}
}
/**
* Returns the runtime linked version of libmodbus as a string.
* The compile time version is available as a constant VERSION.
* @function version
* @return eg "3.0.6"
* @see other_constants
*/
static int libmodbus_version(lua_State *L)
{
char version[16];
snprintf(version, sizeof(version) - 1, "%i.%i.%i",
libmodbus_version_major, libmodbus_version_minor, libmodbus_version_micro);
lua_pushstring(L, version);
return 1;
}
/**
* Create a Modbus/RTU context
* @function new_rtu
* @param device (required)
* @param baud rate, defaults to 19200
* @param parity defaults to EVEN
* @param databits defaults to 8
* @param stopbits defaults to 1
* @return a modbus context ready for use
*/
static int libmodbus_new_rtu(lua_State *L)
{
const char *device = luaL_checkstring(L, 1);
int baud = luaL_optnumber(L, 2, 19200);
const char *parityin = luaL_optstring(L, 3, "EVEN");
int databits = luaL_optnumber(L, 4, 8);
int stopbits = luaL_optnumber(L, 5, 1);
/* just accept baud as is */
/* parity must be one of a few things... */
char parity;
switch (parityin[0]) {
case 'e':
case 'E':
parity = 'E';
break;
case 'n':
case 'N':
parity = 'N';
break;
case 'o':
case 'O':
parity = 'O';
break;
default:
return luaL_argerror(L, 3, "Unrecognised parity");
}
ctx_t *ctx = (ctx_t *) lua_newuserdata(L, sizeof(ctx_t));
ctx->modbus = modbus_new_rtu(device, baud, parity, databits, stopbits);
ctx->max_len = MODBUS_RTU_MAX_ADU_LENGTH;
ctx->is_rtu = true;
if (ctx->modbus == NULL) {
return luaL_error(L, modbus_strerror(errno));
}
ctx->L = L;
/* save data for nice string representations */
ctx->baud = baud;
ctx->databits = databits;
ctx->dev_host = strdup(device);
ctx->parity = parity;
ctx->stopbits = stopbits;
/* Make sure unused fields are zeroed */
ctx->service = NULL;
luaL_getmetatable(L, MODBUS_META_CTX);
// Can I put more functions in for rtu here? maybe?
lua_setmetatable(L, -2);
return 1;
}
/**
* Create a Modbus/TCP context
* @function new_tcp_pi
* @param host eg "192.168.1.100" or "modbus.example.org"
* @param service eg "502" or "mbap"
* @return a modbus context ready for use
*/
static int libmodbus_new_tcp_pi(lua_State *L)
{
const char *host = luaL_checkstring(L, 1);
const char *service = luaL_checkstring(L, 2);
// Do we really need any of this? no callbacks in libmodbus?
ctx_t *ctx = (ctx_t *) lua_newuserdata(L, sizeof(ctx_t));
ctx->modbus = modbus_new_tcp_pi(host, service);
ctx->max_len = MODBUS_TCP_MAX_ADU_LENGTH;
ctx->is_rtu = false;
if (ctx->modbus == NULL) {
return luaL_error(L, modbus_strerror(errno));
}
ctx->L = L;
/* save data for nice string representations */
ctx->dev_host = strdup(host);
ctx->service = strdup(service);
ctx->databits = 0;
luaL_getmetatable(L, MODBUS_META_CTX);
// Can I put more functions in for tcp here? maybe?
lua_setmetatable(L, -2);
return 1;
}
/** Write a 32bit (u)int to 2x16bit registers
* @function set_s32
* @param num 32bit number
* @return reg1 upper 16bits
* @return reg2 lower 16bits
*/
static int helper_set_s32(lua_State *L)
{
/* truncate as necessary */
//const uint32_t toval = (uint32_t)luaL_checknumber(L, 1);
lua_Number n = luaL_checknumber(L, 1);
int32_t toval = (int32_t)n;
lua_pushinteger(L, toval >> 16);
lua_pushinteger(L, toval & 0xffff);
return 2;
}
/** Write a bit float to 2x16bit registers
* @function set_f32
* @param num floating point number
* @return reg1 upper 16bits of a 32bit float
* @return reg2 lower 16bits of a 32bit float
*/
static int helper_set_f32(lua_State *L)
{
/* truncate to float if necessary */
const float in = (float)luaL_checknumber(L, 1);
uint32_t out;
memcpy(&out, &in, sizeof(out));
lua_pushinteger(L, out >> 16);
lua_pushinteger(L, out & 0xffff);
return 2;
}
/**
* 16bit register as number to signed 16bit
* @function get_s16
* @param 1 16bit register
* @return signed 16bit number
*/
static int helper_get_s16(lua_State *L)
{
const int16_t in = luaL_checknumber(L, 1);
lua_pushinteger(L, in);
return 1;
}
/**
* 2x16bit registers as number to signed 32 bit
* @function get_s32
* @param 1 16bit register
* @param 2 16bit register
* @return 32bit number
*/
static int helper_get_s32(lua_State *L)
{
const uint16_t in1 = luaL_checknumber(L, 1);
const uint16_t in2 = luaL_checknumber(L, 2);
int32_t out = in1 << 16 | in2;
lua_pushinteger(L, out);
return 1;
}
/**
* 2x16bit registers as number to signed 32 bit (reverse order)
* @function get_s32le
* @param 1 16bit register
* @param 2 16bit register
* @return 32bit number
*/
static int helper_get_s32le(lua_State *L)
{
const uint16_t in2 = luaL_checknumber(L, 1);
const uint16_t in1 = luaL_checknumber(L, 2);
int32_t out = in1 << 16 | in2;
lua_pushinteger(L, out);
return 1;
}
/**
* 2x16bit registers as number to unsigned 32 bit
* @function get_u32
* @param 1 16bit register
* @param 2 16bit register
* @return 32bit number
*/
static int helper_get_u32(lua_State *L)
{
const uint16_t in1 = luaL_checknumber(L, 1);
const uint16_t in2 = luaL_checknumber(L, 2);
uint32_t out = in1 << 16 | in2;
lua_pushnumber(L, out);
return 1;
}
/**
* 2x16bit registers as number to unsigned 32 bit (reversed order)
* @function get_u32le
* @param 1 16bit register
* @param 2 16bit register
* @return 32bit number
*/
static int helper_get_u32le(lua_State *L)
{
const uint16_t in2 = luaL_checknumber(L, 1);
const uint16_t in1 = luaL_checknumber(L, 2);
uint32_t out = in1 << 16 | in2;
lua_pushnumber(L, out);
return 1;
}
/**
* 2x16bit registers as number to 32 bit float
* @function get_f32
* @param 1 16bit register
* @param 2 16bit register
* @return 32bit float
*/
static int helper_get_f32(lua_State *L)
{
const uint16_t in1 = luaL_checknumber(L, 1);
const uint16_t in2 = luaL_checknumber(L, 2);
uint32_t inval = in1<<16 | in2;
float f;
memcpy(&f, &inval, sizeof(f));
lua_pushnumber(L, f);
return 1;
}
/**
* 2x16bit registers as number to 32 bit float (Reversed order)
* @function get_f32le
* @param 1 16bit register
* @param 2 16bit register
* @return 32bit float
*/
static int helper_get_f32le(lua_State *L)
{
const uint16_t in2 = luaL_checknumber(L, 1);
const uint16_t in1 = luaL_checknumber(L, 2);
uint32_t inval = in1<<16 | in2;
float f;
memcpy(&f, &inval, sizeof(f));
lua_pushnumber(L, f);
return 1;
}
/**
* 4x16bit registers as number to signed 64 bit
* @function get_s64
* @param 1 16bit register
* @param 2 16bit register
* @param 3 16bit register
* @param 4 16bit register
* @return 64bit number
*/
static int helper_get_s64(lua_State *L)
{
const uint16_t in1 = luaL_checknumber(L, 1);
const uint16_t in2 = luaL_checknumber(L, 2);
const uint16_t in3 = luaL_checknumber(L, 3);
const uint16_t in4 = luaL_checknumber(L, 4);
int64_t out = in1;
out = out << 16 | in2;
out = out << 16 | in3;
out = out << 16 | in4;
lua_pushnumber(L, out);
return 1;
}
/**
* 4x16bit registers as number to unsigned 64 bit
* @function get_u64
* @param 1 16bit register
* @param 2 16bit register
* @param 3 16bit register
* @param 4 16bit register
* @return 64bit number
*/
static int helper_get_u64(lua_State *L)
{
const uint16_t in1 = luaL_checknumber(L, 1);
const uint16_t in2 = luaL_checknumber(L, 2);
const uint16_t in3 = luaL_checknumber(L, 3);
const uint16_t in4 = luaL_checknumber(L, 4);
uint64_t out = (uint64_t)in1 << 48 | (uint64_t)in2 << 32 | (uint64_t)in3 << 16 | in4;
lua_pushnumber(L, out);
return 1;
}
static ctx_t * ctx_check(lua_State *L, int i)
{
return (ctx_t *) luaL_checkudata(L, i, MODBUS_META_CTX);
}
static int ctx_destroy(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
modbus_close(ctx->modbus);
modbus_free(ctx->modbus);
if (ctx->dev_host) {
free(ctx->dev_host);
}
if (ctx->service) {
free(ctx->service);
}
/* remove all methods operating on ctx */
lua_newtable(L);
lua_setmetatable(L, -2);
/* Nothing to return on stack */
return 0;
}
static int ctx_tostring(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
if (ctx->databits) {
lua_pushfstring(L, "ModbusRTU<%s@%d/%c%d>", ctx->dev_host, ctx->databits, ctx->parity, ctx->stopbits);
} else {
lua_pushfstring(L, "ModbusTCP<%s@%s>", ctx->dev_host, ctx->service);
}
return 1;
}
/** Context Methods.
* These functions are members of a modbus context, from either new_rtu() or new_tcp_pi()
* @section context_methods
*/
/**
* Connect, see modbus_connect()
* @function ctx:connect
*/
static int ctx_connect(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int rc = modbus_connect(ctx->modbus);
return libmodbus_rc_to_nil_error(L, rc, 0);
}
/**
* Close, see modbus_close()
* @function ctx:close
*/
static int ctx_close(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
modbus_close(ctx->modbus);
return 0;
}
/**
* Set debug
* @function ctx:set_debug
* @param enable optional bool, defaults to true
*/
static int ctx_set_debug(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
bool opt;
if (lua_isnil(L, -1)) {
opt = true;
} else {
opt = lua_toboolean(L, -1);
}
modbus_set_debug(ctx->modbus, opt);
return 0;
}
/**
* Set error recovery, see modbus_set_error_recovery.
* The arguments will be or'd together.
* @function ctx:set_error_recovery
* @param a one of @{error_recovery_constants}
* @param b one of @{error_recovery_constants}
*/
static int ctx_set_error_recovery(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int opt = luaL_checkinteger(L, 2);
int opt2 = luaL_optinteger(L, 3, 0);
int rc = modbus_set_error_recovery(ctx->modbus, opt | opt2);
return libmodbus_rc_to_nil_error(L, rc, 0);
}
/**
* See also modbus_set_byte_timeout
* @function ctx:set_byte_timeout
* @param seconds (required)
* @param microseconds (optional, defaults to 0)
*/
static int ctx_set_byte_timeout(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int opt = luaL_checkinteger(L, 2);
int opt2 = luaL_optinteger(L, 3, 0);
#if LIBMODBUS_VERSION_CHECK(3,1,0)
modbus_set_byte_timeout(ctx->modbus, opt, opt2);
#else
struct timeval t = { opt, opt2 };
modbus_set_byte_timeout(ctx->modbus, &t);
#endif
return 0;
}
/**
* @function ctx:get_byte_timeout
* @return[1] seconds
* @return[1] microseconds
*/
static int ctx_get_byte_timeout(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
uint32_t opt1, opt2;
#if LIBMODBUS_VERSION_CHECK(3,1,0)
modbus_get_byte_timeout(ctx->modbus, &opt1, &opt2);
#else
struct timeval t;
modbus_get_byte_timeout(ctx->modbus, &t);
opt1 = t.tv_sec;
opt2 = t.tv_usec;
#endif
lua_pushnumber(L, opt1);
lua_pushnumber(L, opt2);
return 2;
}
/**
* @function ctx:set_response_timeout
* @param seconds (required)
* @param microseconds (optional, defaults to 0)
*/
static int ctx_set_response_timeout(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int opt = luaL_checkinteger(L, 2);
int opt2 = luaL_optinteger(L, 3, 0);
#if LIBMODBUS_VERSION_CHECK(3,1,0)
modbus_set_response_timeout(ctx->modbus, opt, opt2);
#else
struct timeval t = { opt, opt2 };
modbus_set_response_timeout(ctx->modbus, &t);
#endif
return 0;
}
/**
* @function ctx:get_response_timeout
* @return[1] seconds
* @return[1] microseconds
*/
static int ctx_get_response_timeout(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
uint32_t opt1, opt2;
#if LIBMODBUS_VERSION_CHECK(3,1,0)
modbus_get_response_timeout(ctx->modbus, &opt1, &opt2);
#else
struct timeval t;
modbus_get_response_timeout(ctx->modbus, &t);
opt1 = t.tv_sec;
opt2 = t.tv_usec;
#endif
lua_pushnumber(L, opt1);
lua_pushnumber(L, opt2);
return 2;
}
/**
* @function ctx:get_socket
* @return the socket number
*/
static int ctx_get_socket(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
lua_pushinteger(L, modbus_get_socket(ctx->modbus));
return 1;
}
/**
* @function ctx:set_socket
* @param sock integer socket number to set
*/
static int ctx_set_socket(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int newfd = luaL_checknumber(L, 2);
modbus_set_socket(ctx->modbus, newfd);
return 0;
}
/**
* @function ctx:rtu_get_serial_mode
* @return @{rtu_constants} the serial mode, either RTU_RS232 or RTU_RS485
*/
static int ctx_rtu_get_serial_mode(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
if (!ctx->is_rtu) {
return luaL_error(L, "Cannot call RTU methods on an TCP context");
}
lua_pushinteger(L, modbus_rtu_get_serial_mode(ctx->modbus));
return 1;
}
/**
* Sets the mode of a serial port.
* Remember, this is only required if your kernel is handling rs485 natively.
* If you are using a USB adapter, you do NOT need this.
* @function ctx:rtu_set_serial_mode
* @param mode The selected serial mode from @{rtu_constants}, either RTU_RS232 or RTU_RS485.
*/
static int ctx_rtu_set_serial_mode(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
if (!ctx->is_rtu) {
return luaL_error(L, "Cannot call RTU methods on an TCP context");
}
int mode = luaL_checknumber(L, 2);
int rc = modbus_rtu_set_serial_mode(ctx->modbus, mode);
return libmodbus_rc_to_nil_error(L, rc, 0);
}
/**
* @function ctx:rtu_get_rts
* @return @{rtu_rts_constants} the RTS handling mode, up/down/none
*/
static int ctx_rtu_get_rts(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
if (!ctx->is_rtu) {
return luaL_error(L, "Cannot call RTU methods on an TCP context");
}
lua_pushinteger(L, modbus_rtu_get_rts(ctx->modbus));
return 1;
}
/**
* Sets the RTS handling of a serial port.
* @function ctx:rtu_set_rts
* @param mode The selected RTS handling mode from @{rtu_rts_constants}, up/down/none.
*/
static int ctx_rtu_set_rts(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
if (!ctx->is_rtu) {
return luaL_error(L, "Cannot call RTU methods on an TCP context");
}
int mode = luaL_checknumber(L, 2);
int rc = modbus_rtu_set_rts(ctx->modbus, mode);
return libmodbus_rc_to_nil_error(L, rc, 0);
}
/**
* @function ctx:rtu_get_rts_delay
* @return usecs
*/
static int ctx_rtu_get_rts_delay(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
if (!ctx->is_rtu) {
return luaL_error(L, "Cannot call RTU methods on an TCP context");
}
lua_pushinteger(L, modbus_rtu_get_rts_delay(ctx->modbus));
return 1;
}
/**
* Sets the RTS delay of a serial port.
* @function ctx:rtu_set_rts_delay
* @param usecs
*/
static int ctx_rtu_set_rts_delay(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
if (!ctx->is_rtu) {
return luaL_error(L, "Cannot call RTU methods on an TCP context");
}
int usecs = luaL_checknumber(L, 2);
int rc = modbus_rtu_set_rts_delay(ctx->modbus, usecs);
return libmodbus_rc_to_nil_error(L, rc, 0);
}
/**
* Returns the header length of the transport
* @function ctx:get_header_length
* @return the length in bytes
*/
static int ctx_get_header_length(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
lua_pushinteger(L, modbus_get_header_length(ctx->modbus));
return 1;
}
/**
* @function ctx:set_slave
* @param unitid the unit address / slave id to use
*/
static int ctx_set_slave(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int slave = luaL_checknumber(L, 2);
int rc = modbus_set_slave(ctx->modbus, slave);
return libmodbus_rc_to_nil_error(L, rc, 0);
}
static int _ctx_read_bits(lua_State *L, bool input)
{
ctx_t *ctx = ctx_check(L, 1);
int addr = luaL_checknumber(L, 2);
int count = luaL_checknumber(L, 3);
int rcount = 0;
int rc;
if (count > MODBUS_MAX_READ_BITS) {
return luaL_argerror(L, 3, "requested too many bits");
}
uint8_t *buf = malloc(count * sizeof(uint8_t));
assert(buf);
if (input) {
rc = modbus_read_input_bits(ctx->modbus, addr, count, buf);
} else {
rc = modbus_read_bits(ctx->modbus, addr, count, buf);
}
if (rc == count) {
lua_newtable(L);
/* nota bene, lua style offsets! */
for (int i = 1; i <= rc; i++) {
lua_pushnumber(L, i);
/* TODO - push number or push bool? what's a better lua api? */
lua_pushnumber(L, buf[i-1]);
lua_settable(L, -3);
}
rcount = 1;
} else {
rcount = libmodbus_rc_to_nil_error(L, rc, count);
}
free(buf);
return rcount;
}
/**
* @function ctx:read_input_bits
* @param address
* @param count
* @return an array of results
*/
static int ctx_read_input_bits(lua_State *L)
{
return _ctx_read_bits(L, true);
}
/**
* @function ctx:read_bits
* @param address
* @param count
* @return an array of results
*/
static int ctx_read_bits(lua_State *L)
{
return _ctx_read_bits(L, false);
}
static int _ctx_read_regs(lua_State *L, bool input)
{
ctx_t *ctx = ctx_check(L, 1);
int addr = luaL_checknumber(L, 2);
int count = luaL_checknumber(L, 3);
int rcount = 0;
int rc;
if (count > MODBUS_MAX_READ_REGISTERS) {
return luaL_argerror(L, 3, "requested too many registers");
}
// better malloc as much space as we need to return data here...
uint16_t *buf = malloc(count * sizeof(uint16_t));
assert(buf);
if (input) {
rc = modbus_read_input_registers(ctx->modbus, addr, count, buf);
} else {
rc = modbus_read_registers(ctx->modbus, addr, count, buf);
}
if (rc == count) {
lua_newtable(L);
/* nota bene, lua style offsets! */
for (int i = 1; i <= rc; i++) {
lua_pushnumber(L, i);
lua_pushnumber(L, buf[i-1]);
lua_settable(L, -3);
}
rcount = 1;
} else {
rcount = libmodbus_rc_to_nil_error(L, rc, count);
}
free(buf);
return rcount;
}
/**
* @function ctx:read_input_registers
* @param address
* @param count
* @return an array of results
*/
static int ctx_read_input_registers(lua_State *L)
{
return _ctx_read_regs(L, true);
}
/**
* @function ctx:read_registers
* @param address
* @param count
* @return an array of results
*/
static int ctx_read_registers(lua_State *L)
{
return _ctx_read_regs(L, false);
}
/**
* @function ctx:report_slave_id
* @return a luastring with the raw result (lua strings can contain nulls)
*/
static int ctx_report_slave_id(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
uint8_t *buf = malloc(ctx->max_len);
assert(buf);
#if LIBMODBUS_VERSION_CHECK(3,1,0)
int rc = modbus_report_slave_id(ctx->modbus, ctx->max_len, buf);
#else
int rc = modbus_report_slave_id(ctx->modbus, buf);
#endif
if (rc < 0) {
return libmodbus_rc_to_nil_error(L, rc, 0);
}
lua_pushlstring(L, (char *)buf, rc);
return 1;
}
/**
* @function ctx:write_bit
* @param address
* @param value either a number or a boolean
*/
static int ctx_write_bit(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int addr = luaL_checknumber(L, 2);
int val;
if (lua_type(L, 3) == LUA_TNUMBER) {
val = lua_tonumber(L, 3);
} else if (lua_type(L, 3) == LUA_TBOOLEAN) {
val = lua_toboolean(L, 3);
} else {
return luaL_argerror(L, 3, "bit must be numeric or boolean");
}
int rc = modbus_write_bit(ctx->modbus, addr, val);
return libmodbus_rc_to_nil_error(L, rc, 1);
}
/**
* @function ctx:write_register
* @param address
* @param value
*/
static int ctx_write_register(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int addr = luaL_checknumber(L, 2);
int val = luaL_checknumber(L, 3);
int rc = modbus_write_register(ctx->modbus, addr, val);
return libmodbus_rc_to_nil_error(L, rc, 1);
}
/**
* @function ctx:write_bits
* @param address
* @param value as a lua array table
*/
static int ctx_write_bits(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int addr = luaL_checknumber(L, 2);
int rc;
int rcount;
/*
* TODO - could allow just a series of arguments too? easier for
* smaller sets?"?)
*/
luaL_checktype(L, 3, LUA_TTABLE);
/* array style table only! */
int count = lua_rawlen(L, 3);
if (count > MODBUS_MAX_WRITE_BITS) {
return luaL_argerror(L, 3, "requested too many bits");
}
/* Convert table to uint8_t array */
uint8_t *buf = malloc(count * sizeof(uint8_t));
assert(buf);
for (int i = 1; i <= count; i++) {
bool ok = false;
lua_rawgeti(L, 3, i);
if (lua_type(L, -1) == LUA_TNUMBER) {
buf[i-1] = lua_tonumber(L, -1);
ok = true;
}
if (lua_type(L, -1) == LUA_TBOOLEAN) {
buf[i-1] = lua_toboolean(L, -1);
ok = true;
}
if (ok) {
lua_pop(L, 1);
} else {
free(buf);
return luaL_argerror(L, 3, "table values must be numeric or bool");
}
}
rc = modbus_write_bits(ctx->modbus, addr, count, buf);
if (rc == count) {
rcount = 1;
lua_pushboolean(L, true);
} else {
rcount = libmodbus_rc_to_nil_error(L, rc, count);