separate modbus.go doesn't make sense for now

This commit is contained in:
Josh Deprez 2021-01-12 10:49:06 +11:00
parent 39a9b7434b
commit 942415760d
2 changed files with 28 additions and 36 deletions

View file

@ -1,36 +0,0 @@
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
}

View file

@ -63,3 +63,31 @@ func frequency2(data []byte) (float64, error) {
} }
return float64(n), nil return float64(n), nil
} }
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
}