-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.go
145 lines (123 loc) · 3.43 KB
/
serial.go
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
/*
Copyright © 2024 Martin Marsh [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
*/
package nmea_mux
import (
"fmt"
"github.com/martinmarsh/nmea-mux/io"
"strconv"
"time"
"slices"
)
func (n *NmeaMux) serialProcess(name string) error {
(n.Monitor_channel) <- fmt.Sprintf("started navmux serial %s", name)
config := n.Config.Values[name]
var baud int64 = 4800
var err error = nil
var report_tx = false
var report_rx = false
tag := ""
if origin_tags, found := config["origin_tag"]; found {
if len(origin_tags) > 0 {
tag = fmt.Sprintf("@%s@", origin_tags[0])
}
}
if baud_list, found := config["baud"]; found {
if len(baud_list) > 0 {
if baud, err = strconv.ParseInt(baud_list[0], 10, 32); err != nil {
baud = 4800
}
}
}
portName := config["name"][0]
if slices.Contains(n.monitor_report, "device"){
if reports, found := config["report"]; found {
for _, v := range(reports){
switch v{
case "tx":
report_tx = true
case "rx":
report_rx = true
}
}
}
}
n.SerialIoDevices[name].SetMode(int(baud), portName)
(n.Monitor_channel) <- fmt.Sprintf("Serial device %s baud rate set to %d", name, baud)
err = n.SerialIoDevices[name].Open()
if err != nil {
(n.Monitor_channel) <- fmt.Sprintf("Serial device %s <name> == <%s> should be a valid port error: %s",
name, portName, err)
} else {
if outputs, found := config["outputs"]; found {
if len(outputs) > 0 {
(n.Monitor_channel) <- fmt.Sprintf("Open read serial port " + portName)
go serialReader(name, n.SerialIoDevices[name], outputs, tag, &n.Monitor_channel, &n.Channels, report_rx)
}
}
if inputs, found := config["input"]; found {
if len(inputs) == 1 {
(n.Monitor_channel) <- fmt.Sprintf("Open write serial port " + portName)
go serialWriter(name, n.SerialIoDevices[name], inputs[0], &n.Monitor_channel, &n.Channels, report_tx)
}
}
}
return nil
}
func serialReader(name string, ser io.Serial_interfacer, outputs []string, tag string, monitor_channel *chan string,
channels *map[string](chan string), report_rx bool) {
buff := make([]byte, 25)
cb := MakeByteBuffer(400, 92)
time.Sleep(100 * time.Millisecond)
for {
n, err := ser.Read(&buff)
if err != nil {
*(monitor_channel) <- fmt.Sprintf("FATAL Error on port %s", name)
time.Sleep(5 * time.Second)
}
if n == 0 {
*(monitor_channel) <- fmt.Sprintf("EOF on read of %s", name)
time.Sleep(5 * time.Second)
} else {
for i := 0; i < n; i++ {
if buff[i] != 10 {
cb.Write_byte(buff[i])
}
}
}
for {
str, err := cb.ReadString()
if err != nil {
*(monitor_channel) <- fmt.Sprintf("Serial read error in %s error %s", name, err)
}
if len(str) == 0 {
break
}
str = tag + str
if report_rx {
*(monitor_channel) <- fmt.Sprintf("Serial %s Rx: %s", name, str)
}
for _, out := range outputs {
(*channels)[out] <- str
}
}
}
}
func serialWriter(name string, ser io.Serial_interfacer, input string, monitor_channel *chan string,
channels *map[string](chan string), report_tx bool) {
time.Sleep(100 * time.Millisecond)
for {
str := <-(*channels)[input]
_, str = trim_tag(str)
str += "\r\n"
if report_tx {
*(monitor_channel) <- fmt.Sprintf("Serial %s Tx: %s", name, str)
}
_, err := ser.Write([]byte(str))
if err != nil {
*(monitor_channel) <- fmt.Sprintf("Serial write error in %s error %s", name, err)
time.Sleep(time.Minute)
}
}
}