-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
157 lines (126 loc) · 3.28 KB
/
main.go
File metadata and controls
157 lines (126 loc) · 3.28 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
package main
import (
"bufio"
"errors"
"fmt"
"os"
"strconv"
"strings"
"github.com/tarm/serial"
)
func main() {
// args[0] is the exe path, which we don't need
args := os.Args[1:]
inputPort := ""
inputBaudRate := 115200
inputNewline := "\n"
// the first argument has to be the USB/Serial Device
if len(args) > 0 {
// find args[0] in serialPorts
inputPort = args[0]
} else {
criticalError("Provide a port name")
}
// Optionally, the second argument can be the baud rate.
// Default rate is 115200
if len(args) > 1 {
i, err := strconv.Atoi(args[1])
if err != nil {
// if the 2nd parameter is not a valid baudrate,
// it could be the newline as baudrate is optional.
newline, newline_err := parseNewline(args[1])
// if not a valid newline, show the baudrate error
if newline_err != nil {
criticalError("Invalid baud rate")
}
inputNewline = newline
} else if i < 0 {
criticalError("Invalid baud rate")
} else {
inputBaudRate = i
// if it was a valid baudrate and there is a third argument,
// try to parse it as a newline.
if len(args) > 2 {
newline, err := parseNewline(args[2])
if err != nil {
fmt.Println(err)
criticalError("Invalid newline character")
}
inputNewline = newline
}
}
}
begin(inputPort, inputBaudRate, inputNewline)
}
func parseNewline(arg string) (string, error) {
switch arg {
case "CR": // carriage-return
return "\r", nil
case "LF": // line-feed
return "\n", nil
case "CRLF": // carriage-return line-feed
return "\r\n", nil
case "LFCR": // line-feed carriage-return
return "\n\r", nil
default:
return "", errors.New("invalid newline")
}
}
// Opens a new connection on the specified port at the specified baud rate
func begin(portName string, baudRate int, newline string) {
config := &serial.Config{
Baud: baudRate,
Name: portName,
}
port, err := serial.OpenPort(config)
if err != nil {
criticalError("Error opening serial port: " + err.Error())
}
// close the port when this method exits
defer func(port *serial.Port) {
err := port.Close()
if err != nil {
criticalError("Error closing serial port: " + err.Error() + ". The connection might stay open.")
}
}(port)
// asynchronously read input
go userInput(port, newline)
// while also receiving data from the serial device
buf := make([]byte, 1024)
for {
n, err := port.Read(buf)
if err != nil {
criticalError("Error reading from serial port: " + err.Error())
}
fmt.Printf("%s", string(buf[:n]))
}
}
// reads the standard input until the next EOF,
// or until the input line equals "exit"
func userInput(port *serial.Port, newline string) {
reader := bufio.NewReader(os.Stdin)
for {
input, err := reader.ReadString('\n')
if err != nil {
if err.Error() == "EOF" {
os.Exit(0)
}
criticalError("Error reading input: " + err.Error())
}
if len(input) == 0 {
continue
}
input = strings.TrimSpace(input)
// write the input data to the port,
// after appending the defined newline character to it
_, err = port.Write([]byte(input + newline))
if err != nil {
criticalError("Error writing to serial port: " + err.Error())
}
}
}
// helper method to print a critical error and exit with code 1
func criticalError(message string) {
fmt.Println(message)
os.Exit(1)
}