-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconn.go
More file actions
167 lines (137 loc) · 2.94 KB
/
conn.go
File metadata and controls
167 lines (137 loc) · 2.94 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
162
163
164
165
166
167
// Copyright 2022 TrueLevel SA
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// SPDX-License-Identifier: MPL-2.0
package nd300
import (
"fmt"
"time"
"go.bug.st/serial"
)
const (
DefaultBaudRate = 9600
DefaultParityBit = 8
interReadDelay = 5 * time.Millisecond
)
var SerialMode = &serial.Mode{
BaudRate: DefaultBaudRate,
DataBits: DefaultParityBit,
Parity: serial.EvenParity,
StopBits: serial.OneStopBit,
}
type BadWriteSizeError int
func (e BadWriteSizeError) Error() string {
return fmt.Sprintf("bad write size %d", e)
}
type Conn struct {
port serial.Port
mode *serial.Mode
log chan<- SerialMsg
Log <-chan SerialMsg
portName string
_rxBuff []byte
rxBuff Msg
txBuff Msg
timeout time.Duration
}
func NewDefaultConn(port string, machineno byte, timeout time.Duration, logBuff int) *Conn {
return NewConn(
port,
SerialMode,
STX,
machineno,
MsgLen,
timeout,
logBuff,
)
}
func NewConn(
port string,
mode *serial.Mode,
stx byte,
machineno byte,
msgLen int,
timeout time.Duration,
logBuff int,
) *Conn {
if mode == nil {
mode = SerialMode
}
c := &Conn{
port: nil,
mode: mode,
portName: port,
_rxBuff: make([]byte, msgLen),
rxBuff: make([]byte, msgLen),
txBuff: make([]byte, msgLen),
timeout: timeout,
}
c.txBuff[idxStx] = stx
c.txBuff[idxMS] = CmdFlag
c.txBuff[idxMachine] = machineno
if logBuff >= 0 {
log := make(chan SerialMsg, logBuff)
c.log, c.Log = log, log
}
return c
}
func (c *Conn) Open() (serial.Port, error) { //nolint:ireturn // Return what serial.Open returns.
if c.port != nil {
return c.port, nil
}
var err error
if c.port, err = serial.Open(c.portName, c.mode); err != nil {
return c.port, fmt.Errorf("failed to open serial port: %w", err)
}
if err = c.port.SetReadTimeout(c.timeout); err != nil {
return c.port, fmt.Errorf("failed to set timeout: %w", err)
}
return c.port, nil
}
func (c *Conn) read() error {
var (
size int
err error
i int
)
pos := 0
for pos < len(c._rxBuff) {
size, err = c.port.Read(c._rxBuff[pos:])
if err != nil {
return fmt.Errorf("failed to read msg: %w", err)
}
for i = 0; i < size; i++ {
c.rxBuff[pos+i] = c._rxBuff[pos+i]
}
pos += size
time.Sleep(interReadDelay) // Sadly no better option right now...
}
if c.log != nil {
c.log <- SerialMsg{
Type: RX,
Data: c.rxBuff[:],
Err: err,
}
}
return c.rxBuff.ValidateAsStatus()
}
func (c *Conn) write() error {
size, err := c.port.Write(c.txBuff)
switch {
case err != nil:
err = fmt.Errorf("failed to write msg: %w", err)
case size != len(c.txBuff):
err = fmt.Errorf("%w: should have written %d", BadWriteSizeError(size), len(c.txBuff))
}
if c.log != nil {
c.log <- SerialMsg{
Type: TX,
Data: c.txBuff[:],
Err: err,
}
}
return err
}