-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvisa.go
100 lines (86 loc) · 2.65 KB
/
visa.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
// Copyright (c) 2015-2024 The usbtmc developers. All rights reserved.
// Project site: https://github.com/gotmc/usbtmc
// 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 usbtmc
import (
"errors"
"regexp"
"strconv"
"strings"
)
// VisaResource represents a VISA enabled piece of test equipment.
type VisaResource struct {
resourceString string
interfaceType string
boardIndex int
manufacturerID int
modelCode int
serialNumber string
interfaceIndex int
resourceClass string
}
// NewVisaResource creates a new VisaResource using the given VISA resourceString.
func NewVisaResource(resourceString string) (*VisaResource, error) {
visa := &VisaResource{
resourceString: resourceString,
interfaceType: "",
boardIndex: 0,
manufacturerID: 0,
modelCode: 0,
serialNumber: "",
interfaceIndex: 0,
resourceClass: "",
}
regString := `^(?P<interfaceType>[A-Za-z]+)(?P<boardIndex>\d*)::` +
`(?P<manufacturerID>[^\s:]+)::` +
`(?P<modelCode>[^\s:]+)` +
`(::(?P<serialNumber>[^\s:]+))?` +
`(::(?P<interfaceNumber>[^\s:]+))?` +
`::(?P<resourceClass>[^\s:]+)$`
re := regexp.MustCompile(regString)
res := re.FindStringSubmatch(resourceString)
subexpNames := re.SubexpNames()
matchMap := map[string]string{}
for i, n := range res {
matchMap[subexpNames[i]] = string(n)
}
if strings.ToUpper(matchMap["interfaceType"]) != "USB" {
return visa, errors.New("visa: interface type was not usb")
}
visa.interfaceType = "USB"
if matchMap["boardIndex"] != "" {
boardIndex, err := strconv.ParseUint(matchMap["boardIndex"], 0, 16)
if err != nil {
return visa, errors.New("visa: boardIndex error")
}
visa.boardIndex = int(boardIndex)
}
if matchMap["manufacturerID"] != "" {
manufacturerID, err := strconv.ParseUint(matchMap["manufacturerID"], 0, 16)
if err != nil {
return visa, errors.New("visa: manufacturerID error")
}
visa.manufacturerID = int(manufacturerID)
}
if matchMap["modelCode"] != "" {
modelCode, err := strconv.ParseUint(matchMap["modelCode"], 0, 16)
if err != nil {
return visa, errors.New("visa: modelCode error")
}
visa.modelCode = int(modelCode)
}
if matchMap["interfaceNumber"] != "" {
interfaceNumber, err := strconv.ParseUint(matchMap["interfaceNumber"], 0, 16)
if err != nil {
return visa, errors.New("visa: interface number error")
}
visa.interfaceIndex = int(interfaceNumber)
}
visa.serialNumber = matchMap["serialNumber"]
if strings.ToUpper(matchMap["resourceClass"]) != "INSTR" {
return visa, errors.New("visa: resource class was not instr")
}
visa.resourceClass = "INSTR"
return visa, nil
}