Skip to content
This repository has been archived by the owner on Apr 2, 2021. It is now read-only.

Add support for float32 registers #1

Open
tom-myny opened this issue Jan 4, 2018 · 4 comments
Open

Add support for float32 registers #1

tom-myny opened this issue Jan 4, 2018 · 4 comments

Comments

@tom-myny
Copy link

tom-myny commented Jan 4, 2018

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)
}

@lupoDharkael
Copy link
Owner

lupoDharkael commented Jan 5, 2018

I'm having exams the next 2 weeks. I'll check it when I have time.

@tom-myny
Copy link
Author

tom-myny commented Jan 5, 2018 via email

@tom-myny
Copy link
Author

tom-myny commented May 2, 2018

Any idea or can I help in this issue :)

@lupoDharkael
Copy link
Owner

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 free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants