-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (57 loc) · 1.18 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
// A Serial Assistant tool writed by Go.
package main
import (
"flag"
"fmt"
"log"
"go.bug.st/serial"
)
// Port Configuration.
var confPort = flag.String("c", "COM6", "Serial port like COM6.")
var confBaud = flag.Int("b", 115200, "BaudRate.")
// Function.
var confList = flag.Bool("l", false, "List the ports.")
func main() {
flag.Parse()
if *confList {
getPortList()
} else {
mode := &serial.Mode{
BaudRate: *confBaud,
}
port, err := serial.Open(*confPort, mode)
if err != nil {
fmt.Printf("[error] Can't open port.")
}
err = port.SetMode(mode)
if err != nil {
fmt.Printf("[error] Can't set mode.")
}
buff := make([]byte, 100)
for {
n, err := port.Read(buff)
if err != nil {
log.Fatal(err)
break
}
if n == 0 {
fmt.Println("\nEOF")
break
}
fmt.Printf("%v", string(buff[:n]))
}
}
}
// Get the list of Serial Ports.
func getPortList() {
ports, err := serial.GetPortsList()
if err != nil {
fmt.Printf("[error] Can't get list.")
}
if len(ports) == 0 {
fmt.Printf("[warning] No Port.")
}
for _, port := range ports {
fmt.Printf("Found port: %v\n", port)
}
}