Temperature Measurement & MODBUS Integration #17
|
@ -2,7 +2,6 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/simonvetter/modbus"
|
||||
|
@ -22,7 +21,7 @@ func (d *daemon) modbusConnect() error {
|
|||
// Setup modbus client
|
||||
d.modbusClient, err = modbus.NewClient(&modbus.ClientConfiguration{
|
||||
URL: "tcp://10.250.241.20:8887",
|
||||
Timeout: 5 * time.Second,
|
||||
Timeout: 1 * time.Second,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -48,6 +47,7 @@ func (d *daemon) modbusRestart() error {
|
|||
// Returns whether modbus should restart (only in case of an underlying network error)
|
||||
func (d *daemon) modbusUpdate() bool {
|
||||
var err error
|
||||
var numDevicesNotResponding int
|
||||
|
||||
// Switch to slave 1 (BTA1)
|
||||
d.modbusClient.SetUnitId(1)
|
||||
|
@ -56,9 +56,7 @@ func (d *daemon) modbusUpdate() bool {
|
|||
var registersBTA1 []uint16 // temperature, humidity
|
||||
registersBTA1, err = d.modbusClient.ReadRegisters(1, 2, modbus.INPUT_REGISTER)
|
||||
if err != nil {
|
||||
if _, ok := err.(net.Error); ok {
|
||||
return true
|
||||
}
|
||||
numDevicesNotResponding += 1
|
||||
klog.Warningf("error while reading registers from BTA1 %v", err)
|
||||
} else if len(registersBTA1) != 2 {
|
||||
klog.Warningf("expected two registers from modbus slave 1, but got %d", len(registersBTA1))
|
||||
|
@ -79,9 +77,7 @@ func (d *daemon) modbusUpdate() bool {
|
|||
var registersKEC2 []uint16 // temperatures dp
|
||||
registersKEC2, err = d.modbusClient.ReadRegisters(0, 3, modbus.HOLDING_REGISTER)
|
||||
if err != nil {
|
||||
if _, ok := err.(net.Error); ok {
|
||||
return true
|
||||
}
|
||||
numDevicesNotResponding += 1
|
||||
klog.Warningf("error while reading registers from KEC2 %v", err)
|
||||
} else if len(registersKEC2) != 3 {
|
||||
klog.Warningf("expected three registers from modbus slave 2, but got %d", len(registersKEC2))
|
||||
|
@ -102,9 +98,7 @@ func (d *daemon) modbusUpdate() bool {
|
|||
|
||||
digitalInputRegisters, err = d.modbusClient.ReadRegisters(0x81, 8, modbus.HOLDING_REGISTER)
|
||||
if err != nil {
|
||||
if _, ok := err.(net.Error); ok {
|
||||
return true
|
||||
}
|
||||
numDevicesNotResponding += 1
|
||||
klog.Warningf("error while reading digital inputs from KEC1 %v", err)
|
||||
} else {
|
||||
// Convert MODBUS words into bools
|
||||
|
@ -157,12 +151,15 @@ func (d *daemon) modbusUpdate() bool {
|
|||
|
||||
err = d.modbusClient.WriteRegisters(0x01, registerValuesKEC1[:])
|
||||
if err != nil {
|
||||
if _, ok := err.(net.Error); ok {
|
||||
return true
|
||||
}
|
||||
numDevicesNotResponding += 1
|
||||
klog.Warningf("error while updating registers %v", err)
|
||||
}
|
||||
|
||||
if numDevicesNotResponding >= 4 {
|
||||
klog.Warningf("no device did respond to our request. Probably a network timeout.")
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue