-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathascii_packager.go
134 lines (129 loc) · 3.73 KB
/
ascii_packager.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
package modbus
import (
"bytes"
"encoding/hex"
"fmt"
"strings"
)
const (
asciiMaxSize = rtuMaxSize * 2
asciiMinSize = rtuMinSize * 2
asciiStart = ":"
asciiEnd = "\r\n"
)
type asciiPackager struct {
slaveID byte
}
func (p *asciiPackager) Encode(pdu protocolDataUnit) (adu ApplicationDataUnit, err error) {
data := make([]byte, 3+len(pdu.GetData()))
data[0] = p.slaveID
data[1] = pdu.GetFunctionCode()
copy(data[2:], pdu.GetData())
checkSum := LRC(data)
checkSumByte := []byte{checkSum}
buf := bytes.NewBuffer([]byte{})
//start
_, _ = buf.WriteString(asciiStart)
_, _ = buf.WriteString(strings.ToUpper(hex.EncodeToString(data)))
_, _ = buf.WriteString(strings.ToUpper(hex.EncodeToString(checkSumByte)))
_, _ = buf.WriteString(asciiEnd)
adu = applicationDataUnit{
slaveID: p.slaveID,
pdu: pdu,
checkSum: uint16(checkSum),
checkSumByte: checkSumByte,
data: buf.Bytes(),
length: buf.Len(),
mode: ASCII,
}
return
}
func (p *asciiPackager) Decode(results []byte) (adu ApplicationDataUnit, err error) {
length := len(results)
if length > asciiMaxSize {
err = fmt.Errorf("modbus: response data size '%v' exceeds the maximum limit of '%v'", length, asciiMaxSize)
return
}
if length < asciiMinSize {
err = fmt.Errorf("modbus: response data size '%v' less than maximum limit of '%v'", length, asciiMinSize)
return
}
slaveIDBs, err := hex.DecodeString(string(results[1:3]))
if err != nil {
return
}
slaveID := slaveIDBs[0]
functionCodeBs, err := hex.DecodeString(string(results[3:5]))
if err != nil {
return
}
functionCode := functionCodeBs[0]
pduLengthBs, err := hex.DecodeString(string(results[5:7]))
if err != nil {
return
}
pduLength := pduLengthBs[0]
pduData := results[7 : length-4]
pduData, err = hex.DecodeString(string(pduData))
if err != nil {
return
}
pdu := protocolDataUnit{
functionCode: functionCode,
data: pduData,
length: int(pduLength),
}
checkSum := results[length-4 : length-2]
checkSum, err = hex.DecodeString(string(checkSum))
if err != nil {
return
}
adu = applicationDataUnit{
slaveID: slaveID,
pdu: pdu,
checkSumByte: checkSum,
checkSum: uint16(checkSum[0]),
data: results,
length: length,
mode: ASCII,
}
return
}
func (p *asciiPackager) Verify(aduRequest ApplicationDataUnit, aduResponse ApplicationDataUnit) (err error) {
if aduRequest.GetSlaveId() != aduResponse.GetSlaveId() {
err = fmt.Errorf("modbus: aduRequest slaveId '%v' and aduResponse slaveId '%v' are inconsistent", aduRequest.GetSlaveId(), aduResponse.GetSlaveId())
return
}
if aduRequest.GetFunctionCode() != aduResponse.GetFunctionCode() {
if aduResponse.GetFunctionCode() == aduRequest.GetFunctionCode()+0x80 {
err = fmt.Errorf("modbus: error errorCode '%X'", aduResponse.GetFunctionCode())
data := aduResponse.GetPDU().GetData()
if len(data) == 0 {
return
}
text, exist := faults[data[0]]
if !exist {
return
}
err = fmt.Errorf("modbus: error errorCode: '%X' %s", aduResponse.GetFunctionCode(), text)
return
}
err = fmt.Errorf("modbus: aduRequest functionCode '%v' and aduResponse functionCode '%v' are inconsistent", aduRequest.GetFunctionCode(), aduResponse.GetFunctionCode())
return
}
data := aduResponse.GetData()
data, err = hex.DecodeString(string(data[1 : len(data)-4]))
if err != nil {
return
}
checksum := LRC(data)
if uint16(checksum) != aduResponse.GetCheckSum() {
err = fmt.Errorf("modbus: lrc validation failed source:'%v' reality:'%v' ", hex.EncodeToString(aduResponse.GetCheckSumByte()), hex.EncodeToString([]byte{checksum}))
return
}
return
}
func NewAsciiPackager(slaveID byte) (p Packager) {
p = &asciiPackager{slaveID: slaveID}
return
}