-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserial_rtu.go
57 lines (44 loc) · 1.48 KB
/
serial_rtu.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
package modbusone
import (
"fmt"
"github.com/xiegeo/modbusone/crc"
)
// MaxRTUSize is the max possible size of a RTU packet.
const MaxRTUSize = 256
// MaxPDUSize is the max possible size of a PDU packet.
const MaxPDUSize = 253
// OverSizeSupport ignores max packet size and encoded number of bytes to support
// over sided implementations encountered in the wild. This setting only applies
// to the server end, since client is always reserved in what it requests.
// Also change OverSizeMaxRTU properly.
var OverSizeSupport = false
// OverSizeMaxRTU overrides MaxRTUSize when OverSizeSupport is true.
var OverSizeMaxRTU = MaxRTUSize
const smallestRTUSize = 4
// RTU is the Modbus RTU Application Data Unit.
type RTU []byte
// MakeRTU makes a RTU with slaveID and PDU.
func MakeRTU(slaveID byte, p PDU) RTU {
return RTU(crc.Sum(append([]byte{slaveID}, p...)))
}
// IsMulticast returns true if slaveID is the multicast address 0.
func (r RTU) IsMulticast() bool {
return len(r) > 0 && r[0] == 0
}
// ErrorCrc indicates data corruption detected by checking the CRC.
var ErrorCrc = fmt.Errorf("RTU data crc not valid")
// GetPDU returns the PDU inside, CRC is checked.
func (r RTU) GetPDU() (PDU, error) {
if len(r) < 4 {
return nil, fmt.Errorf("RTU data too short to produce PDU")
}
if !crc.Validate(r) {
return nil, ErrorCrc
}
p := r.fastGetPDU()
return p, nil
}
// fastGetPDU returns the PDU inside, with no safety checks.
func (r RTU) fastGetPDU() PDU {
return PDU(r[1 : len(r)-2])
}