This repository was archived by the owner on Dec 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoulombmeter.go
More file actions
162 lines (151 loc) · 2.62 KB
/
coulombmeter.go
File metadata and controls
162 lines (151 loc) · 2.62 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
MQTT "github.com/eclipse/paho.mqtt.golang"
log "github.com/sirupsen/logrus"
"github.com/tarm/serial"
"io"
"os"
"time"
)
// parse state
const (
// wait start of frame
sof = iota
// wait soc
soc
// wait voltage
voltage
// wait capacity
capacity
// wait current
current
// wait time remaining
remaining
// wait crc
crc
)
// tf03 frame content
type frame struct {
Soc uint8 `json:"soc"`
DeciVolt uint16 `json:"decivolt"`
CapaMAh uint32 `json:"capamah"`
CurrentMA int32 `json:"currentma"`
RemainSec uint32 `json:"remainsec"`
Sum uint8 `json:"-"`
}
type Coulombmeter struct {
dev *serial.Port
port string
}
// init serial port
func (cm *Coulombmeter) init() error {
var err error
c := &serial.Config{Name: cm.port, Baud: 9600, ReadTimeout: 2 * time.Second}
cm.dev, err = serial.OpenPort(c)
if err != nil {
return err
}
// flush device buffers
rs := make([]byte, 300)
_, _ = cm.dev.Read(rs)
return nil
}
// frame parser goroutine
func (cm *Coulombmeter) parseFrame(ch chan uint8, ct MQTT.Client) {
var f frame
state := sof
var shift = 0
for c := range ch {
switch state {
case sof:
if c == 0xA5 {
state = soc
f.Sum = 0xA5
}
// ignore byte
continue
case soc:
f.Soc = c
f.Sum += c
state = voltage
shift = 8
continue
case voltage:
f.DeciVolt = f.DeciVolt | (uint16(c) << shift)
f.Sum += c
shift -= 8
if shift < 0 {
state = capacity
shift = 24
}
continue
case capacity:
f.CapaMAh = f.CapaMAh | (uint32(c) << shift)
f.Sum += c
shift -= 8
if shift < 0 {
state = current
shift = 24
}
continue
case current:
f.CurrentMA = f.CurrentMA | (int32(c) << shift)
f.Sum += c
shift -= 8
if shift < 0 {
state = remaining
shift = 16
}
continue
case remaining:
f.RemainSec = f.RemainSec | (uint32(c) << shift)
f.Sum += c
shift -= 8
if shift < 0 {
state = crc
}
continue
case crc:
if f.Sum == c {
err := publishAsJson(cfg.Topic, f, ct)
if err != nil {
log.Errorln(err)
os.Exit(-3)
}
}
state = sof
shift = 0
f.reset()
continue
}
}
}
// reset frame
func (f *frame) reset() {
f.RemainSec = 0
f.CurrentMA = 0
f.CapaMAh = 0
f.DeciVolt = 0
f.Soc = 0
f.Sum = 0
}
// serial port infinite reader
func (cm *Coulombmeter) poll(ch chan uint8) error {
rs := make([]byte, 64)
defer close(ch)
for true {
n, err := cm.dev.Read(rs)
if err == io.EOF || n == 0 {
log.Warnln("timeout")
continue
}
if err != nil {
log.Errorln(err)
return err
}
for i := 0; i < n; i++ {
ch <- rs[i]
}
}
return nil
}