complete modbus mock

This commit is contained in:
Javier Peletier
2020-12-16 00:53:13 +01:00
parent ca479304f2
commit 76be5bcd2c
+4 -3
View File
@@ -12,8 +12,8 @@ type Mock struct {
func NewMock() Modbus { func NewMock() Modbus {
return &Mock{ return &Mock{
State: map[byte][]uint16{ State: map[byte][]uint16{
49: {3, 68, 41, 41, 3, 68, 41, 41, 3, 68, 41, 45, 3, 68, 41, 45, 3, 68, 41, 42, 3, 52, 41, 40, 3, 52, 41, 44, 3, 68, 41, 41, 3, 68, 41, 40, 3, 68, 41, 41, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0}, 49: {3, 68, 41, 41, 3, 68, 41, 41, 3, 68, 41, 45, 3, 68, 41, 45, 3, 68, 41, 42, 3, 52, 41, 40, 3, 52, 41, 44, 3, 68, 41, 41, 3, 68, 41, 40, 3, 68, 41, 41, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 3, 4, 2, 49, 3, 7, 1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
50: {3, 68, 41, 41, 3, 68, 41, 41, 3, 68, 41, 45, 3, 68, 41, 45, 3, 68, 41, 42, 3, 52, 41, 40, 3, 52, 41, 44, 3, 68, 41, 41, 3, 68, 41, 40, 3, 68, 41, 41, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0}, 50: {3, 68, 41, 41, 3, 68, 41, 41, 3, 68, 41, 45, 3, 68, 41, 45, 3, 68, 41, 42, 3, 52, 41, 40, 3, 52, 41, 44, 3, 68, 41, 41, 3, 68, 41, 40, 3, 68, 41, 41, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 3, 4, 2, 49, 3, 7, 1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}, },
} }
} }
@@ -23,6 +23,7 @@ func (ms *Mock) ReadRegister(slaveID byte, address uint16, quantity uint16) (res
if !ok { if !ok {
return nil, errors.New("Unknown slave") return nil, errors.New("Unknown slave")
} }
address--
for a := address; a < address+quantity; a++ { for a := address; a < address+quantity; a++ {
b := make([]byte, 2) b := make([]byte, 2)
binary.BigEndian.PutUint16(b, state[a]) binary.BigEndian.PutUint16(b, state[a])
@@ -35,7 +36,7 @@ func (ms *Mock) WriteRegister(slaveID byte, address uint16, value uint16) (resul
if !ok { if !ok {
return nil, errors.New("Unknown slave") return nil, errors.New("Unknown slave")
} }
state[address] = value state[address-1] = value
results = make([]byte, 2) results = make([]byte, 2)
binary.BigEndian.PutUint16(results, value) binary.BigEndian.PutUint16(results, value)
return results, nil return results, nil