-
Notifications
You must be signed in to change notification settings - Fork 4
/
protocols.go
67 lines (55 loc) · 1.27 KB
/
protocols.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
// Copyright 2019 Sergey Soldatov. All rights reserved.
// This software may be modified and distributed under the terms
// of the Apache license. See the LICENSE file for details.
package modbus
// Type of Modbus Protocol:
// - ModbusTCP
// - ModbusRTUviaTCP
type ModbusTypeProtocol int
const (
ModbusTCP ModbusTypeProtocol = 0
ModbusRTUviaTCP ModbusTypeProtocol = 1
ModbusUnknown = -1
)
const (
ModbusRTUviaTCPMaxSize int = 256
ModbusTCPMaxSize int = 260
)
// Get MaxSize of packet for this protocol
func (p ModbusTypeProtocol) MaxSize() int {
names := []int{
ModbusTCPMaxSize,
ModbusRTUviaTCPMaxSize}
if p < ModbusTCP || p > ModbusRTUviaTCP {
return 0
}
return names[p]
}
// PDU offest
func (p ModbusTypeProtocol) Offset() int {
if p == ModbusTCP {
return 6
}
return 0
}
// Get the name of this protocol
func (p ModbusTypeProtocol) String() string {
names := []string{
"ModbusTCP",
"ModbusRTUviaTCP"}
if p < ModbusTCP || p > ModbusRTUviaTCP {
return "Unknown"
}
return names[p]
}
// Convert name protocol to type
func StringToModbusTypeProtocol(name string) ModbusTypeProtocol {
switch name {
case "ModbusTCP":
return ModbusTCP
case "ModbusRTUviaTCP":
return ModbusRTUviaTCP
default:
return ModbusUnknown
}
}