jeol-t330a/succbone/succd/modbus.go

158 lines
3.6 KiB
Go
Raw Normal View History

2024-11-10 00:47:01 +00:00
package main
import (
"context"
"errors"
"fmt"
"time"
"github.com/simonvetter/modbus"
)
func modbusValuesToFloat(v uint16) float32 {
return float32(v) / 10.0
}
func (d *daemon) modbusConnect() error {
var err error
d.mu.Lock()
defer d.mu.Unlock()
// Setup modbus client
d.modbusClient, err = modbus.NewClient(&modbus.ClientConfiguration{
URL: "tcp://10.250.241.20:8887",
Timeout: 5 * time.Second,
})
if err != nil {
return err
}
// Connect to modbus client
err = d.modbusClient.Open()
if err != nil {
return err
}
return nil
}
// There are currently two devices connected to the modbus.
// The first one (slave 1) is a temperature/humidity sensor.
// The second one (slave 2) is a PTA8D08 transmitter
func (d *daemon) modbusUpdate() error {
d.mu.Lock()
defer d.mu.Unlock()
var err error
var registers []uint16 // temperature, humidity
// Switch to slave 1
d.modbusClient.SetUnitId(1)
// Read temperature and humidity
registers, err = d.modbusClient.ReadRegisters(1, 2, modbus.INPUT_REGISTER)
if err != nil {
return err
}
if len(registers) != 2 {
msg := fmt.Sprintf("Expected two registers from modbus slave 1, but got %d", len(registers))
return errors.New(msg)
}
d.daemonState.tempSEM = modbusValuesToFloat(registers[0])
d.daemonState.humiditySEM = modbusValuesToFloat(registers[1])
// Switch to slave 2 (KEC2)
2024-11-10 00:47:01 +00:00
d.modbusClient.SetUnitId(2)
// PT100 mapping
// Channel 0: Cable -WGA6, Sensor "dp bottom"
// Channel 1: Cable -WGA8, Sensor "dp inlet"
// Channel 2: Cable WGA7, Sensor "dp top"
registers, err = d.modbusClient.ReadRegisters(0, 3, modbus.HOLDING_REGISTER)
if err != nil {
return err
}
if len(registers) != 3 {
return fmt.Errorf("expected three registers from modbus slave 2, but got %d", len(registers))
2024-11-10 00:47:01 +00:00
}
d.daemonState.tempDPBottom = modbusValuesToFloat(registers[0])
d.daemonState.tempDPInlet = modbusValuesToFloat(registers[1])
d.daemonState.tempDPTop = modbusValuesToFloat(registers[2])
// Switch to slave 3 (KEC1)
d.modbusClient.SetUnitId(3)
// Do a read first to avoid side-effects from the subsequent write to the relay states
var digitalInputs [8]bool
var digitalInputRegisters []uint16
digitalInputRegisters, err = d.modbusClient.ReadRegisters(0x81, 8, modbus.HOLDING_REGISTER)
if err != nil {
return err
}
// Convert MODBUS words into bools
for idx, value := range digitalInputRegisters {
if value != 0 {
digitalInputs[idx] = true
} else {
digitalInputs[idx] = false
}
}
// TODO: Input mapping goes here
// KFA1-KFA8
var relayState [8]bool
// -KFA1 Roughing Pump
relayState[0] = d.daemonState.rpOn
// -KFA2 Diffusion Pump
relayState[1] = d.daemonState.dpOn
// -KFA4 Button Vent
relayState[3] = d.daemonState.vent.output
// -KFA5 Button Pump-Down
relayState[4] = d.daemonState.pumpdown.output
// -KFA6 Fake-Pirani Rough
relayState[5] = d.aboveRough.output
// -KFA7 Fake-Pirani High
relayState[6] = d.aboveHigh.output
// The KEC1 module uses a non-standard MODBUS interface
// instead of coils
// 0x0100 is the open command
// 0x0200 is the close command
// We write 8 words (16-bit) to address 0x01 to update the relays
var registerValues [8]uint16
// Convert the boolean values to the commands
for idx, state := range relayState {
if state {
registerValues[idx] = 0x0100
} else {
registerValues[idx] = 0x0200
}
}
err = d.modbusClient.WriteRegisters(0x01, registerValues[:])
if err != nil {
return err
}
2024-11-10 00:47:01 +00:00
return nil
}
// Call modbusUpdate every 100 milliseconds
2024-11-10 00:47:01 +00:00
func (d *daemon) modbusProcess(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
default:
d.modbusUpdate()
time.Sleep(time.Millisecond * 100)
2024-11-10 00:47:01 +00:00
}
}
}