Improve error messages, close conn on transactionID mismatch.
This commit is contained in:
@@ -76,6 +76,7 @@ func (m *Mbclient) ReadRegisters(first uint16, numRegs uint16) ([]uint16, error)
|
||||
binary.BigEndian.PutUint16(req[8:10], first-1)
|
||||
binary.BigEndian.PutUint16(req[10:12], numRegs)
|
||||
m.conn.SetDeadline(time.Now().Add(5 * time.Second))
|
||||
fmt.Printf("%x", req)
|
||||
byteswritten, err := m.conn.Write(req)
|
||||
if err != nil {
|
||||
m.closeConn()
|
||||
@@ -95,7 +96,7 @@ func (m *Mbclient) ReadRegisters(first uint16, numRegs uint16) ([]uint16, error)
|
||||
expectedDataLength := responseHeader.length - 1
|
||||
|
||||
if m.transactionCounter != responseHeader.transactionID {
|
||||
m.t.Reset(0)
|
||||
m.closeConn()
|
||||
return nil, fmt.Errorf("modbus transaction mismatch %v != %v", m.transactionCounter, responseHeader.transactionID)
|
||||
}
|
||||
response := make([]byte, expectedDataLength)
|
||||
@@ -112,7 +113,7 @@ func (m *Mbclient) ReadRegisters(first uint16, numRegs uint16) ([]uint16, error)
|
||||
err = mbpayload.unMarshal(response)
|
||||
if mbpayload.functionCode != 3 {
|
||||
m.t.Reset(m.keepAliveDuration)
|
||||
return nil, fmt.Errorf("modbus exception %v req: %v", mbpayload.functionCode&0x7F, req)
|
||||
return nil, fmt.Errorf("modbus exception %v req: %x", mbpayload.functionCode&0x7F, req)
|
||||
}
|
||||
if err != nil {
|
||||
m.t.Reset(m.keepAliveDuration)
|
||||
|
||||
Reference in New Issue
Block a user