-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
64 lines (57 loc) · 1.04 KB
/
main.go
File metadata and controls
64 lines (57 loc) · 1.04 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
package main
import (
"fmt"
"log"
"strconv"
"github.com/tarm/serial"
)
func getComPort() string {
isport := false
port := "COM"
max := 20
for i := 0; i < max; i++ {
port = "COM" + strconv.Itoa(i)
c := &serial.Config{Name: port, Baud: 9600}
isport = true
s, err := serial.OpenPort(c)
if err != nil {
isport = false
}
if isport {
i = max
s.Close()
}
}
return port
}
func main() {
fmt.Println("hello world ....")
port := getComPort()
fmt.Printf("Port : %s\n", port)
c := &serial.Config{Name: port, Baud: 9600}
s, err := serial.OpenPort(c)
if err != nil {
fmt.Println("Unable to open port")
log.Fatal(err)
}
line := ""
buf := make([]byte, 1)
on := true
n := 0
for on != false {
n, err = s.Read(buf)
if err != nil {
fmt.Println("Error ")
log.Fatal(err)
}
if string(buf[:n]) != "13" || string(buf[:n]) != "10" {
line = line + string(buf[:n])
}
fmt.Printf("%s", buf[:n])
// cr := bytes.Equal(buf[:n], []byte(13))
if string(buf[:n]) == "13" {
fmt.Println(line)
line = ""
}
}
}