Skip to content

Commit

Permalink
Merge pull request goburrow#15 from xiegeo/master
Browse files Browse the repository at this point in the history
switch around meaning of high and low byte of crc as per implementation guide
  • Loading branch information
nqv committed Oct 10, 2016
2 parents 5b97c12 + e6659ee commit f7afd8d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions crc.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ func (crc *crc) pushBytes(bs []byte) *crc {
var idx, b byte

for _, b = range bs {
idx = crc.high ^ b
crc.high = crc.low ^ crcHighBytes[idx]
crc.low = crcLowBytes[idx]
idx = crc.low ^ b
crc.low = crc.high ^ crcHighBytes[idx]
crc.high = crcLowBytes[idx]
}
return crc
}
Expand Down
4 changes: 2 additions & 2 deletions crc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestCRC(t *testing.T) {
crc.reset()
crc.pushBytes([]byte{0x02, 0x07})

if 0x4112 != crc.value() {
t.Fatalf("crc expected %v, actual %v", 0x4112, crc.value())
if 0x1241 != crc.value() {
t.Fatalf("crc expected %v, actual %v", 0x1241, crc.value())
}
}
6 changes: 3 additions & 3 deletions rtuclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func (mb *rtuPackager) Encode(pdu *ProtocolDataUnit) (adu []byte, err error) {
crc.reset().pushBytes(adu[0 : length-2])
checksum := crc.value()

adu[length-2] = byte(checksum >> 8)
adu[length-1] = byte(checksum)
adu[length-1] = byte(checksum >> 8)
adu[length-2] = byte(checksum)
return
}

Expand All @@ -90,7 +90,7 @@ func (mb *rtuPackager) Decode(adu []byte) (pdu *ProtocolDataUnit, err error) {
// Calculate checksum
var crc crc
crc.reset().pushBytes(adu[0 : length-2])
checksum := uint16(adu[length-2])<<8 | uint16(adu[length-1])
checksum := uint16(adu[length-1])<<8 | uint16(adu[length-2])
if checksum != crc.value() {
err = fmt.Errorf("modbus: response crc '%v' does not match expected '%v'", checksum, crc.value())
return
Expand Down

0 comments on commit f7afd8d

Please sign in to comment.