-
Notifications
You must be signed in to change notification settings - Fork 89
/
client.go
1210 lines (970 loc) · 30.1 KB
/
client.go
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
package modbus
import (
"crypto/tls"
"crypto/x509"
"fmt"
"log"
"net"
"os"
"strings"
"sync"
"time"
)
type RegType uint
type Endianness uint
type WordOrder uint
const (
PARITY_NONE uint = 0
PARITY_EVEN uint = 1
PARITY_ODD uint = 2
HOLDING_REGISTER RegType = 0
INPUT_REGISTER RegType = 1
// endianness of 16-bit registers
BIG_ENDIAN Endianness = 1
LITTLE_ENDIAN Endianness = 2
// word order of 32-bit registers
HIGH_WORD_FIRST WordOrder = 1
LOW_WORD_FIRST WordOrder = 2
)
// Modbus client configuration object.
type ClientConfiguration struct {
// URL sets the client mode and target location in the form
// <mode>://<serial device or host:port> e.g. tcp://plc:502
URL string
// Speed sets the serial link speed (in bps, rtu only)
Speed uint
// DataBits sets the number of bits per serial character (rtu only)
DataBits uint
// Parity sets the serial link parity mode (rtu only)
Parity uint
// StopBits sets the number of serial stop bits (rtu only)
StopBits uint
// Timeout sets the request timeout value
Timeout time.Duration
// TLSClientCert sets the client-side TLS key pair (tcp+tls only)
TLSClientCert *tls.Certificate
// TLSRootCAs sets the list of CA certificates used to authenticate
// the server (tcp+tls only). Leaf (i.e. server) certificates can also
// be used in case of self-signed certs, or if cert pinning is required.
TLSRootCAs *x509.CertPool
// Logger provides a custom sink for log messages.
// If nil, messages will be written to stdout.
Logger *log.Logger
}
// Modbus client object.
type ModbusClient struct {
conf ClientConfiguration
logger *logger
lock sync.Mutex
endianness Endianness
wordOrder WordOrder
transport transport
unitId uint8
transportType transportType
}
// NewClient creates, configures and returns a modbus client object.
func NewClient(conf *ClientConfiguration) (mc *ModbusClient, err error) {
var clientType string
var splitURL []string
mc = &ModbusClient{
conf: *conf,
}
splitURL = strings.SplitN(mc.conf.URL, "://", 2)
if len(splitURL) == 2 {
clientType = splitURL[0]
mc.conf.URL = splitURL[1]
}
mc.logger = newLogger(
fmt.Sprintf("modbus-client(%s)", mc.conf.URL), conf.Logger)
switch clientType {
case "rtu":
// set useful defaults
if mc.conf.Speed == 0 {
mc.conf.Speed = 19200
}
// note: the "modbus over serial line v1.02" document specifies an
// 11-bit character frame, with even parity and 1 stop bit as default,
// and mandates the use of 2 stop bits when no parity is used.
// This stack defaults to 8/N/2 as most devices seem to use no parity,
// but giving 8/N/1, 8/E/1 and 8/O/1 a shot may help with serial
// issues.
if mc.conf.DataBits == 0 {
mc.conf.DataBits = 8
}
if mc.conf.StopBits == 0 {
if mc.conf.Parity == PARITY_NONE {
mc.conf.StopBits = 2
} else {
mc.conf.StopBits = 1
}
}
if mc.conf.Timeout == 0 {
mc.conf.Timeout = 300 * time.Millisecond
}
mc.transportType = modbusRTU
case "rtuovertcp":
if mc.conf.Speed == 0 {
mc.conf.Speed = 19200
}
if mc.conf.Timeout == 0 {
mc.conf.Timeout = 1 * time.Second
}
mc.transportType = modbusRTUOverTCP
case "rtuoverudp":
if mc.conf.Speed == 0 {
mc.conf.Speed = 19200
}
if mc.conf.Timeout == 0 {
mc.conf.Timeout = 1 * time.Second
}
mc.transportType = modbusRTUOverUDP
case "tcp":
if mc.conf.Timeout == 0 {
mc.conf.Timeout = 1 * time.Second
}
mc.transportType = modbusTCP
case "tcp+tls":
if mc.conf.Timeout == 0 {
mc.conf.Timeout = 1 * time.Second
}
// expect a client-side certificate for mutual auth as the
// modbus/mpab protocol has no inherent auth facility.
// (see requirements R-08 and R-19 of the MBAPS spec)
if mc.conf.TLSClientCert == nil {
mc.logger.Errorf("missing client certificate")
err = ErrConfigurationError
return
}
// expect a CertPool object containing at least 1 CA or
// leaf certificate to validate the server-side cert
if mc.conf.TLSRootCAs == nil {
mc.logger.Errorf("missing CA/server certificate")
err = ErrConfigurationError
return
}
mc.transportType = modbusTCPOverTLS
case "udp":
if mc.conf.Timeout == 0 {
mc.conf.Timeout = 1 * time.Second
}
mc.transportType = modbusTCPOverUDP
default:
if len(splitURL) != 2 {
mc.logger.Errorf("missing client type in URL '%s'", mc.conf.URL)
} else {
mc.logger.Errorf("unsupported client type '%s'", clientType)
}
err = ErrConfigurationError
return
}
mc.unitId = 1
mc.endianness = BIG_ENDIAN
mc.wordOrder = HIGH_WORD_FIRST
return
}
// Opens the underlying transport (network socket or serial line).
func (mc *ModbusClient) Open() (err error) {
var spw *serialPortWrapper
var sock net.Conn
mc.lock.Lock()
defer mc.lock.Unlock()
switch mc.transportType {
case modbusRTU:
// create a serial port wrapper object
spw = newSerialPortWrapper(&serialPortConfig{
Device: mc.conf.URL,
Speed: mc.conf.Speed,
DataBits: mc.conf.DataBits,
Parity: mc.conf.Parity,
StopBits: mc.conf.StopBits,
})
// open the serial device
err = spw.Open()
if err != nil {
return
}
// discard potentially stale serial data
discard(spw)
// create the RTU transport
mc.transport = newRTUTransport(
spw, mc.conf.URL, mc.conf.Speed, mc.conf.Timeout, mc.conf.Logger)
case modbusRTUOverTCP:
// connect to the remote host
sock, err = net.DialTimeout("tcp", mc.conf.URL, 5 * time.Second)
if err != nil {
return
}
// discard potentially stale serial data
discard(sock)
// create the RTU transport
mc.transport = newRTUTransport(
sock, mc.conf.URL, mc.conf.Speed, mc.conf.Timeout, mc.conf.Logger)
case modbusRTUOverUDP:
// open a socket to the remote host (note: no actual connection is
// being made as UDP is connection-less)
sock, err = net.DialTimeout("udp", mc.conf.URL, 5 * time.Second)
if err != nil {
return
}
// create the RTU transport, wrapping the UDP socket in
// an adapter to allow the transport to read the stream of
// packets byte per byte
mc.transport = newRTUTransport(
newUDPSockWrapper(sock),
mc.conf.URL, mc.conf.Speed, mc.conf.Timeout, mc.conf.Logger)
case modbusTCP:
// connect to the remote host
sock, err = net.DialTimeout("tcp", mc.conf.URL, 5 * time.Second)
if err != nil {
return
}
// create the TCP transport
mc.transport = newTCPTransport(sock, mc.conf.Timeout, mc.conf.Logger)
case modbusTCPOverTLS:
// connect to the remote host with TLS
sock, err = tls.DialWithDialer(
&net.Dialer{
Deadline: time.Now().Add(15 * time.Second),
}, "tcp", mc.conf.URL,
&tls.Config{
Certificates: []tls.Certificate{
*mc.conf.TLSClientCert,
},
RootCAs: mc.conf.TLSRootCAs,
// mandate TLS 1.2 or higher (see R-01 of the MBAPS spec)
MinVersion: tls.VersionTLS12,
})
if err != nil {
return
}
// force the TLS handshake
err = sock.(*tls.Conn).Handshake()
if err != nil {
sock.Close()
return
}
// create the TCP transport, wrapping the TLS socket in
// an adapter to work around write timeouts corrupting internal
// state (see https://pkg.go.dev/crypto/tls#Conn.SetWriteDeadline)
mc.transport = newTCPTransport(
newTLSSockWrapper(sock), mc.conf.Timeout, mc.conf.Logger)
case modbusTCPOverUDP:
// open a socket to the remote host (note: no actual connection is
// being made as UDP is connection-less)
sock, err = net.DialTimeout("udp", mc.conf.URL, 5 * time.Second)
if err != nil {
return
}
// create the TCP transport, wrapping the UDP socket in
// an adapter to allow the transport to read the stream of
// packets byte per byte
mc.transport = newTCPTransport(
newUDPSockWrapper(sock), mc.conf.Timeout, mc.conf.Logger)
default:
// should never happen
err = ErrConfigurationError
}
return
}
// Closes the underlying transport.
func (mc *ModbusClient) Close() (err error) {
mc.lock.Lock()
defer mc.lock.Unlock()
if mc.transport != nil {
err = mc.transport.Close()
}
return
}
// Sets the unit id of subsequent requests.
func (mc *ModbusClient) SetUnitId(id uint8) (err error) {
mc.lock.Lock()
defer mc.lock.Unlock()
mc.unitId = id
return
}
// Sets the encoding (endianness and word ordering) of subsequent requests.
func (mc *ModbusClient) SetEncoding(endianness Endianness, wordOrder WordOrder) (err error) {
mc.lock.Lock()
defer mc.lock.Unlock()
if endianness != BIG_ENDIAN && endianness != LITTLE_ENDIAN {
mc.logger.Errorf("unknown endianness value %v", endianness)
err = ErrUnexpectedParameters
return
}
if wordOrder != HIGH_WORD_FIRST && wordOrder != LOW_WORD_FIRST {
mc.logger.Errorf("unknown word order value %v", wordOrder)
err = ErrUnexpectedParameters
return
}
mc.endianness = endianness
mc.wordOrder = wordOrder
return
}
// Reads multiple coils (function code 01).
func (mc *ModbusClient) ReadCoils(addr uint16, quantity uint16) (values []bool, err error) {
values, err = mc.readBools(addr, quantity, false)
return
}
// Reads a single coil (function code 01).
func (mc *ModbusClient) ReadCoil(addr uint16) (value bool, err error) {
var values []bool
values, err = mc.readBools(addr, 1, false)
if err == nil {
value = values[0]
}
return
}
// Reads multiple discrete inputs (function code 02).
func (mc *ModbusClient) ReadDiscreteInputs(addr uint16, quantity uint16) (values []bool, err error) {
values, err = mc.readBools(addr, quantity, true)
return
}
// Reads a single discrete input (function code 02).
func (mc *ModbusClient) ReadDiscreteInput(addr uint16) (value bool, err error) {
var values []bool
values, err = mc.readBools(addr, 1, true)
if err == nil {
value = values[0]
}
return
}
// Reads multiple 16-bit registers (function code 03 or 04).
func (mc *ModbusClient) ReadRegisters(addr uint16, quantity uint16, regType RegType) (values []uint16, err error) {
var mbPayload []byte
// read quantity uint16 registers, as bytes
mbPayload, err = mc.readRegisters(addr, quantity, regType)
if err != nil {
return
}
// decode payload bytes as uint16s
values = bytesToUint16s(mc.endianness, mbPayload)
return
}
// Reads a single 16-bit register (function code 03 or 04).
func (mc *ModbusClient) ReadRegister(addr uint16, regType RegType) (value uint16, err error) {
var values []uint16
// read 1 uint16 register, as bytes
values, err = mc.ReadRegisters(addr, 1, regType)
if err == nil {
value = values[0]
}
return
}
// Reads multiple 32-bit registers.
func (mc *ModbusClient) ReadUint32s(addr uint16, quantity uint16, regType RegType) (values []uint32, err error) {
var mbPayload []byte
// read 2 * quantity uint16 registers, as bytes
mbPayload, err = mc.readRegisters(addr, quantity * 2, regType)
if err != nil {
return
}
// decode payload bytes as uint32s
values = bytesToUint32s(mc.endianness, mc.wordOrder, mbPayload)
return
}
// Reads a single 32-bit register.
func (mc *ModbusClient) ReadUint32(addr uint16, regType RegType) (value uint32, err error) {
var values []uint32
values, err = mc.ReadUint32s(addr, 1, regType)
if err == nil {
value = values[0]
}
return
}
// Reads multiple 32-bit float registers.
func (mc *ModbusClient) ReadFloat32s(addr uint16, quantity uint16, regType RegType) (values []float32, err error) {
var mbPayload []byte
// read 2 * quantity uint16 registers, as bytes
mbPayload, err = mc.readRegisters(addr, quantity * 2, regType)
if err != nil {
return
}
// decode payload bytes as float32s
values = bytesToFloat32s(mc.endianness, mc.wordOrder, mbPayload)
return
}
// Reads a single 32-bit float register.
func (mc *ModbusClient) ReadFloat32(addr uint16, regType RegType) (value float32, err error) {
var values []float32
values, err = mc.ReadFloat32s(addr, 1, regType)
if err == nil {
value = values[0]
}
return
}
// Reads multiple 64-bit registers.
func (mc *ModbusClient) ReadUint64s(addr uint16, quantity uint16, regType RegType) (values []uint64, err error) {
var mbPayload []byte
// read 4 * quantity uint16 registers, as bytes
mbPayload, err = mc.readRegisters(addr, quantity * 4, regType)
if err != nil {
return
}
// decode payload bytes as uint64s
values = bytesToUint64s(mc.endianness, mc.wordOrder, mbPayload)
return
}
// Reads a single 64-bit register.
func (mc *ModbusClient) ReadUint64(addr uint16, regType RegType) (value uint64, err error) {
var values []uint64
values, err = mc.ReadUint64s(addr, 1, regType)
if err == nil {
value = values[0]
}
return
}
// Reads multiple 64-bit float registers.
func (mc *ModbusClient) ReadFloat64s(addr uint16, quantity uint16, regType RegType) (values []float64, err error) {
var mbPayload []byte
// read 4 * quantity uint16 registers, as bytes
mbPayload, err = mc.readRegisters(addr, quantity * 4, regType)
if err != nil {
return
}
// decode payload bytes as float64s
values = bytesToFloat64s(mc.endianness, mc.wordOrder, mbPayload)
return
}
// Reads a single 64-bit float register.
func (mc *ModbusClient) ReadFloat64(addr uint16, regType RegType) (value float64, err error) {
var values []float64
values, err = mc.ReadFloat64s(addr, 1, regType)
if err == nil {
value = values[0]
}
return
}
// Reads one or multiple 16-bit registers (function code 03 or 04) as bytes.
// A per-register byteswap is performed if endianness is set to LITTLE_ENDIAN.
func (mc *ModbusClient) ReadBytes(addr uint16, quantity uint16, regType RegType) (values []byte, err error) {
values, err = mc.readBytes(addr, quantity, regType, true)
return
}
// Reads one or multiple 16-bit registers (function code 03 or 04) as bytes.
// No byte or word reordering is performed: bytes are returned exactly as they come
// off the wire, allowing the caller to handle encoding/endianness/word order manually.
func (mc *ModbusClient) ReadRawBytes(addr uint16, quantity uint16, regType RegType) (values []byte, err error) {
values, err = mc.readBytes(addr, quantity, regType, false)
return
}
// Writes a single coil (function code 05)
func (mc *ModbusClient) WriteCoil(addr uint16, value bool) (err error) {
var req *pdu
var res *pdu
mc.lock.Lock()
defer mc.lock.Unlock()
// create and fill in the request object
req = &pdu{
unitId: mc.unitId,
functionCode: fcWriteSingleCoil,
}
// coil address
req.payload = uint16ToBytes(BIG_ENDIAN, addr)
// coil value
if value {
req.payload = append(req.payload, 0xff, 0x00)
} else {
req.payload = append(req.payload, 0x00, 0x00)
}
// run the request across the transport and wait for a response
res, err = mc.executeRequest(req)
if err != nil {
return
}
// validate the response code
switch {
case res.functionCode == req.functionCode:
// expect 4 bytes (2 byte of address + 2 bytes of value)
if len(res.payload) != 4 ||
// bytes 1-2 should be the coil address
bytesToUint16(BIG_ENDIAN, res.payload[0:2]) != addr ||
// bytes 3-4 should either be {0xff, 0x00} or {0x00, 0x00}
// depending on the coil value
(value == true && res.payload[2] != 0xff) ||
res.payload[3] != 0x00 {
err = ErrProtocolError
return
}
case res.functionCode == (req.functionCode | 0x80):
if len(res.payload) != 1 {
err = ErrProtocolError
return
}
err = mapExceptionCodeToError(res.payload[0])
default:
err = ErrProtocolError
mc.logger.Warningf("unexpected response code (%v)", res.functionCode)
}
return
}
// Writes multiple coils (function code 15)
func (mc *ModbusClient) WriteCoils(addr uint16, values []bool) (err error) {
var req *pdu
var res *pdu
var quantity uint16
var encodedValues []byte
mc.lock.Lock()
defer mc.lock.Unlock()
quantity = uint16(len(values))
if quantity == 0 {
err = ErrUnexpectedParameters
mc.logger.Error("quantity of coils is 0")
return
}
if quantity > 0x7b0 {
err = ErrUnexpectedParameters
mc.logger.Error("quantity of coils exceeds 1968")
return
}
if uint32(addr) + uint32(quantity) - 1 > 0xffff {
err = ErrUnexpectedParameters
mc.logger.Error("end coil address is past 0xffff")
return
}
encodedValues = encodeBools(values)
// create and fill in the request object
req = &pdu{
unitId: mc.unitId,
functionCode: fcWriteMultipleCoils,
}
// start address
req.payload = uint16ToBytes(BIG_ENDIAN, addr)
// quantity
req.payload = append(req.payload, uint16ToBytes(BIG_ENDIAN, quantity)...)
// byte count
req.payload = append(req.payload, byte(len(encodedValues)))
// payload
req.payload = append(req.payload, encodedValues...)
// run the request across the transport and wait for a response
res, err = mc.executeRequest(req)
if err != nil {
return
}
// validate the response code
switch {
case res.functionCode == req.functionCode:
// expect 4 bytes (2 byte of address + 2 bytes of quantity)
if len(res.payload) != 4 ||
// bytes 1-2 should be the base coil address
bytesToUint16(BIG_ENDIAN, res.payload[0:2]) != addr ||
// bytes 3-4 should be the quantity of coils
bytesToUint16(BIG_ENDIAN, res.payload[2:4]) != quantity {
err = ErrProtocolError
return
}
case res.functionCode == (req.functionCode | 0x80):
if len(res.payload) != 1 {
err = ErrProtocolError
return
}
err = mapExceptionCodeToError(res.payload[0])
default:
err = ErrProtocolError
mc.logger.Warningf("unexpected response code (%v)", res.functionCode)
}
return
}
// Writes a single 16-bit register (function code 06).
func (mc *ModbusClient) WriteRegister(addr uint16, value uint16) (err error) {
var req *pdu
var res *pdu
mc.lock.Lock()
defer mc.lock.Unlock()
// create and fill in the request object
req = &pdu{
unitId: mc.unitId,
functionCode: fcWriteSingleRegister,
}
// register address
req.payload = uint16ToBytes(BIG_ENDIAN, addr)
// register value
req.payload = append(req.payload, uint16ToBytes(mc.endianness, value)...)
// run the request across the transport and wait for a response
res, err = mc.executeRequest(req)
if err != nil {
return
}
// validate the response code
switch {
case res.functionCode == req.functionCode:
// expect 4 bytes (2 byte of address + 2 bytes of value)
if len(res.payload) != 4 ||
// bytes 1-2 should be the register address
bytesToUint16(BIG_ENDIAN, res.payload[0:2]) != addr ||
// bytes 3-4 should be the value
bytesToUint16(mc.endianness, res.payload[2:4]) != value {
err = ErrProtocolError
return
}
case res.functionCode == (req.functionCode | 0x80):
if len(res.payload) != 1 {
err = ErrProtocolError
return
}
err = mapExceptionCodeToError(res.payload[0])
default:
err = ErrProtocolError
mc.logger.Warningf("unexpected response code (%v)", res.functionCode)
}
return
}
// Writes multiple 16-bit registers (function code 16).
func (mc *ModbusClient) WriteRegisters(addr uint16, values []uint16) (err error) {
var payload []byte
// turn registers to bytes
for _, value := range values {
payload = append(payload, uint16ToBytes(mc.endianness, value)...)
}
err = mc.writeRegisters(addr, payload)
return
}
// Writes multiple 32-bit registers.
func (mc *ModbusClient) WriteUint32s(addr uint16, values []uint32) (err error) {
var payload []byte
// turn registers to bytes
for _, value := range values {
payload = append(payload, uint32ToBytes(mc.endianness, mc.wordOrder, value)...)
}
err = mc.writeRegisters(addr, payload)
return
}
// Writes a single 32-bit register.
func (mc *ModbusClient) WriteUint32(addr uint16, value uint32) (err error) {
err = mc.writeRegisters(addr, uint32ToBytes(mc.endianness, mc.wordOrder, value))
return
}
// Writes multiple 32-bit float registers.
func (mc *ModbusClient) WriteFloat32s(addr uint16, values []float32) (err error) {
var payload []byte
// turn registers to bytes
for _, value := range values {
payload = append(payload, float32ToBytes(mc.endianness, mc.wordOrder, value)...)
}
err = mc.writeRegisters(addr, payload)
return
}
// Writes a single 32-bit float register.
func (mc *ModbusClient) WriteFloat32(addr uint16, value float32) (err error) {
err = mc.writeRegisters(addr, float32ToBytes(mc.endianness, mc.wordOrder, value))
return
}
// Writes multiple 64-bit registers.
func (mc *ModbusClient) WriteUint64s(addr uint16, values []uint64) (err error) {
var payload []byte
// turn registers to bytes
for _, value := range values {
payload = append(payload, uint64ToBytes(mc.endianness, mc.wordOrder, value)...)
}
err = mc.writeRegisters(addr, payload)
return
}
// Writes a single 64-bit register.
func (mc *ModbusClient) WriteUint64(addr uint16, value uint64) (err error) {
err = mc.writeRegisters(addr, uint64ToBytes(mc.endianness, mc.wordOrder, value))
return
}
// Writes multiple 64-bit float registers.
func (mc *ModbusClient) WriteFloat64s(addr uint16, values []float64) (err error) {
var payload []byte
// turn registers to bytes
for _, value := range values {
payload = append(payload, float64ToBytes(mc.endianness, mc.wordOrder, value)...)
}
err = mc.writeRegisters(addr, payload)
return
}
// Writes a single 64-bit float register.
func (mc *ModbusClient) WriteFloat64(addr uint16, value float64) (err error) {
err = mc.writeRegisters(addr, float64ToBytes(mc.endianness, mc.wordOrder, value))
return
}
// Writes the given slice of bytes to 16-bit registers starting at addr.
// A per-register byteswap is performed if endianness is set to LITTLE_ENDIAN.
// Odd byte quantities are padded with a null byte to fall on 16-bit register boundaries.
func (mc *ModbusClient) WriteBytes(addr uint16, values []byte) (err error) {
err = mc.writeBytes(addr, values, true)
return
}
// Writes the given slice of bytes to 16-bit registers starting at addr.
// No byte or word reordering is performed: bytes are pushed to the wire as-is,
// allowing the caller to handle encoding/endianness/word order manually.
// Odd byte quantities are padded with a null byte to fall on 16-bit register boundaries.
func (mc *ModbusClient) WriteRawBytes(addr uint16, values []byte) (err error) {
err = mc.writeBytes(addr, values, false)
return
}
/*** unexported methods ***/
// Reads one or multiple 16-bit registers (function code 03 or 04) as bytes.
func (mc *ModbusClient) readBytes(addr uint16, quantity uint16, regType RegType, observeEndianness bool) (values []byte, err error) {
var regCount uint16
// read enough registers to get the requested number of bytes
// (2 bytes per reg)
regCount = (quantity / 2) + (quantity % 2)
values, err = mc.readRegisters(addr, regCount, regType)
if err != nil {
return
}
// swap bytes on register boundaries if requested by the caller
// and endianness is set to little endian
if observeEndianness && mc.endianness == LITTLE_ENDIAN {
for i := 0; i < len(values); i += 2 {
values[i], values[i+1] = values[i+1], values[i]
}
}
// pop the last byte on odd quantities
if quantity % 2 == 1 {
values = values[0:len(values) - 1]
}
return
}
// Writes the given slice of bytes to 16-bit registers starting at addr.
func (mc *ModbusClient) writeBytes(addr uint16, values []byte, observeEndianness bool) (err error) {
// pad odd quantities to make for full registers
if len(values) % 2 == 1 {
values = append(values, 0x00)
}
// swap bytes on register boundaries if requested by the caller
// and endianness is set to little endian
if observeEndianness && mc.endianness == LITTLE_ENDIAN {
for i := 0; i < len(values); i += 2 {
values[i], values[i+1] = values[i+1], values[i]
}
}
err = mc.writeRegisters(addr, values)
return
}
// Reads and returns quantity booleans.
// Digital inputs are read if di is true, otherwise coils are read.
func (mc *ModbusClient) readBools(addr uint16, quantity uint16, di bool) (values []bool, err error) {
var req *pdu
var res *pdu
var expectedLen int
mc.lock.Lock()
defer mc.lock.Unlock()
if quantity == 0 {
err = ErrUnexpectedParameters
mc.logger.Error("quantity of coils/discrete inputs is 0")
return
}
if quantity > 2000 {
err = ErrUnexpectedParameters
mc.logger.Error("quantity of coils/discrete inputs exceeds 2000")
return
}
if uint32(addr) + uint32(quantity) - 1 > 0xffff {
err = ErrUnexpectedParameters
mc.logger.Error("end coil/discrete input address is past 0xffff")
return
}
// create and fill in the request object
req = &pdu{
unitId: mc.unitId,
}
if di {
req.functionCode = fcReadDiscreteInputs
} else {
req.functionCode = fcReadCoils
}
// start address
req.payload = uint16ToBytes(BIG_ENDIAN, addr)
// quantity
req.payload = append(req.payload, uint16ToBytes(BIG_ENDIAN, quantity)...)
// run the request across the transport and wait for a response
res, err = mc.executeRequest(req)
if err != nil {
return
}
// validate the response code
switch {
case res.functionCode == req.functionCode:
// expect a payload of 1 byte (byte count) + 1 byte for 8 coils/discrete inputs)
expectedLen = 1
expectedLen += int(quantity) / 8
if quantity % 8 != 0 {
expectedLen++
}
if len(res.payload) != expectedLen {
err = ErrProtocolError
return
}
// validate the byte count field
if int(res.payload[0]) + 1 != expectedLen {
err = ErrProtocolError
return
}
// turn bits into a bool slice
values = decodeBools(quantity, res.payload[1:])