forked from robinson/gos7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
539 lines (490 loc) · 15 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
package gos7
// Copyright 2018 Trung Hieu Le. All rights reserved.
// This software may be modified and distributed under the terms
// of the BSD license. See the LICENSE file for details.
import (
"encoding/binary"
"fmt"
"strconv"
"strings"
)
const (
// Area ID
s7areape = 0x81 //process inputs
s7areapa = 0x82 //process outputs
s7areamk = 0x83 //Merkers
s7areadb = 0x84 //DB
s7areact = 0x1C //counters
s7areatm = 0x1D //timers
// Word Length
s7wlbit = 0x01 //Bit (inside a word)
s7wlbyte = 0x02 //Byte (8 bit)
s7wlChar = 0x03
s7wlword = 0x04 //Word (16 bit)
s7wlint = 0x05
s7wldword = 0x06 //Double Word (32 bit)
s7wldint = 0x07
s7wlreal = 0x08 //Real (32 bit float)
s7wlcounter = 0x1C //Counter (16 bit)
s7wltimer = 0x1D //Timer (16 bit)
// PLC Status
s7CpuStatusUnknown = 0
s7CpuStatusRun = 8
s7CpuStatusStop = 4
//size header
sizeHeaderRead int = 31 // Header Size when Reading
sizeHeaderWrite int = 35 // Header Size when Writing
// Result transport size
tsResBit = 3
tsResByte = 4
tsResInt = 5
tsResReal = 7
tsResOctet = 9
)
//PDULength variable to store pdu length after connect
//var tt, _ := mb.transporter.(*tcpTransporter)tt, _ := mb.transporter.(*tcpTransporter) int //global variable pdulength
// CliePDULengthntHandler is the interface that groups the Packager and Transporter methods.
type ClientHandler interface {
Packager
Transporter
}
type client struct {
packager Packager
transporter Transporter
}
// NewClient creates a new s7 client with given backend handler.
func NewClient(handler ClientHandler) Client {
return &client{packager: handler, transporter: handler}
}
// NewClient2 creates a new s7 client with given backend packager and transporter.
func NewClient2(packager Packager, transporter Transporter) Client {
return &client{packager: packager, transporter: transporter}
}
//implement of the interface AGReadDB
func (mb *client) AGReadDB(dbnumber int, start int, size int, buffer []byte) (err error) {
return mb.readArea(s7areadb, dbnumber, start, size, s7wlbyte, buffer)
}
//implement of the interface AGWriteDB
func (mb *client) AGWriteDB(dbNumber int, start int, size int, buffer []byte) (err error) {
return mb.writeArea(s7areadb, dbNumber, start, size, s7wlbyte, buffer)
}
//implement of the interface AGReadMB
func (mb *client) AGReadMB(start int, size int, buffer []byte) (err error) {
return mb.readArea(s7areamk, 0, start, size, s7wlbyte, buffer)
}
//implement of the interface AGWriteMB
func (mb *client) AGWriteMB(start int, size int, buffer []byte) (err error) {
return mb.writeArea(s7areamk, 0, start, size, s7wlbyte, buffer)
}
//implement of the interface AGReadEB
func (mb *client) AGReadEB(start int, size int, buffer []byte) (err error) {
return mb.readArea(s7areape, 0, start, size, s7wlbyte, buffer)
}
//implement of the interface AGWriteEB
func (mb *client) AGWriteEB(start int, size int, buffer []byte) (err error) {
return mb.writeArea(s7areape, 0, start, size, s7wlbyte, buffer)
}
//implement of the interface AGReadAB
func (mb *client) AGReadAB(start int, size int, buffer []byte) (err error) {
return mb.readArea(s7areapa, 0, start, size, s7wlbyte, buffer)
}
//implement of the interface AGWriteAB
func (mb *client) AGWriteAB(start int, size int, buffer []byte) (err error) {
return mb.writeArea(s7areapa, 0, start, size, s7wlbyte, buffer)
}
//implement of the interface AGReadTM - read timer
func (mb *client) AGReadTM(start int, amount int, buffer []byte) (err error) {
sbuffer := make([]byte, amount*2)
err = mb.readArea(s7areatm, 0, start, amount, s7wltimer, sbuffer)
if err == nil {
for c := 0; c < amount; c++ {
buffer[c] = byte(uint16(sbuffer[c*2+1])<<8 + uint16(sbuffer[c*2]))
}
}
return err
}
//implement of the interface AGWriteTM - write timer
func (mb *client) AGWriteTM(start int, amount int, buffer []byte) (err error) {
sbuffer := make([]byte, amount*2)
for c := 0; c < amount; c++ {
sbuffer[c*2+1] = byte((uint(buffer[c]) & uint(0xFF00)) >> 8)
sbuffer[c*2] = byte(buffer[c] & 0x00FF)
}
err = mb.writeArea(s7areatm, 0, start, amount, s7wltimer, sbuffer)
return err
}
//implement of the interface AGReadCT - read counter
func (mb *client) AGReadCT(start int, amount int, buffer []byte) (err error) {
sbuffer := make([]byte, amount*2)
err = mb.readArea(s7areact, 0, start, amount, s7wlcounter, sbuffer)
if err == nil {
for c := 0; c < amount; c++ {
buffer[c] = byte(uint(sbuffer[c*2+1])<<8 + uint(sbuffer[c*2]))
}
}
return err
}
//implement of the interface AGWriteCT - write counter
func (mb *client) AGWriteCT(start int, amount int, buffer []byte) (err error) {
sbuffer := make([]byte, amount*2)
for c := 0; c < amount; c++ {
sbuffer[c*2+1] = byte((uint(buffer[c]) & uint(0xFF00)) >> 8)
sbuffer[c*2] = byte(buffer[c] & 0x00FF)
}
err = mb.writeArea(s7areact, 0, start, amount, s7wlcounter, sbuffer)
return err
}
//read generic area, pass result into a buffer
func (mb *client) readArea(area int, dbNumber int, start int, amount int, wordLen int, buffer []byte) (err error) {
var address, numElements, maxElements, totElements, sizeRequested int
offset := 0
wordSize := 1
// Some adjustment
if area == s7areact {
wordLen = s7wlcounter
}
if area == s7areatm {
wordLen = s7wltimer
}
// Calc Word size
wordSize = dataSizeByte(wordLen)
if wordSize == 0 {
return fmt.Errorf(ErrorText(errIsoInvalidDataSize))
}
if wordLen == s7wlbit {
amount = 1 // Only 1 bit can be transferred at time
} else {
if wordLen != s7wlcounter && wordLen != s7wltimer {
amount = amount * wordSize
wordSize = 1
wordLen = s7wlbyte
}
}
tt, _ := interface{}(mb.transporter).(*TCPClientHandler)
maxElements = (tt.PDULength - 18) / wordSize // 18 = Reply telegram header //lth note here
totElements = amount
for totElements > 0 && err == nil {
numElements = totElements
if numElements > maxElements {
numElements = maxElements
}
sizeRequested = numElements * wordSize
// Setup the telegram
requestData := make([]byte, sizeHeaderRead)
copy(requestData[0:], s7ReadWriteTelegram[0:])
request := NewProtocolDataUnit(requestData)
// Set DB Number
request.Data[27] = byte(area)
// Set Area
if area == s7areadb {
binary.BigEndian.PutUint16(request.Data[25:], uint16(dbNumber))
//SetWordAt(request.Data, 25, uint16(DBNumber))
}
// Adjusts Start and word length
if wordLen == s7wlbit || wordLen == s7wlcounter || wordLen == s7wltimer {
address = start
request.Data[22] = byte(wordLen)
} else {
address = start << 3
}
// Num elements
binary.BigEndian.PutUint16(request.Data[23:], uint16(numElements))
//SetWordAt(request.Data, 23, uint16(numElements))
// Address into the PLC (only 3 bytes)
request.Data[30] = byte(address & 0x0FF)
address = address >> 8
request.Data[29] = byte(address & 0x0FF)
address = address >> 8
request.Data[28] = byte(address & 0x0FF)
var response *ProtocolDataUnit
response, sendError := mb.send(&request)
err = sendError
if err == nil {
if size := len(response.Data); size < 25 {
err = fmt.Errorf(ErrorText(errIsoInvalidDataSize)+"'%v'", len(response.Data))
} else {
if response.Data[21] != 0xFF {
err = fmt.Errorf(ErrorText(CPUError(uint(response.Data[21]))))
} else {
//copy response to buffer
copy(buffer[offset:offset+sizeRequested], response.Data[25:25+sizeRequested])
offset += sizeRequested
}
}
}
totElements -= numElements
start += numElements * wordSize
}
return
}
//writeArea write generic area into PLC with following parameters:
//1.area: s7areape/s7areapa/s7areamk/s7areadb/s7areact/s7areatm
//2.dbnumber: specify dbnumber, to use in write DB area, otherwise = 0
//3.start: start of the address
//4.amount: amount of the address
//5.wordlen: bit/byte/word/dword/real/counter/timer
//6.buffer: a byte array input for writing
func (mb *client) writeArea(area int, dbnumber int, start int, amount int, wordlen int, buffer []byte) (err error) {
var address, numElements, maxElements, totElements, dataSize, isoSize, length int
offset := 0
wordSize := 1
// Some adjustment
if area == s7areact {
wordlen = s7wlcounter
}
if area == s7areatm {
wordlen = s7wltimer
}
// Calc Word size
wordSize = dataSizeByte(wordlen)
if wordSize == 0 {
return fmt.Errorf(ErrorText(errIsoInvalidDataSize))
}
if wordlen == s7wlbit {
amount = 1 // Only 1 bit can be transferred at time
} else {
if wordlen != s7wlcounter && wordlen != s7wltimer {
amount = amount * wordSize
wordSize = 1
wordlen = s7wlbyte
}
}
tt, _ := interface{}(mb.transporter).(*TCPClientHandler)
maxElements = (tt.PDULength - 35) / wordSize // 35 = Reply telegram header
totElements = amount
for totElements > 0 && err == nil {
numElements = totElements
if numElements > maxElements {
numElements = maxElements
}
dataSize = numElements * wordSize
isoSize = sizeHeaderWrite + dataSize
// Setup the telegram
requestData := make([]byte, sizeHeaderWrite)
copy(requestData[0:], s7ReadWriteTelegram[0:])
request := NewProtocolDataUnit(requestData)
// Whole telegram Size
binary.BigEndian.PutUint16(request.Data[2:], uint16(isoSize))
//SetWordAt(request.Data, 2, uint16(isoSize))
// Data length
length = dataSize + 4
binary.BigEndian.PutUint16(request.Data[15:], uint16(length))
// SetWordAt(request.Data, 15, uint16(length))
// Function
request.Data[17] = byte(0x05)
// Set DB Number
request.Data[27] = byte(area)
if area == s7areadb {
binary.BigEndian.PutUint16(request.Data[25:], uint16(dbnumber))
//SetWordAt(request.Data, 25, uint16(dbnumber))
}
// Adjusts start and word length
if wordlen == s7wlbit || wordlen == s7wlcounter || wordlen == s7wltimer {
address = start
length = dataSize
request.Data[22] = byte(wordlen)
} else {
address = start << 3
length = dataSize << 3
}
// Num elements
binary.BigEndian.PutUint16(request.Data[23:], uint16(numElements))
// SetWordAt(request.Data, 23, uint16(numElements))
// address into the PLC
request.Data[30] = byte(address & 0x0FF)
address = address >> 8
request.Data[29] = byte(address & 0x0FF)
address = address >> 8
request.Data[28] = byte(address & 0x0FF)
// Transport Size
switch wordlen {
case s7wlbit:
request.Data[32] = tsResBit
break
case s7wlcounter:
case s7wltimer:
request.Data[32] = tsResOctet
break
default:
request.Data[32] = tsResByte // byte/word/dword etc.
break
}
// length
// SetWordAt(request.Data, 33, uint16(length))
binary.BigEndian.PutUint16(request.Data[33:], uint16(length))
//expand values into array
request.Data = append(request.Data[:35], append(buffer[offset:offset+dataSize], request.Data[35:]...)...)
response, sendError := mb.send(&request)
err = sendError
if err == nil {
if length = len(response.Data); length == 22 {
if response.Data[21] != byte(0xFF) {
err = fmt.Errorf(ErrorText(CPUError(uint(response.Data[21]))))
}
} else {
err = fmt.Errorf(ErrorText(errIsoInvalidPDU))
}
}
offset += dataSize
totElements -= numElements
start += numElements * wordSize
}
return
}
//DBRead
func (mb *client) Read(variable string, buffer []byte) (value interface{}, err error) {
variable = strings.ToUpper(variable) //upper
variable = strings.Replace(variable, " ", "", -1) //remove spaces
if variable == "" {
err = fmt.Errorf("input variable is empty, variable should be S7 syntax")
return
}
//var area, dbNumber, start, amount, wordLen int
switch valueArea := variable[0:2]; valueArea {
case "EB": //input byte
case "EW": //input word
case "ED": //Input double-word
case "AB": //Output byte
case "AW": //Output word
case "AD": //Output double-word
case "MB": //Memory byte
case "MW": //Memory word
case "MD": //Memory double-word
case "DB": //Data Block
dbArray := strings.Split(variable, ".")
if len(dbArray) < 2 {
err = fmt.Errorf("Db Area read variable should not be empty")
return
}
dbNo, _ := strconv.ParseInt(string(string(dbArray[0])[2:]), 10, 16)
dbIndex, _ := strconv.ParseInt(string(string(dbArray[1])[3:]), 10, 16)
dbType := string(dbArray[1])[0:3]
switch dbType {
case "DBB": //byte
err = mb.AGReadDB(int(dbNo), int(dbIndex), 1, buffer)
value = buffer[0]
return
case "DBW": //word
err = mb.AGReadDB(int(dbNo), int(dbIndex), 2, buffer)
value = binary.BigEndian.Uint16(buffer[0:])
return
case "DBD": //dword
err = mb.AGReadDB(int(dbNo), int(dbIndex), 4, buffer)
value = binary.BigEndian.Uint32(buffer[0:])
return
case "DBX": //bit
mBit, _ := strconv.ParseInt(string(string(dbArray[2])[0:]), 10, 16)
if mBit > 7 || mBit < 0 {
err = fmt.Errorf("Db read bit is invalid")
return
}
err = mb.AGReadDB(int(dbNo), int(dbIndex), 1, buffer)
mask := []byte{0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}
value = buffer[0] & mask[mBit]
return
default:
err = fmt.Errorf("error when parsing dbtype")
return
}
default:
switch otherArea := variable[0:1]; otherArea {
case "E":
case "I": //input
case "A":
case "0": //output
case "M": //memory
case "T": //timer
startByte, _ := strconv.ParseInt(string(variable[1:]), 10, 16)
err = mb.AGReadTM(int(startByte), 1, buffer)
if err != nil {
return
}
helper := Helper{}
helper.GetValueAt(buffer, 0, value)
return
case "Z":
case "C": //counter
startByte, _ := strconv.ParseInt(string(variable[1:]), 10, 16)
err = mb.AGReadCT(int(startByte), 1, buffer)
if err != nil {
return
}
helper := Helper{}
helper.GetValueAt(buffer, 0, value)
return
default:
err = fmt.Errorf("error when parsing db area")
return
}
}
return
}
//send the package of a pdu request and a pdu response, check for response error and verify the package
func (mb *client) send(request *ProtocolDataUnit) (response *ProtocolDataUnit, err error) {
dataResponse, err := mb.transporter.Send(request.Data)
if err != nil {
return
}
if err = mb.packager.Verify(request.Data, dataResponse); err != nil {
return
}
if dataResponse == nil || len(dataResponse) == 0 {
// Empty response
err = fmt.Errorf("s7: response data is empty")
return
}
response = &ProtocolDataUnit{
Data: dataResponse,
}
//check for error if any
err = responseError(response)
return response, err
}
//responseError get response error from pdu return S7Error with high and low byte
func responseError(response *ProtocolDataUnit) error {
s7Error := &S7Error{}
if response.Data != nil && len(response.Data) > 0 {
switch int(response.Data[1]) {
case 1:
case 7:
s7Error.High = response.Data[2]
s7Error.Low = response.Data[3]
break
case 2:
case 3:
s7Error.High = response.Data[10]
s7Error.Low = response.Data[11]
break
default:
return nil
}
}
return s7Error
}
//dataSize to number of byte accordingly
func dataSizeByte(wordLength int) int {
switch wordLength {
case s7wlbit:
return 1
case s7wlbyte:
return 1
case s7wlChar:
return 1
case s7wlword:
return 2
case s7wlint:
return 2
case s7wlcounter:
return 2
case s7wltimer:
return 2
case s7wldword:
return 4
case s7wldint:
return 4
case s7wlreal:
return 4
default:
return 0
}
}