This repository has been archived by the owner on Dec 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserial.go
129 lines (114 loc) · 3.69 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
// Copyright (C) 2017, 2019 Steve Merrony
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package main
import (
"fmt"
"time"
"github.com/distributed/sers"
)
const breakMs = 110 // How many ms to hold BREAK signal
type serialSessionT struct {
serPort sers.SerialPort
sendSerialBreakChan chan bool
stopSerialWriterChan chan bool
}
func newSerialSession() *serialSessionT {
ser := new(serialSessionT)
ser.sendSerialBreakChan = make(chan bool)
ser.stopSerialWriterChan = make(chan bool, 2)
return ser
}
func (ser *serialSessionT) openSerialPort(port string, baud int, bits int, parityStr string, stopBits int) bool {
var parity int
var err error
switch parityStr {
case "None":
parity = sers.N
case "Even":
parity = sers.E
case "Odd":
parity = sers.O
}
ser.serPort, err = sers.Open(port) // serial.Open(options)
if err != nil {
fmt.Printf("ERROR: Could not open serial port - %s\n", err.Error())
return false
}
if err = ser.serPort.SetMode(baud, bits, parity, stopBits, sers.NO_HANDSHAKE); err != nil {
fmt.Printf("ERROR: Could not set serial part mode as requested - %s\n", err.Error())
return false
}
go ser.serialReader(fromHostChan)
go ser.serialWriter(keyboardChan)
terminal.rwMutex.Lock()
terminal.connectionType = serialConnected
terminal.serialPort = port
terminal.rwMutex.Unlock()
return true
}
func (ser *serialSessionT) getParms() string {
intMode, _ := ser.serPort.GetMode()
return intMode.String()
}
func (ser *serialSessionT) closeSerialPort() {
ser.serPort.Close()
//sendSerialBreakChan = nil
ser.stopSerialWriterChan <- true
terminal.rwMutex.Lock()
terminal.connectionType = disconnected
terminal.rwMutex.Unlock()
}
func (ser *serialSessionT) serialReader(hostChan chan []byte) {
for {
hostBytes := make([]byte, hostBuffSize)
n, err := ser.serPort.Read(hostBytes)
if n == 0 {
fmt.Println("WARNING: serialReader got zero length message")
if err == nil {
continue
}
}
if err != nil {
fmt.Printf("WARNING: Could not read from Serial Port - %s\n", err.Error())
fmt.Println("WARNING: Stopping serialReader and requesting serialWriter stop")
ser.stopSerialWriterChan <- true
return
}
hostChan <- hostBytes[:n]
}
}
func (ser *serialSessionT) serialWriter(kbdChan chan byte) {
// drain stop chan in case of multiple stops queued
for len(ser.stopSerialWriterChan) > 0 {
<-ser.stopSerialWriterChan
}
for {
select {
case k := <-kbdChan:
ser.serPort.Write([]byte{k})
case sb := <-ser.sendSerialBreakChan:
if sb {
ser.serPort.SetBreak(true)
time.Sleep(breakMs * time.Millisecond)
ser.serPort.SetBreak(false)
}
case <-ser.stopSerialWriterChan:
fmt.Println("INFO: serialWriter stopping")
return
}
}
}