lrc.go 598 B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright 2014 Quoc-Viet Nguyen. All rights reserved.
  2. // This software may be modified and distributed under the terms
  3. // of the BSD license. See the LICENSE file for details.
  4. package modbus
  5. // Longitudinal Redundancy Checking
  6. type lrc struct {
  7. sum uint8
  8. }
  9. func (lrc *lrc) reset() *lrc {
  10. lrc.sum = 0
  11. return lrc
  12. }
  13. func (lrc *lrc) pushByte(b byte) *lrc {
  14. lrc.sum += b
  15. return lrc
  16. }
  17. func (lrc *lrc) pushBytes(data []byte) *lrc {
  18. var b byte
  19. for _, b = range data {
  20. lrc.sum += b
  21. }
  22. return lrc
  23. }
  24. func (lrc *lrc) value() byte {
  25. // Return twos complement
  26. return uint8(-int8(lrc.sum))
  27. }