#9 Mutex locking for bus operations
This commit is contained in:
parent
8143414c39
commit
876123e518
|
@ -5,6 +5,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/goburrow/modbus"
|
"github.com/goburrow/modbus"
|
||||||
|
@ -26,7 +27,8 @@ func newCoil(symbol string, description string) pingvinCoil {
|
||||||
|
|
||||||
// unit modbus data
|
// unit modbus data
|
||||||
type PingvinKL struct {
|
type PingvinKL struct {
|
||||||
Coils []pingvinCoil
|
Coils []pingvinCoil
|
||||||
|
buslock *sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// read a CSV file containing data for coils or registers
|
// read a CSV file containing data for coils or registers
|
||||||
|
@ -64,6 +66,7 @@ func (p PingvinKL) getHandler() *modbus.RTUClientHandler {
|
||||||
|
|
||||||
func (p PingvinKL) Update() {
|
func (p PingvinKL) Update() {
|
||||||
handler := p.getHandler()
|
handler := p.getHandler()
|
||||||
|
p.buslock.Lock()
|
||||||
err := handler.Connect()
|
err := handler.Connect()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Update: handler.Connect: ", err)
|
log.Fatal("Update: handler.Connect: ", err)
|
||||||
|
@ -74,6 +77,7 @@ func (p PingvinKL) Update() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Update: client.ReadCoils:", err)
|
log.Fatal("Update: client.ReadCoils:", err)
|
||||||
}
|
}
|
||||||
|
p.buslock.Unlock()
|
||||||
// modbus.ReadCoils returns a byte array, with the first byte's bits representing coil values 0-7,
|
// modbus.ReadCoils returns a byte array, with the first byte's bits representing coil values 0-7,
|
||||||
// second byte coils 8-15 etc.
|
// second byte coils 8-15 etc.
|
||||||
// Within each byte, LSB represents the lowest n coil while MSB is the highest
|
// Within each byte, LSB represents the lowest n coil while MSB is the highest
|
||||||
|
@ -95,6 +99,7 @@ func (p PingvinKL) Update() {
|
||||||
|
|
||||||
func (p PingvinKL) ReadCoil(n uint16) []byte {
|
func (p PingvinKL) ReadCoil(n uint16) []byte {
|
||||||
handler := p.getHandler()
|
handler := p.getHandler()
|
||||||
|
p.buslock.Lock()
|
||||||
err := handler.Connect()
|
err := handler.Connect()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("ReadCoil: handler.Connect: ", err)
|
log.Fatal("ReadCoil: handler.Connect: ", err)
|
||||||
|
@ -102,15 +107,18 @@ func (p PingvinKL) ReadCoil(n uint16) []byte {
|
||||||
defer handler.Close()
|
defer handler.Close()
|
||||||
client := modbus.NewClient(handler)
|
client := modbus.NewClient(handler)
|
||||||
results, err := client.ReadCoils(n, 1)
|
results, err := client.ReadCoils(n, 1)
|
||||||
|
p.buslock.Unlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("ReadCoil: client.ReadCoils", err)
|
log.Fatal("ReadCoil: client.ReadCoils", err)
|
||||||
}
|
}
|
||||||
|
p.Coils[n].Value = results[0] == 1
|
||||||
return results
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a PingvinKL struct, read coils and registers from CSVs
|
// create a PingvinKL struct, read coils and registers from CSVs
|
||||||
func New() PingvinKL {
|
func New() PingvinKL {
|
||||||
pingvin := PingvinKL{}
|
pingvin := PingvinKL{}
|
||||||
|
pingvin.buslock = &sync.Mutex{}
|
||||||
coilData := readCsvLines("coils.csv")
|
coilData := readCsvLines("coils.csv")
|
||||||
for i := 0; i < len(coilData); i++ {
|
for i := 0; i < len(coilData); i++ {
|
||||||
pingvin.Coils = append(pingvin.Coils, newCoil(coilData[i][1], coilData[i][2]))
|
pingvin.Coils = append(pingvin.Coils, newCoil(coilData[i][1], coilData[i][2]))
|
||||||
|
|
Loading…
Reference in New Issue