-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
113 lines (95 loc) · 2.46 KB
/
example_test.go
File metadata and controls
113 lines (95 loc) · 2.46 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) 2017-2026 The asrl developers. All rights reserved.
// Project site: https://github.com/gotmc/asrl
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE.txt file for the project.
package asrl_test
import (
"context"
"fmt"
"log"
"github.com/gotmc/asrl"
)
func Example() {
ctx := context.Background()
// Open a serial device using a VISA resource string.
dev, err := asrl.NewDevice(ctx, "ASRL::/dev/tty.usbserial-PX484GRU::9600::8N2::INSTR")
if err != nil {
log.Fatal(err)
}
defer dev.Close()
// Query the instrument identification.
idn, err := dev.Query(ctx, "*IDN?")
if err != nil {
log.Fatal(err)
}
fmt.Println(idn)
// Send a SCPI command.
if err := dev.Command(ctx, "OUTP ON"); err != nil {
log.Fatal(err)
}
}
func ExampleNewDevice() {
ctx := context.Background()
dev, err := asrl.NewDevice(ctx, "ASRL::/dev/tty.usbserial-PX484GRU::9600::8N2::INSTR")
if err != nil {
log.Fatal(err)
}
defer dev.Close()
fmt.Println("opened serial device")
}
func ExampleNewDevice_withOptions() {
ctx := context.Background()
dev, err := asrl.NewDevice(ctx,
"ASRL::/dev/tty.usbserial-PX8X3YR6::9600::8N2::INSTR",
asrl.WithHWHandshaking(true),
asrl.WithDelayTime(100),
)
if err != nil {
log.Fatal(err)
}
defer dev.Close()
fmt.Println("opened serial device with hardware handshaking")
}
func ExampleNewVisaResource() {
v, err := asrl.NewVisaResource("ASRL::/dev/tty.usbserial-PX484GRU::9600::8N2::INSTR")
if err != nil {
log.Fatal(err)
}
fmt.Println(v.InterfaceType())
fmt.Println(v.Address())
fmt.Println(v.Baud())
// Output:
// ASRL
// /dev/tty.usbserial-PX484GRU
// 9600
}
func ExampleDevice_Command() {
ctx := context.Background()
dev, err := asrl.NewDevice(ctx, "ASRL::/dev/tty.usbserial-PX484GRU::9600::8N2::INSTR")
if err != nil {
log.Fatal(err)
}
defer dev.Close()
// Command sends a SCPI command with an auto-appended endmark character.
if err := dev.Command(ctx, "*RST"); err != nil {
log.Fatal(err)
}
// Command supports fmt.Sprintf-style formatting.
if err := dev.Command(ctx, "VOLT %f", 5.0); err != nil {
log.Fatal(err)
}
}
func ExampleDevice_Query() {
ctx := context.Background()
dev, err := asrl.NewDevice(ctx, "ASRL::/dev/tty.usbserial-PX484GRU::9600::8N2::INSTR")
if err != nil {
log.Fatal(err)
}
defer dev.Close()
// Query sends a command and reads the response.
idn, err := dev.Query(ctx, "*IDN?")
if err != nil {
log.Fatal(err)
}
fmt.Println(idn)
}