forked from bugst/go-serial
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_modem_bits_test.go
70 lines (64 loc) · 1.22 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
//
// Copyright 2014-2021 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"
"go.bug.st/serial"
)
func ExampleGetSetModemBits() {
// Open the first serial port detected at 9600bps N81
mode := &serial.Mode{
BaudRate: 9600,
Parity: serial.NoParity,
DataBits: 8,
StopBits: serial.OneStopBit,
}
port, err := serial.Open("/dev/ttyACM1", mode)
if err != nil {
log.Fatal(err)
}
defer port.Close()
count := 0
for count < 25 {
status, err := port.GetModemStatusBits()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Status: %+v\n", status)
time.Sleep(time.Second)
count++
if count == 5 {
err := port.SetDTR(false)
if err != nil {
log.Fatal(err)
}
fmt.Println("Set DTR OFF")
}
if count == 10 {
err := port.SetDTR(true)
if err != nil {
log.Fatal(err)
}
fmt.Println("Set DTR ON")
}
if count == 15 {
err := port.SetRTS(false)
if err != nil {
log.Fatal(err)
}
fmt.Println("Set RTS OFF")
}
if count == 20 {
err := port.SetRTS(true)
if err != nil {
log.Fatal(err)
}
fmt.Println("Set RTS ON")
}
}
}