#9 Function to update all coil values
This commit is contained in:
parent
3fab63f60f
commit
8143414c39
|
@ -2,16 +2,14 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/0ranki/enervent-ctrl/enervent-ctrl-go/pingvinKL"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.Println(time.Now())
|
||||
pingvin := pingvinKL.New()
|
||||
log.Println(time.Now())
|
||||
fmt.Println(pingvin.ReadCoil(40))
|
||||
log.Println(time.Now())
|
||||
pingvin.Update()
|
||||
for i := 0; i < len(pingvin.Coils); i++ {
|
||||
fmt.Println(pingvin.Coils[i].Symbol, pingvin.Coils[i].Value, pingvin.Coils[i].Description)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ func readCsvLines(file string) [][]string {
|
|||
return data
|
||||
}
|
||||
|
||||
// Configure the modbus client on creation
|
||||
// Configure the modbus parameters
|
||||
func (p PingvinKL) getHandler() *modbus.RTUClientHandler {
|
||||
// TODO: read configuration from file, hardcoded for now
|
||||
handler := modbus.NewRTUClientHandler("/dev/ttyS0")
|
||||
|
@ -58,25 +58,52 @@ func (p PingvinKL) getHandler() *modbus.RTUClientHandler {
|
|||
handler.Parity = "N"
|
||||
handler.StopBits = 1
|
||||
handler.SlaveId = 1
|
||||
handler.Timeout = 200 * time.Millisecond
|
||||
handler.Timeout = 1500 * time.Millisecond
|
||||
return handler
|
||||
}
|
||||
|
||||
func (p PingvinKL) Update() {
|
||||
// coildata, err := p.Client.ReadCoils(0, len(p.Coils))
|
||||
handler := p.getHandler()
|
||||
err := handler.Connect()
|
||||
if err != nil {
|
||||
log.Fatal("Update: handler.Connect: ", err)
|
||||
}
|
||||
defer handler.Close()
|
||||
client := modbus.NewClient(handler)
|
||||
results, err := client.ReadCoils(0, uint16(len(p.Coils)))
|
||||
if err != nil {
|
||||
log.Fatal("Update: client.ReadCoils:", err)
|
||||
}
|
||||
// modbus.ReadCoils returns a byte array, with the first byte's bits representing coil values 0-7,
|
||||
// second byte coils 8-15 etc.
|
||||
// Within each byte, LSB represents the lowest n coil while MSB is the highest
|
||||
// e.g. reading the first 8 coils might return a byte array of length 1, with the following:
|
||||
// [4], which is 00000100, meaning all other coils are 0 except coil #2 (3rd coil)
|
||||
//
|
||||
k := 0 // pingvinCoil index
|
||||
for i := 0; i < len(results); i++ { // loop through the byte array
|
||||
for j := 0; j < 8; j++ {
|
||||
// Here we loop through each bit in the byte, shifting right
|
||||
// and checking if the LSB after the shift is 1 with a bitwise AND
|
||||
// A coil value of 1 means on/true/yes, so == 1 returns the bool value
|
||||
// for each coil
|
||||
p.Coils[k].Value = (results[i] >> j & 0x1) == 1
|
||||
k++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p PingvinKL) ReadCoil(n uint16) []byte {
|
||||
handler := p.getHandler()
|
||||
err := handler.Connect()
|
||||
if err != nil {
|
||||
log.Fatal("ReadCoil1: ", err)
|
||||
log.Fatal("ReadCoil: handler.Connect: ", err)
|
||||
}
|
||||
defer handler.Close()
|
||||
client := modbus.NewClient(handler)
|
||||
results, err := client.ReadCoils(n, 1)
|
||||
if err != nil {
|
||||
log.Fatal("ReadCoil2: ", err)
|
||||
log.Fatal("ReadCoil: client.ReadCoils", err)
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue