forked from albenik/go-serial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_modem_bits_test.go
72 lines (66 loc) · 1.44 KB
/
example_modem_bits_test.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
//
// Copyright 2014-2017 Cristian Maglie. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
package serial_test
import (
"fmt"
"log"
"time"
"github.com/albenik/go-serial/v2"
)
func ExamplePort_GetModemStatusBits() {
// Open the first serial port detected at 9600bps N81
port, err := serial.Open("/dev/ttyACM1",
serial.WithBaudrate(9600),
serial.WithDataBits(8),
serial.WithParity(serial.NoParity),
serial.WithStopBits(serial.OneStopBit),
serial.WithReadTimeout(1000),
serial.WithWriteTimeout(1000),
)
if err != nil {
log.Fatal(err)
}
defer port.Close()
count := 0
for count < 25 {
status, err := port.GetModemStatusBits()
if err != nil {
log.Println(err) // DO NOT USER log.Fatal or `port.Close()` deferred call will never happened!
return
}
fmt.Printf("Status: %+v\n", status)
time.Sleep(time.Second)
count++
if count == 5 {
if err = port.SetDTR(false); err != nil {
log.Println(err)
return
}
fmt.Println("Set DTR OFF")
}
if count == 10 {
if err = port.SetDTR(true); err != nil {
log.Println(err)
return
}
fmt.Println("Set DTR ON")
}
if count == 15 {
if err = port.SetRTS(false); err != nil {
log.Println(err)
return
}
fmt.Println("Set RTS OFF")
}
if count == 20 {
if err = port.SetRTS(true); err != nil {
log.Println(err)
return
}
fmt.Println("Set RTS ON")
}
}
}