| 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 | |
| 5 | package modbus |
| 6 | |
| 7 | // Longitudinal Redundancy Checking |
| 8 | type lrc struct { |
| 9 | sum uint8 |
| 10 | } |
| 11 | |
| 12 | func (lrc *lrc) reset() *lrc { |
| 13 | lrc.sum = 0 |
| 14 | return lrc |
| 15 | } |
| 16 | |
| 17 | func (lrc *lrc) pushByte(b byte) *lrc { |
| 18 | lrc.sum += b |
| 19 | return lrc |
| 20 | } |
| 21 | |
| 22 | func (lrc *lrc) pushBytes(data []byte) *lrc { |
| 23 | var b byte |
| 24 | for _, b = range data { |
| 25 | lrc.sum += b |
| 26 | } |
| 27 | return lrc |
| 28 | } |
| 29 | |
| 30 | func (lrc *lrc) value() byte { |
| 31 | // Return twos complement |
| 32 | return uint8(-int8(lrc.sum)) |
| 33 | } |