-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
165 lines (142 loc) · 4.08 KB
/
main.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"fmt"
"io"
"net"
"os"
"strconv"
"time"
"flag"
"github.com/tarm/serial"
)
var (
serialPortName = flag.String("serial-port", "COM2", "Port name of serial")
serialPortParity = flag.String("parity", "E", "Serial port parity (N, O, E,M, S)")
serialPortStopBit = flag.Int("stop-bits", 1, "Serial port stop bits (1, 15, 2)")
baudrate = flag.Int("baudrate", 9600, "Baudrate of serial port")
output = flag.Int("tcp-port", 9000, "TCP port output")
responseInterval = flag.Duration("response-interval", time.Second*2, "Wait before read serial response")
)
func main() {
flag.Parse()
fmt.Printf("Starting serial-port to IP converter\n")
fmt.Printf("Com port: %s, Baudrate: %d, Port: %d\n", *serialPortName, *baudrate, *output)
parity := serial.ParityEven
switch *serialPortParity {
case "N":
parity = serial.ParityNone
case "E":
parity = serial.ParityEven
case "O":
parity = serial.ParityOdd
case "M":
parity = serial.ParityMark
case "S":
parity = serial.ParitySpace
}
serConfig := serial.Config{Name: *serialPortName, Baud: *baudrate, Parity: parity, StopBits: serial.StopBits(*serialPortStopBit)}
serPort, err := serial.OpenPort(&serConfig)
if err != nil {
fmt.Fprintf(os.Stderr, "Can not open serial port: %s\n", err)
return
}
defer serPort.Close()
listener, err := net.Listen("tcp", ":"+strconv.Itoa(*output))
defer listener.Close()
ser2ipBuf := make([]byte, 1024)
ip2serBuf := make([]byte, 1024)
serPortReadChan := make(chan readResult)
serPortReadMore := make(chan bool)
go readProc(serPort, ser2ipBuf, serPortReadChan, serPortReadMore)
ipReadChan := make(chan readResult)
acceptChan := make(chan acceptResult)
acceptMore := make(chan bool)
go acceptProc(listener, acceptChan, acceptMore)
// Things that belong to the current connection
var currentCon net.Conn = nil
var currentReadMore chan bool = nil
var connErr error = nil
var serialErr error = nil
for {
select {
case readResult := <-serPortReadChan:
if readResult.err != nil {
serialErr = readResult.err
} else {
if currentCon != nil {
_, connErr = currentCon.Write(ser2ipBuf[0:readResult.bytesRead])
}
// Read more
serPortReadMore <- true
}
case readResult := <-ipReadChan:
if readResult.err != nil {
// Error reading from ip connection
connErr = readResult.err
} else {
_, serialErr = serPort.Write(ip2serBuf[0:readResult.bytesRead])
if serialErr == nil {
// Read more
currentReadMore <- true
}
}
case acceptResult := <-acceptChan:
if acceptResult.err != nil {
fmt.Fprintf(os.Stderr, "Can not accept connection: %s\n", acceptResult.err)
return
} else {
currentCon = acceptResult.conn
currentReadMore = make(chan bool)
go readProc(currentCon, ip2serBuf, ipReadChan, currentReadMore)
}
}
if serialErr != nil {
fmt.Fprintf(os.Stderr, "Error reading from serial port: %s\n", serialErr)
if currentCon != nil {
currentCon.Close()
return
}
}
if currentCon != nil && connErr != nil {
// Close the connection and accept a new one
currentCon.Close()
currentCon = nil
connErr = nil
acceptMore <- true
}
}
}
type readResult struct {
bytesRead int
err error
}
type acceptResult struct {
conn net.Conn
err error
}
// Reads from a reader and returns the results in a channel
// After that reading will be stopped until readMore is signaled to give the
// receiver a chance to work with everything in the buffer before we overwrite it
func readProc(src io.Reader, buf []byte, result chan readResult, readMore chan bool) {
for {
<-time.After(*responseInterval)
n, err := src.Read(buf)
result <- readResult{bytesRead: n, err: err}
_, ok := <-readMore
if !ok {
return
}
}
}
// Accepts connections in the goroutine
// After accepting a single connection accepting will be stopped until acceptMore is signaled
func acceptProc(listener net.Listener, result chan acceptResult, acceptMore chan bool) {
for {
conn, err := listener.Accept()
result <- acceptResult{conn: conn, err: err}
_, ok := <-acceptMore
if !ok {
return
}
}
}