-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttyosc.go
103 lines (87 loc) · 1.93 KB
/
ttyosc.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
package main
import (
"flag"
"log"
"net"
"github.com/Lobaro/slip"
"go.bug.st/serial"
)
var tty = flag.String("t", "/dev/ttyUSB0", "serial device")
var remote = flag.String("r", ":6010", "remote address: osc from serial is sent here")
var local = flag.String("l", ":6020", "local address: send your osc here")
var baudRate = flag.Int("baud", 115200, "serial connection's baudrate")
var verbose = flag.Bool("v", false, "verbose")
func main() {
flag.Parse()
// Serial connection
mode := &serial.Mode{
BaudRate: *baudRate,
}
port, err := serial.Open(*tty, mode)
if err != nil {
log.Fatal(err)
}
var done = make(chan error)
// udp -> serial communication
localAddress, err := net.ResolveUDPAddr("udp", *local)
if err != nil {
log.Fatal(err)
}
log.Println("local address:", localAddress.String())
go func() {
writer := slip.NewWriter(port)
// open UDP server
conn, err := net.ListenUDP("udp", localAddress)
if err != nil {
done <- err
return
}
defer conn.Close()
// listen to incoming data from UDP and send to serial
buf := make([]byte, 1024)
for {
n, _, err := conn.ReadFromUDP(buf)
writer.WritePacket(buf[:n])
if *verbose {
log.Println("udp -> serial")
}
if err != nil {
done <- err
return
}
}
}()
// serial -> udp communication
remoteAddress, err := net.ResolveUDPAddr("udp", *remote)
if err != nil {
log.Fatal(err)
}
log.Println("remote address:", remoteAddress.String())
go func() {
reader := slip.NewReader(port)
// open UDP client
conn, err := net.DialUDP("udp", nil, remoteAddress)
if err != nil {
done <- err
return
}
defer conn.Close()
// listen to incoming data from serial and send to UDP
for {
p, _, err := reader.ReadPacket()
if err != nil {
done <- err
return
}
_, err = conn.Write(p)
if err != nil {
done <- err
return
}
if *verbose {
log.Println("serial -> udp")
}
}
}()
log.Fatal(<-done)
}