Temperature Measurement & MODBUS Integration #17

Merged
rahix merged 13 commits from more-temps into main 2024-11-10 06:58:09 +00:00
Showing only changes of commit 3b9c1ba912 - Show all commits

View file

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