Skip to content

Commit 26bf218

Browse files
matthewrankinclaude
andcommitted
Add context.Context parameter to NewDevice
NewDevice now takes a context as its first parameter, consistent with Command, Query, and the rest of the API. Update all call sites in examples and example tests. Add pkgsite docs recipe to Justfile. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 6e772e8 commit 26bf218

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

Justfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ coverage_file := "coverage.out"
1212
loc:
1313
scc --remap-unknown "-*- Justfile -*-":"justfile"
1414

15+
# View documentation in web browser using pkgsite.
16+
[group('general')]
17+
docs:
18+
pkgsite -open .
19+
1520
# Format and vet Go code. Runs before tests.
1621
[group('test')]
1722
check:

asrl.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,14 @@ func WithReadTimeout(t time.Duration) DeviceOption {
6262
}
6363

6464
// NewDevice opens a serial Device using the given VISA address resource string.
65-
// Optional DeviceOption values can be provided to override the default settings
66-
// for EndMark, HWHandshaking, DelayTime, and ReadTimeout.
67-
func NewDevice(address string, opts ...DeviceOption) (*Device, error) {
65+
// The context is checked before opening the serial port. Optional DeviceOption
66+
// values can be provided to override the default settings for EndMark,
67+
// HWHandshaking, DelayTime, and ReadTimeout.
68+
func NewDevice(ctx context.Context, address string, opts ...DeviceOption) (*Device, error) {
69+
if err := ctx.Err(); err != nil {
70+
return nil, err
71+
}
72+
6873
v, err := NewVisaResource(address)
6974
if err != nil {
7075
return nil, err

example_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ import (
1414
)
1515

1616
func Example() {
17+
ctx := context.Background()
18+
1719
// Open a serial device using a VISA resource string.
18-
dev, err := asrl.NewDevice("ASRL::/dev/tty.usbserial-PX484GRU::9600::8N2::INSTR")
20+
dev, err := asrl.NewDevice(ctx, "ASRL::/dev/tty.usbserial-PX484GRU::9600::8N2::INSTR")
1921
if err != nil {
2022
log.Fatal(err)
2123
}
2224
defer func() { _ = dev.Close() }()
2325

24-
ctx := context.Background()
25-
2626
// Query the instrument identification.
2727
idn, err := dev.Query(ctx, "*IDN?")
2828
if err != nil {
@@ -37,8 +37,10 @@ func Example() {
3737
}
3838

3939
func Example_withOptions() {
40+
ctx := context.Background()
41+
4042
// Open a device with functional options.
41-
dev, err := asrl.NewDevice(
43+
dev, err := asrl.NewDevice(ctx,
4244
"ASRL::/dev/tty.usbserial-PX8X3YR6::9600::8N2::INSTR",
4345
asrl.WithHWHandshaking(true),
4446
)
@@ -47,8 +49,6 @@ func Example_withOptions() {
4749
}
4850
defer func() { _ = dev.Close() }()
4951

50-
ctx := context.Background()
51-
5252
// With hardware handshaking enabled, Command polls DSR before writing.
5353
if err := dev.Command(ctx, "SYST:REM"); err != nil {
5454
log.Fatal(err)

examples/keysight/e3631a/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ func main() {
5353
// Open virtual comm port.
5454
address := fmt.Sprintf("ASRL::%s::9600::8N2::INSTR", serialPort)
5555
log.Printf("VISA Address = %s", address)
56-
dev, err := asrl.NewDevice(address, asrl.WithHWHandshaking(true))
56+
ctx := context.Background()
57+
dev, err := asrl.NewDevice(ctx, address, asrl.WithHWHandshaking(true))
5758
if err != nil {
5859
log.Fatal(err)
5960
}
@@ -62,7 +63,6 @@ func main() {
6263
log.Printf("error closing device: %v", err)
6364
}
6465
}()
65-
ctx := context.Background()
6666

6767
// Query the identification of the function generator.
6868
idn, err := dev.Query(ctx, "*IDN?")

examples/srs/ds345/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ func main() {
4242
// Open virtual comm port.
4343
address := fmt.Sprintf("ASRL::%s::%d::8N2::INSTR", serialPort, baudRate)
4444
log.Printf("VISA Address = %s", address)
45-
dev, err := asrl.NewDevice(address)
45+
ctx := context.Background()
46+
dev, err := asrl.NewDevice(ctx, address)
4647
if err != nil {
4748
log.Fatal(err)
4849
}
@@ -52,8 +53,6 @@ func main() {
5253
}
5354
}()
5455

55-
ctx := context.Background()
56-
5756
// Query the identification of the function generator.
5857
idn, err := dev.Query(ctx, "*idn?")
5958
if err != nil && !errors.Is(err, io.EOF) {

0 commit comments

Comments
 (0)