Compare commits
No commits in common. "679680fe1506b6480080ae4d7752cb1460c0aa91" and "d8a467a0c451edd11a7a61212670bbca845b465c" have entirely different histories.
679680fe15
...
d8a467a0c4
|
@ -2,10 +2,11 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/simonvetter/modbus"
|
"github.com/simonvetter/modbus"
|
||||||
"k8s.io/klog"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func modbusValuesToFloat(v uint16) float32 {
|
func modbusValuesToFloat(v uint16) float32 {
|
||||||
|
@ -38,7 +39,7 @@ func (d *daemon) modbusConnect() error {
|
||||||
// There are currently two devices connected to the modbus.
|
// There are currently two devices connected to the modbus.
|
||||||
// The first one (slave 1) is a temperature/humidity sensor.
|
// The first one (slave 1) is a temperature/humidity sensor.
|
||||||
// The second one (slave 2) is a PTA8D08 transmitter
|
// The second one (slave 2) is a PTA8D08 transmitter
|
||||||
func (d *daemon) modbusUpdate() {
|
func (d *daemon) modbusUpdate() error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
// Switch to slave 1 (BTA1)
|
// Switch to slave 1 (BTA1)
|
||||||
|
@ -48,15 +49,18 @@ func (d *daemon) modbusUpdate() {
|
||||||
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 {
|
||||||
klog.Warningf("error while reading registers from BTA1 %v", err)
|
return err
|
||||||
} else if len(registersBTA1) != 2 {
|
}
|
||||||
klog.Warningf("expected two registers from modbus slave 1, but got %d", len(registersBTA1))
|
|
||||||
} else {
|
if len(registersBTA1) != 2 {
|
||||||
|
msg := fmt.Sprintf("Expected two registers from modbus slave 1, but got %d", len(registersBTA1))
|
||||||
|
return errors.New(msg)
|
||||||
|
}
|
||||||
|
|
||||||
d.mu.Lock()
|
d.mu.Lock()
|
||||||
d.daemonState.tempSEM = modbusValuesToFloat(registersBTA1[0])
|
d.daemonState.tempSEM = modbusValuesToFloat(registersBTA1[0])
|
||||||
d.daemonState.humiditySEM = modbusValuesToFloat(registersBTA1[1])
|
d.daemonState.humiditySEM = modbusValuesToFloat(registersBTA1[1])
|
||||||
d.mu.Unlock()
|
d.mu.Unlock()
|
||||||
}
|
|
||||||
|
|
||||||
// Switch to slave 2 (KEC2)
|
// Switch to slave 2 (KEC2)
|
||||||
d.modbusClient.SetUnitId(2)
|
d.modbusClient.SetUnitId(2)
|
||||||
|
@ -68,16 +72,18 @@ func (d *daemon) modbusUpdate() {
|
||||||
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 {
|
||||||
klog.Warningf("error while reading registers from KEC2 %v", err)
|
return err
|
||||||
} else if len(registersKEC2) != 3 {
|
}
|
||||||
klog.Warningf("expected three registers from modbus slave 2, but got %d", len(registersKEC2))
|
|
||||||
} else {
|
if len(registersKEC2) != 3 {
|
||||||
|
return fmt.Errorf("expected three registers from modbus slave 2, but got %d", len(registersKEC2))
|
||||||
|
}
|
||||||
|
|
||||||
d.mu.Lock()
|
d.mu.Lock()
|
||||||
d.daemonState.tempDPBottom = modbusValuesToFloat(registersKEC2[0])
|
d.daemonState.tempDPBottom = modbusValuesToFloat(registersKEC2[0])
|
||||||
d.daemonState.tempDPInlet = modbusValuesToFloat(registersKEC2[1])
|
d.daemonState.tempDPInlet = modbusValuesToFloat(registersKEC2[1])
|
||||||
d.daemonState.tempDPTop = modbusValuesToFloat(registersKEC2[2])
|
d.daemonState.tempDPTop = modbusValuesToFloat(registersKEC2[2])
|
||||||
d.mu.Unlock()
|
d.mu.Unlock()
|
||||||
}
|
|
||||||
|
|
||||||
// Switch to slave 3 (KEC1)
|
// Switch to slave 3 (KEC1)
|
||||||
d.modbusClient.SetUnitId(3)
|
d.modbusClient.SetUnitId(3)
|
||||||
|
@ -88,8 +94,8 @@ func (d *daemon) modbusUpdate() {
|
||||||
|
|
||||||
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 {
|
||||||
klog.Warningf("error while reading digital inputs from KEC1 %v", err)
|
return err
|
||||||
} else {
|
}
|
||||||
// Convert MODBUS words into bools
|
// Convert MODBUS words into bools
|
||||||
for idx, value := range digitalInputRegisters {
|
for idx, value := range digitalInputRegisters {
|
||||||
if value != 0 {
|
if value != 0 {
|
||||||
|
@ -99,7 +105,6 @@ func (d *daemon) modbusUpdate() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// TODO: Input mapping goes here
|
// TODO: Input mapping goes here
|
||||||
}
|
|
||||||
|
|
||||||
// KFA1-KFA8
|
// KFA1-KFA8
|
||||||
var relayState [8]bool
|
var relayState [8]bool
|
||||||
|
@ -135,8 +140,10 @@ func (d *daemon) modbusUpdate() {
|
||||||
|
|
||||||
err = d.modbusClient.WriteRegisters(0x01, registerValuesKEC1[:])
|
err = d.modbusClient.WriteRegisters(0x01, registerValuesKEC1[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Warningf("error while updating registers %v", err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call modbusUpdate every 100 milliseconds
|
// Call modbusUpdate every 100 milliseconds
|
||||||
|
|
Loading…
Reference in a new issue