sungrow/modbus.go

24 lines
612 B
Go
Raw Normal View History

2021-01-01 16:51:16 +11:00
package main
import "encoding/binary"
// Helpers for converting straight from modbus data to floats.
func u16(data []byte) float64 {
return float64(binary.BigEndian.Uint16(data))
}
func s16(data []byte) float64 {
return float64(int16(binary.BigEndian.Uint16(data)))
}
func u32(data []byte) float64 {
// Little-endian big-endian :#
return float64(uint32(binary.BigEndian.Uint16(data)) + uint32(binary.BigEndian.Uint16(data[2:]))<<16)
}
func s32(data []byte) float64 {
// Little-endian big-endian :#
return float64(int32(binary.BigEndian.Uint16(data)) + int32(binary.BigEndian.Uint16(data[2:]))<<16)
}