-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsers_test.go
More file actions
53 lines (44 loc) · 1.22 KB
/
sers_test.go
File metadata and controls
53 lines (44 loc) · 1.22 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
package sers_test
import (
"encoding/hex"
"fmt"
"log"
"github.com/distributed/sers"
)
// This program opens a serial port, configurable by changing portname
// below and configures it for 57600 baud, 8 data bits, no parity bit,
// 1 stop bit, no handshaking. It then reads up to 128 bytes from the serial
// port, with a timeout of 1 second and prints the received
// bytes to stdout.
func Example() {
portname := "/dev/ttyUSB0"
rb, err := readFirstBytesFromPort(portname)
if err != nil {
log.Fatal(err)
}
fmt.Printf("got %d bytes from %s:\n%s", len(rb), portname,
hex.Dump(rb))
}
func readFirstBytesFromPort(fn string) ([]byte, error) {
sp, err := sers.Open(fn)
if err != nil {
return nil, err
}
defer sp.Close()
// 57600 baud, 8 data bits, no parity bit, 1 stop bit
// no handshake. non-standard baud rates are possible.
err = sp.SetMode(57600, 8, sers.N, 1, sers.NO_HANDSHAKE)
if err != nil {
return nil, err
}
// setting:
// minread = 0: minimal buffering on read, return characters as early as possible
// timeout = 1.0: time out if after 1.0 seconds nothing is received
err = sp.SetReadParams(0, 1.0)
if err != nil {
return nil, err
}
var rb [128]byte
n, err := sp.Read(rb[:])
return rb[:n], err
}