forked from gonium/gosdm630
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modbus.go
239 lines (206 loc) · 5.63 KB
/
modbus.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
package sdm630
import (
"fmt"
"log"
"os"
"time"
"github.com/goburrow/modbus"
. "github.com/gonium/gosdm630/internal/meters"
)
const (
MaxRetryCount = 5
)
const (
_ = iota
ModbusComset2400_8N1
ModbusComset9600_8N1
ModbusComset19200_8N1
ModbusComset2400_8E1
ModbusComset9600_8E1
ModbusComset19200_8E1
)
type ModbusEngine struct {
client modbus.Client
handler *modbus.RTUClientHandler
verbose bool
status *Status
}
func NewRTUClient(rtuDevice string, comset int, verbose bool) *modbus.RTUClientHandler {
// Modbus RTU/ASCII
rtuclient := modbus.NewRTUClientHandler(rtuDevice)
rtuclient.Parity = "N"
rtuclient.DataBits = 8
rtuclient.StopBits = 1
switch comset {
case ModbusComset2400_8N1:
rtuclient.BaudRate = 2400
case ModbusComset9600_8N1:
rtuclient.BaudRate = 9600
case ModbusComset19200_8N1:
rtuclient.BaudRate = 19200
case ModbusComset2400_8E1:
rtuclient.BaudRate = 2400
rtuclient.Parity = "E"
case ModbusComset9600_8E1:
rtuclient.BaudRate = 9600
rtuclient.Parity = "E"
case ModbusComset19200_8E1:
rtuclient.BaudRate = 19200
rtuclient.Parity = "E"
default:
log.Fatal("Invalid communication set specified. See -h for help.")
}
rtuclient.Timeout = 300 * time.Millisecond
if verbose {
rtuclient.Logger = log.New(os.Stdout, "RTUClientHandler: ", log.LstdFlags)
log.Printf("Connecting to RTU via %s, %d %d%s%d\r\n", rtuDevice,
rtuclient.BaudRate, rtuclient.DataBits, rtuclient.Parity,
rtuclient.StopBits)
}
err := rtuclient.Connect()
if err != nil {
log.Fatal("Failed to connect: ", err)
}
defer rtuclient.Close()
return rtuclient
}
func NewModbusEngine(
rtuDevice string,
comset int,
verbose bool,
status *Status,
) *ModbusEngine {
var rtuclient *modbus.RTUClientHandler
var mbclient modbus.Client
if rtuDevice == "simulation" {
rtuclient = &modbus.RTUClientHandler{}
mbclient = NewMockClient(50) // 50% error rate for testing
} else {
rtuclient = NewRTUClient(rtuDevice, comset, verbose)
mbclient = modbus.NewClient(rtuclient)
}
return &ModbusEngine{
client: mbclient,
handler: rtuclient,
verbose: verbose,
status: status,
}
}
func (q *ModbusEngine) query(snip QuerySnip) (retval []byte, err error) {
q.status.IncreaseRequestCounter()
// update the slave id in the handler
q.handler.SlaveId = snip.DeviceId
if snip.ReadLen <= 0 {
log.Fatalf("Invalid meter operation %v.", snip)
}
switch snip.FuncCode {
case ReadInputReg:
retval, err = q.client.ReadInputRegisters(snip.OpCode, snip.ReadLen)
case ReadHoldingReg:
retval, err = q.client.ReadHoldingRegisters(snip.OpCode, snip.ReadLen)
default:
log.Fatalf("Unknown function code %d - cannot query device.",
snip.FuncCode)
}
if err != nil && q.verbose {
log.Printf("Failed to retrieve opcode 0x%x, error was: %s\r\n", snip.OpCode, err.Error())
}
return retval, err
}
func (q *ModbusEngine) Transform(
inputStream QuerySnipChannel,
controlStream ControlSnipChannel,
outputStream QuerySnipChannel,
) {
var previousDeviceId uint8
for {
PROCESS_READINGS:
snip := <-inputStream
// The SDM devices need to have a little pause between querying
// different devices.
if previousDeviceId != snip.DeviceId {
time.Sleep(time.Duration(100) * time.Millisecond)
}
previousDeviceId = snip.DeviceId
for retryCount := 0; retryCount < MaxRetryCount; retryCount++ {
reading, err := q.query(snip)
if err == nil {
// convert bytes to value
snip.Value = snip.Transform(reading)
snip.ReadTimestamp = time.Now()
outputStream <- snip
// signal ok
successSnip := ControlSnip{
Type: CONTROLSNIP_OK,
Message: "OK",
DeviceId: snip.DeviceId,
}
controlStream <- successSnip
goto PROCESS_READINGS
} else {
q.status.IncreaseReconnectCounter()
log.Printf("Device %d failed to respond - retry attempt %d of %d",
snip.DeviceId, retryCount+1, MaxRetryCount)
time.Sleep(time.Duration(100) * time.Millisecond)
}
}
// signal error
errorSnip := ControlSnip{
Type: CONTROLSNIP_ERROR,
Message: fmt.Sprintf("Device %d did not respond.", snip.DeviceId),
DeviceId: snip.DeviceId,
}
controlStream <- errorSnip
}
}
func (q *ModbusEngine) Scan() {
type DeviceInfo struct {
DeviceId uint8
MeterType string
}
var deviceId uint8
deviceList := make([]DeviceInfo, 0)
oldtimeout := q.handler.Timeout
q.handler.Timeout = 50 * time.Millisecond
log.Printf("Starting bus scan")
producers := []Producer{
NewSDMProducer(),
NewJanitzaProducer(),
NewDZGProducer(),
}
SCAN:
// loop over all valid slave adresses
for deviceId = 1; deviceId <= 247; deviceId++ {
// give the bus some time to recover before querying the next device
time.Sleep(time.Duration(40) * time.Millisecond)
for _, producer := range producers {
operation := producer.Probe()
snip := NewQuerySnip(deviceId, operation)
value, err := q.query(snip)
if err == nil {
log.Printf("Device %d: %s type device found, %s: %.2f\r\n",
deviceId,
producer.GetMeterType(),
GetIecDescription(snip.IEC61850),
snip.Transform(value))
dev := DeviceInfo{
DeviceId: deviceId,
MeterType: producer.GetMeterType(),
}
deviceList = append(deviceList, dev)
continue SCAN
}
}
log.Printf("Device %d: n/a\r\n", deviceId)
}
// restore timeout to old value
q.handler.Timeout = oldtimeout
log.Printf("Found %d active devices:\r\n", len(deviceList))
for _, device := range deviceList {
log.Printf("* slave address %d: type %s\r\n", device.DeviceId,
device.MeterType)
}
log.Println("WARNING: This lists only the devices that responded to " +
"a known probe request. Devices with different " +
"function code definitions might not be detected.")
}