36 lines
904 B
Go
36 lines
904 B
Go
package main
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"fmt"
|
|
)
|
|
|
|
// Helpers for converting straight from modbus data to floats.
|
|
|
|
func u16(data []byte) (float64, error) {
|
|
n := binary.BigEndian.Uint16(data)
|
|
if n == 0xFFFF {
|
|
// probably a misread
|
|
return 0, fmt.Errorf("likely misread: u16 read %v", n)
|
|
}
|
|
return float64(n), nil
|
|
}
|
|
|
|
func s16(data []byte) (float64, error) {
|
|
return float64(int16(binary.BigEndian.Uint16(data))), nil
|
|
}
|
|
|
|
func u32(data []byte) (float64, error) {
|
|
// Little-endian big-endian :#
|
|
n := uint32(binary.BigEndian.Uint16(data)) + uint32(binary.BigEndian.Uint16(data[2:]))<<16
|
|
if n == 0xFFFFFFFF {
|
|
// probably a misread
|
|
return 0, fmt.Errorf("likely misread: u32 read %v", n)
|
|
}
|
|
return float64(n), nil
|
|
}
|
|
|
|
func s32(data []byte) (float64, error) {
|
|
// Little-endian big-endian :#
|
|
return float64(int32(binary.BigEndian.Uint16(data)) + int32(binary.BigEndian.Uint16(data[2:]))<<16), nil
|
|
}
|