-
Notifications
You must be signed in to change notification settings - Fork 0
/
asrl.go
151 lines (135 loc) · 4.65 KB
/
asrl.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) 2017-2024 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
import (
"bufio"
"fmt"
"log"
"strings"
"time"
"go.bug.st/serial"
)
// Device models a serial device and implements the ivi.Driver interface.
type Device struct {
EndMark byte
HWHandshaking bool
DelayTime time.Duration
port serial.Port
}
// NewDevice opens a serial Device using the given VISA address resource string.
func NewDevice(address string) (*Device, error) {
v, err := NewVisaResource(address)
if err != nil {
return nil, err
}
log.Printf("Baud rate = %d", v.baud)
log.Printf("Parity = %v", v.parity)
log.Printf("Data bits = %d", v.dataBits)
log.Printf("Stop bits = %v", v.stopBits)
mode := &serial.Mode{
BaudRate: v.baud,
Parity: v.parity,
DataBits: v.dataBits,
StopBits: v.stopBits,
}
port, err := serial.Open(v.address, mode)
if err != nil {
return nil, err
}
return &Device{
port: port,
HWHandshaking: false,
EndMark: '\n',
DelayTime: 70 * time.Millisecond,
}, nil
}
// Write writes the given data to the network connection.
func (d *Device) Write(p []byte) (n int, err error) {
return d.port.Write(p)
}
// Read reads from the network connection into the given byte slice.
func (d *Device) Read(p []byte) (n int, err error) {
return d.port.Read(p)
}
// Close closes the underlying network connection.
func (d *Device) Close() error {
time.Sleep(d.DelayTime)
return d.port.Close()
}
// WriteString writes a string using the underlying network connection.
func (d *Device) WriteString(s string) (n int, err error) {
return d.Write([]byte(s))
}
// Command sends the SCPI/ASCII command to the underlying network connection. A
// newline character is automatically added to the end of the string.
func (d *Device) Command(format string, a ...any) error {
if d.HWHandshaking {
d.napIfDataSetNotReady()
}
cmd := format
if a != nil {
cmd = fmt.Sprintf(format, a...)
}
cmd = strings.TrimSpace(cmd) + string(d.EndMark)
_, err := d.WriteString(cmd)
if err != nil {
return err
}
time.Sleep(d.DelayTime)
return err
}
// Query writes the given string to the underlying network connection and
// returns a string. A newline character is automatically added to the query
// command sent to the instrument.
func (d *Device) Query(cmd string) (string, error) {
err := d.Command(cmd)
if err != nil {
log.Printf("error received from command sent to query for %s", cmd)
return "", err
}
return bufio.NewReader(d.port).ReadString('\n')
}
func isDSR(port serial.Port) bool {
msb, err := port.GetModemStatusBits()
if err != nil {
log.Printf("error getting modem status bits: %s", err)
}
return msb.DSR
}
func (d *Device) napIfDataSetNotReady() {
//------------------------------------------------------------------------//
//
// ___====-_ _-====___
// _--^^^#####// \\#####^^^--_
// _-^##########// ( ) \\##########^-_
// -############// |\^^/| \\############-
// _/############// (@::@) \\############\_
// /#############(( \\// ))#############\
// -###############\\ (oo) //###############-
// -#################\\ / VV \ //#################-
// -###################\\/ \//###################-
// _#/|##########/\######( /\ )######/\##########|\#_
// |/ |#/\#/\#/\/ \#/\##\ | | /##/\#/ \/\#/\#/\#| \|
// ` |/ V V ` V \#\| | | |/#/ V ' V V \| '
// ` ` ` ` / | | | | \ ' ' ' '
// ( | | | | )
// __\ | | | | /__
// (vvv(VVV)(VVV)vvv)
//------------------------------------------------------------------------//
// If I use 40 ms instead of 50 ms for the delay time, the Keysight E3631A DC
// power supply will hang when sending commands/queries. Using 50 ms causes
// the power supply to hang sometimes. I'm currently using 70 ms to be safe.
//------------------------------------------------------------------------//
for !isDSR(d.port) {
// log.Printf("DSR is false, so napping for %s", d.DelayTime)
time.Sleep(d.DelayTime)
}
//------------------------------------------------------------------------//
// Sleep a bit longer once the Data Set Ready is true. Without this, the
// Keysight E3631A DC power supply will sometimes hang when sending
// commands/queries.
time.Sleep(d.DelayTime)
//------------------------------------------------------------------------//
}