-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (52 loc) · 1.23 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
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"sync"
"github.com/tarm/serial"
)
const DEFAULT_BAUD_RATE = 115200
func main() {
flag.Usage = func() {
name := filepath.Base(os.Args[0])
fmt.Fprintf(os.Stderr, "USAGE: %s <serial-device> [options]\n", name)
fmt.Fprint(os.Stderr, "Options:\n")
flag.PrintDefaults()
fmt.Fprintln(os.Stderr, "\n")
fmt.Fprintln(os.Stderr, "Example: ")
fmt.Fprintf(os.Stderr, "\t %s /dev/ttyUSB0 --baud-rate 115200 \n", name)
}
baudRate := flag.Int("baud-rate", DEFAULT_BAUD_RATE, "The speed of the serial device")
if len(os.Args) < 2 {
flag.Usage()
os.Exit(1)
}
devicePath := os.Args[1]
config := &serial.Config{Name: devicePath, Baud: *baudRate}
serialPort, err := serial.OpenPort(config)
if err != nil {
log.Fatalf("Could not open serial device %s", devicePath)
}
defer serialPort.Close()
wg := sync.WaitGroup{}
wg.Add(2)
// redirect the stdin to the serial device
go func() {
defer wg.Done()
if _, err := io.Copy(serialPort, os.Stdin); err != nil {
log.Fatal(err)
}
}()
// redirect the serial device to stdout
go func() {
defer wg.Done()
if _, err := io.Copy(os.Stdout, serialPort); err != nil {
log.Fatal(err)
}
}()
wg.Wait()
}