Example work
This commit is contained in:
parent
89de6652ab
commit
65e1307645
14
protypes/modbus_logger/go.sum
Normal file
14
protypes/modbus_logger/go.sum
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
github.com/goburrow/modbus v0.1.0 h1:DejRZY73nEM6+bt5JSP6IsFolJ9dVcqxsYbpLbeW/ro=
|
||||||
|
github.com/goburrow/modbus v0.1.0/go.mod h1:Kx552D5rLIS8E7TyUwQ/UdHEqvX5T8tyiGBTlzMcZBg=
|
||||||
|
github.com/goburrow/serial v0.1.0 h1:v2T1SQa/dlUqQiYIT8+Cu7YolfqAi3K96UmhwYyuSrA=
|
||||||
|
github.com/goburrow/serial v0.1.0/go.mod h1:sAiqG0nRVswsm1C97xsttiYCzSLBmUZ/VSlVLZJ8haA=
|
||||||
|
github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4=
|
||||||
|
github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc=
|
||||||
|
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 h1:UyzmZLoiDWMRywV4DUYb9Fbt8uiOSooupjTq10vpvnU=
|
||||||
|
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
|
||||||
|
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
|
||||||
|
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
periph.io/x/conn/v3 v3.7.1 h1:tMjNv3WO8jEz/ePuXl7y++2zYi8LsQ5otbmqGKy3Myg=
|
||||||
|
periph.io/x/conn/v3 v3.7.1/go.mod h1:c+HCVjkzbf09XzcqZu/t+U8Ss/2QuJj0jgRF6Nye838=
|
||||||
|
periph.io/x/host/v3 v3.8.2 h1:ayKUDzgUCN0g8+/xM9GTkWaOBhSLVcVHGTfjAOi8OsQ=
|
||||||
|
periph.io/x/host/v3 v3.8.2/go.mod h1:yFL76AesNHR68PboofSWYaQTKmvPXsQH2Apvp/ls/K4=
|
136
protypes/modbus_logger/modbus_logger.go
Normal file
136
protypes/modbus_logger/modbus_logger.go
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"math"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/goburrow/modbus"
|
||||||
|
"github.com/tarm/serial"
|
||||||
|
"periph.io/x/conn/v3/gpio"
|
||||||
|
"periph.io/x/conn/v3/gpio/gpioreg"
|
||||||
|
"periph.io/x/host/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
serialPort = "/dev/ttyAMA0"
|
||||||
|
baudRate = 9600
|
||||||
|
dataBits = 8
|
||||||
|
stopBits = 1
|
||||||
|
parity = "N"
|
||||||
|
slaveID = 101
|
||||||
|
deRePin = "GPIO4"
|
||||||
|
)
|
||||||
|
|
||||||
|
var registers = []struct {
|
||||||
|
regaddr uint16
|
||||||
|
regtype uint8
|
||||||
|
}{
|
||||||
|
{1, 2},
|
||||||
|
{3, 1},
|
||||||
|
{5, 0},
|
||||||
|
// Add more registers as needed
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Initialize GPIO
|
||||||
|
if _, err := host.Init(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up DE/RE pin
|
||||||
|
deRe := gpioreg.ByName(deRePin)
|
||||||
|
if deRe == nil {
|
||||||
|
log.Fatal("Failed to find DE/RE pin")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Configure serial port
|
||||||
|
config := &serial.Config{
|
||||||
|
Name: serialPort,
|
||||||
|
Baud: baudRate,
|
||||||
|
//DataBits: dataBits,
|
||||||
|
StopBits: stopBits,
|
||||||
|
//Parity: parity,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create Modbus RTU client
|
||||||
|
handler := modbus.NewRTUClientHandler(config.Name)
|
||||||
|
handler.BaudRate = config.Baud
|
||||||
|
//handler.DataBits = config.DataBits
|
||||||
|
//handler.StopBits = config.StopBits
|
||||||
|
//handler.Parity = config.Parity
|
||||||
|
handler.SlaveId = slaveID
|
||||||
|
handler.Timeout = 5 * time.Second
|
||||||
|
|
||||||
|
err := handler.Connect()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Failed to connect:", err)
|
||||||
|
}
|
||||||
|
defer handler.Close()
|
||||||
|
|
||||||
|
client := modbus.NewClient(handler)
|
||||||
|
|
||||||
|
for {
|
||||||
|
filename := getFilename()
|
||||||
|
file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error opening file:", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write CSV header if file is empty
|
||||||
|
fileInfo, _ := file.Stat()
|
||||||
|
if fileInfo.Size() == 0 {
|
||||||
|
header := "Date Time"
|
||||||
|
for _, reg := range registers {
|
||||||
|
header += fmt.Sprintf(",@%d", reg.regaddr)
|
||||||
|
}
|
||||||
|
file.WriteString(header + "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log current date and time
|
||||||
|
timestamp := time.Now().Format("2006-01-02 15:04:05")
|
||||||
|
line := timestamp
|
||||||
|
|
||||||
|
for _, reg := range registers {
|
||||||
|
deRe.Out(gpio.High)
|
||||||
|
results, err := client.ReadHoldingRegisters(reg.regaddr-1, 2)
|
||||||
|
deRe.Out(gpio.Low)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error reading register %d: %v", reg.regaddr, err)
|
||||||
|
line += ",ERROR"
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
switch reg.regtype {
|
||||||
|
case 2: // Float
|
||||||
|
value := math.Float32frombits(binary.BigEndian.Uint32(results))
|
||||||
|
line += fmt.Sprintf(",%.2f", value)
|
||||||
|
case 1: // Uint16
|
||||||
|
value := binary.BigEndian.Uint16(results)
|
||||||
|
line += fmt.Sprintf(",%d", value)
|
||||||
|
case 0: // Int32
|
||||||
|
value := int32(binary.BigEndian.Uint32(results))
|
||||||
|
line += fmt.Sprintf(",%d", value)
|
||||||
|
default:
|
||||||
|
line += ",UNKNOWN"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
file.WriteString(line + "\n")
|
||||||
|
file.Close()
|
||||||
|
|
||||||
|
log.Printf("Data written to %s", filename)
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getFilename() string {
|
||||||
|
now := time.Now()
|
||||||
|
return filepath.Join("data", fmt.Sprintf("pm8k_%s.csv", now.Format("20060102")))
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user