-
Notifications
You must be signed in to change notification settings - Fork 5
/
helpers.go
35 lines (32 loc) · 951 Bytes
/
helpers.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
// Copyright (c) 2017-2023 The visa developers. All rights reserved.
// Project site: https://github.com/gotmc/visa
// 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 visa
import (
"fmt"
"regexp"
"strings"
)
func determineInterfaceType(address string) (InterfaceType, error) {
regexString := `^(?P<interfaceType>[A-Za-z]+)(?P<boardIndex>\d*)::` +
`(?P<allElse>.*)$`
re := regexp.MustCompile(regexString)
res := re.FindStringSubmatch(address)
subexpNames := re.SubexpNames()
matchMap := map[string]string{}
for i, n := range res {
matchMap[subexpNames[i]] = string(n)
}
interfaceType := strings.ToUpper(matchMap["interfaceType"])
switch interfaceType {
case "USB":
return USBTMC, nil
case "TCPIP":
return TCPIP, nil
case "ASRL":
return ASRL, nil
default:
return ASRL, fmt.Errorf("interface type unidentifiable in address %s", address)
}
}