You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Apr 2, 2021. It is now read-only.
We have a modbus device that uses a combination of 2 registers to return a float32 result (for example ampere).
Is it possible to create support for this kind of values ?
As an example, following code supports this operation:
client := modbus.NewClient(handler)
var modBytes []byte
modBytes, err := client.ReadHoldingRegisters(2999, 2) -> fetch 2 registers
if err != nil || modBytes == nil {
fmt.Println(err)
}
encodedStr := hex.EncodeToString(modBytes) -> combine them to one big hexadecimal number
n, err := strconv.ParseUint(encodedStr, 16, 32)
if err != nil {
panic(err)
}
n2 := uint32(n)
f := *(*float32)(unsafe.Pointer(&n2))
b := float64(f) -> create a float64 integer for prometheus
phase_1_ampere.Set(b) -> the value we just created
http.Handle("/metrics", prometheus.Handler())
http.ListenAndServe(":8080", nil)
}
The text was updated successfully, but these errors were encountered:
Oh sorry I forgot this. I'm sorry but I'm struggling trying to find time as I maintain Flameshot, I have another project planned and I'm a student at university. I wrote this exporter a year ago and I'd have to re-study how the code works as I don't remember much about it (this was my last project in Golang too).
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
We have a modbus device that uses a combination of 2 registers to return a float32 result (for example ampere).
Is it possible to create support for this kind of values ?
As an example, following code supports this operation:
handler := modbus.NewTCPClientHandler("x.x.x.x:502")
handler.Timeout = 5 * time.Second
handler.SlaveId = 2
handler.Logger = log.New(os.Stdout, "tcp: ", log.LstdFlags)
handler.Connect()
defer handler.Close()
client := modbus.NewClient(handler)
var modBytes []byte
modBytes, err := client.ReadHoldingRegisters(2999, 2) -> fetch 2 registers
if err != nil || modBytes == nil {
fmt.Println(err)
}
encodedStr := hex.EncodeToString(modBytes) -> combine them to one big hexadecimal number
n, err := strconv.ParseUint(encodedStr, 16, 32)
if err != nil {
panic(err)
}
n2 := uint32(n)
f := *(*float32)(unsafe.Pointer(&n2))
b := float64(f) -> create a float64 integer for prometheus
phase_1_ampere.Set(b) -> the value we just created
http.Handle("/metrics", prometheus.Handler())
http.ListenAndServe(":8080", nil)
}
The text was updated successfully, but these errors were encountered: