Skip to content

Commit

Permalink
better name for MSW; more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Nov 7, 2023
1 parent 4dcc37d commit d56bec6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
25 changes: 14 additions & 11 deletions peamodbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,30 +313,35 @@ func (dm *lockedDataModel) SetDiscreteInput(i int, v bool) Exception {

func NewDataInterpreter(cfg DataInterpreterConfig) (*DataInterpreter, error) {
di := &DataInterpreter{
badFloat: float32(cfg.BadFloat),
invertWordOrder: cfg.InvertWordOrder,
badFloat: float32(cfg.BadFloat),
mswFirst: cfg.MostSignificantWordFirst,
}
return di, nil
}

type DataInterpreterConfig struct {
BadFloat float64
InvertWordOrder bool
// BadFloat is the value to return when a float conversion fails.
// Ideally the exception should be checked and the value ignored.
BadFloat float64
// MostSignificantWordFirst can be set to interpret contiguous modbus 16bit register data
// as having the first 16bit register be the most significant word (MSW).
// If not set the first 16bit register is the least significant word (LSW).
MostSignificantWordFirst bool
}

// DataInterpreter provides primitive data type reading and writing from and to
// modbus [DataModel]s.
type DataInterpreter struct {
badFloat float32
invertWordOrder bool
badFloat float32
mswFirst bool
}

// putUintReg is the basic building block for putting large integers into consecutive 16bit registers of a data model.
func (interpret *DataInterpreter) putUintReg(regSet func(int, uint16) Exception, dm DataModel, startIdx, sizeWords int, value uint64) (_ Exception) {
if sizeWords < 2 || sizeWords > 4 || regSet == nil {
panic("bad sizeWords/nil func") // Internal bug.
}
if interpret.invertWordOrder {
if interpret.mswFirst {
for i := 0; i < sizeWords; i++ {
shift := (sizeWords - i - 1) * 16
exc := regSet(startIdx+i, uint16(value>>shift))
Expand All @@ -353,17 +358,15 @@ func (interpret *DataInterpreter) putUintReg(regSet func(int, uint16) Exception,
}
}
}

return 0

return ExceptionNone
}

// uintReg is the basic building block for interpreting consecutive 16bit registers.
func (interpret *DataInterpreter) uintReg(regGet func(int) (uint16, Exception), dm DataModel, startIdx, sizeWords int) (v uint64, _ Exception) {
if sizeWords < 2 || sizeWords > 4 || regGet == nil {
panic("bad sizeWords/nil func") // Internal bug.
}
if interpret.invertWordOrder {
if interpret.mswFirst {
for i := 0; i < sizeWords; i++ {
u16, exc := regGet(startIdx + i)
if exc != ExceptionNone {
Expand Down
8 changes: 4 additions & 4 deletions peamodbus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,11 @@ func TestInferTxRequestLength(t *testing.T) {
}
}

func TestDataInterpreter_inverted(t *testing.T) {
func TestDataInterpreter_msw(t *testing.T) {
data := &BlockedModel{}
model := ConcurrencySafeDataModel(data)
interpret, _ := NewDataInterpreter(DataInterpreterConfig{
InvertWordOrder: true,
MostSignificantWordFirst: true,
})
rng := rand.New(rand.NewSource(1))
var buf [8]byte
Expand Down Expand Up @@ -401,11 +401,11 @@ func TestDataInterpreter_inverted(t *testing.T) {
}
}

func TestDataInterpreter_notinverted(t *testing.T) {
func TestDataInterpreter_lsw(t *testing.T) {
data := &BlockedModel{}
model := ConcurrencySafeDataModel(data)
interpret, _ := NewDataInterpreter(DataInterpreterConfig{
InvertWordOrder: false,
MostSignificantWordFirst: false,
})
rng := rand.New(rand.NewSource(1))
var buf [8]byte
Expand Down

0 comments on commit d56bec6

Please sign in to comment.