-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJVisaStatus.java
More file actions
187 lines (168 loc) · 6.4 KB
/
JVisaStatus.java
File metadata and controls
187 lines (168 loc) · 6.4 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* @license
Copyright 2014 Günter Fuchs
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.gpib;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import com.sun.jna.NativeLong;
import java.util.logging.Logger;
import java.util.logging.Level;
import visa.VisaLibrary;
import visatype.VisatypeLibrary;
/**
* This class provides routines related to ViStatus declared in visa.h.
* @author Günter Fuchs ([email protected])
*/
public class JVisaStatus {
/** VISA status as callback function. */
public VisaLibrary.ViStatus visaStatus;
/** VISA status as long. visa.h declares ViStatus as a function pointer. */
public long visaStatusLong;
/** description of VISA status obtained by calling viStatusDesc */
private String visaStatusString;
/**
* This method gets the description of the VISA status returned last.
* @return description of VISA status
*/
public String getVisaStatus() {
return visaStatusString;
}
/**
* return value for success
* Classes which use this class return only success or error. This way
* such classes do not need to import VISA libraries.
*/
public static final long VISA_JAVA_SUCCESS = VisatypeLibrary.VI_SUCCESS;
/**
* return value for error
* Classes which use this class return only success or error. This way
* such classes do not need to import VISA libraries.
*/
public static final long VISA_JAVA_ERROR = 0x7FFFFFFF;
/** size of response buffer */
int bufferSize;
/** handle of resource manager */
long resourceManagerHandle;
/** encoding string */
String encoding;
/** Place this logger in the logger hierarchy below the JVisa logger. */
private final static Logger LOGGER =
Logger.getLogger(String.format("%s.%s", JVisa.class.getName(), JVisaStatus.class.getSimpleName()));
/**
* constructor
* @param bufferSize size of receiving buffer
* @param encoding buffer responseEncoding
*/
public JVisaStatus(int bufferSize, String encoding) {
try {
visaStatus = null;
visaStatusLong = VisatypeLibrary.VI_SUCCESS;
visaStatusString = "OK";
this.bufferSize = bufferSize;
this.encoding = encoding;
}
catch(SecurityException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
}
}
/**
* This method converts the VISA status from a JNA Callback interface to a long.
* @param visaStatus return interface of VISA method.
* @return visatStatus as long
*/
public long setStatus(VisaLibrary.ViStatus visaStatus) {
try {
this.visaStatus = visaStatus;
if (visaStatus == null) {
// JNA VISA functions return null for success because
// VisaLibrary.ViStatus is a function pointer to an error handler.
visaStatusLong = VisatypeLibrary.VI_SUCCESS;
printStatusDescription();
return visaStatusLong;
}
// Getting the function address by parsing what visaStatus.toString()
// returns is a kludge.
String interfaceString = visaStatus.toString();
int posAt = interfaceString.indexOf('@');
String callbackAddress = interfaceString.substring(posAt + 1, posAt + 11);
visaStatusLong = Long.decode(callbackAddress);
printStatusDescription();
return visaStatusLong == VisatypeLibrary.VI_SUCCESS ? visaStatusLong : VISA_JAVA_ERROR;
}
catch(NumberFormatException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
return VISA_JAVA_ERROR;
}
}
/**
* This method converts a VISA status to a descriptive string.
* @param visaResourceManagerHandle handle of VISA resource manager
* @return status description
*/
public String getStatusDescription(long visaResourceManagerHandle) {
try {
String statusString = String.format("VISA status 0x%08X", visaStatusLong);
if (visaResourceManagerHandle == 0) {
return String.format("%s: No description is available because no resource "
+ "manager session is open.", statusString);
}
ByteBuffer pStatusDesc = ByteBuffer.allocate(bufferSize);
VisaLibrary.INSTANCE.viStatusDesc(new NativeLong(visaResourceManagerHandle), visaStatus, pStatusDesc);
byte[] statusArray = pStatusDesc.array();
int stringLength;
for (stringLength = 0; stringLength < statusArray.length; stringLength++) {
if (statusArray[stringLength] == 0)
break;
}
return String.format("%s: %s", statusString, new String(pStatusDesc.array(), 0, stringLength, encoding));
}
catch (UnsupportedEncodingException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
return null;
}
}
/**
* This method converts a VISA status to a descriptive string.
* @return status string
*/
public String getStatusDescription() {
return getStatusDescription(this.resourceManagerHandle);
}
/**
* This method prints the Visa status in hex and if it not null, also its description.
* @param visaResourceManagerHandle handle of VISA resource manager
*/
protected void printStatusDescription(long visaResourceManagerHandle) {
try {
String status = String.format("viStatus = 0x%08X", visaStatusLong);
// Log SEVERE if there was a Visa error.
LOGGER.log(visaStatusLong == VisatypeLibrary.VI_SUCCESS ? Level.FINE : Level.SEVERE, status);
if (visaResourceManagerHandle == 0) {
return;
}
visaStatusString = getStatusDescription(visaResourceManagerHandle);
if (visaStatusLong != VisatypeLibrary.VI_SUCCESS) {
LOGGER.severe(visaStatusString);
}
}
catch (Exception e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
}
}
/**
* This method prints Visa status and its description.
*/
protected void printStatusDescription() {
printStatusDescription(this.resourceManagerHandle);
}
}