File: lrc.go

package info (click to toggle)
golang-github-goburrow-modbus 0.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 192 kB
  • sloc: ansic: 87; makefile: 2
file content (33 lines) | stat: -rw-r--r-- 598 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Copyright 2014 Quoc-Viet Nguyen. All rights reserved.
// This software may be modified and distributed under the terms
// of the BSD license. See the LICENSE file for details.

package modbus

// Longitudinal Redundancy Checking
type lrc struct {
	sum uint8
}

func (lrc *lrc) reset() *lrc {
	lrc.sum = 0
	return lrc
}

func (lrc *lrc) pushByte(b byte) *lrc {
	lrc.sum += b
	return lrc
}

func (lrc *lrc) pushBytes(data []byte) *lrc {
	var b byte
	for _, b = range data {
		lrc.sum += b
	}
	return lrc
}

func (lrc *lrc) value() byte {
	// Return twos complement
	return uint8(-int8(lrc.sum))
}