-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVisaSession.cpp
204 lines (178 loc) · 5.35 KB
/
VisaSession.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
@file VisaSession.cpp
@copyright (c) Rohde & Schwarz GmbH & Co. KG, Munich
*/
#include "VisaSession.h"
#include <stdarg.h>
VisaSession::VisaSession(ViSession vi)
: m_vi(vi)
{
}
VisaSession::~VisaSession()
{
}
ViUInt32 VisaSession::write(const std::string& msg)
{
ViUInt32 retCnt;
const ViStatus status = viWrite(getVi(), reinterpret_cast<ViConstBuf>(msg.c_str()), static_cast<ViUInt32>(msg.size()), &retCnt);
checkAndThrow(status, "viWrite failed");
return retCnt;
}
std::string VisaSession::read(uint16_t num=0)
{
static const ViUInt32 buflen = 1024;
std::string result;
ViByte buf[buflen];
ViUInt32 retCnt;
ViStatus status = VI_SUCCESS_MAX_CNT;
// while (status == VI_SUCCESS_MAX_CNT)
// {
if (num!=0)
{
status = viRead(getVi(), buf, num, &retCnt);
}
else
{
status = viRead(getVi(), buf, buflen, &retCnt);
}
//checkAndThrow(status, "viRead failed");
result.append(reinterpret_cast<char*>(buf), retCnt);
// }
return result;
}
void VisaSession::Printf(const std::string writeFmt, ...)
{
va_list args;
va_start(args, writeFmt);
const ViStatus status = viVPrintf(getVi(), writeFmt.c_str(), args);
va_end(args);
checkAndThrow(status, "viVPrintf failed");
}
void VisaSession::Scanf(const std::string readFmt, ...)
{
va_list args;
va_start(args, readFmt);
const ViStatus status = viVScanf(getVi(), readFmt.c_str(), args);
va_end(args);
checkAndThrow(status, "viVScanf failed");
}
void VisaSession::Queryf(const std::string& writeFmt, const std::string readFmt, ...)
{
va_list args;
va_start(args, readFmt);
const ViStatus status = viVQueryf(getVi(), writeFmt.c_str(), readFmt.c_str(), args);
va_end(args);
checkAndThrow(status, "viVScanf failed");
}
void VisaSession::enableEvent(Events eventType, EventMechanism mechanism)
{
const ViStatus status = viEnableEvent(getVi(), Event2EventType(eventType), static_cast<ViUInt16>(mechanism), VI_NULL);
checkAndThrow(status, "viEnableEvent failed");
}
void VisaSession::disableEvent(Events eventType, EventMechanism mechanism)
{
const ViStatus status = viDisableEvent(getVi(), Event2EventType(eventType), static_cast<ViUInt16>(mechanism));
checkAndThrow(status, "viDisableEvent failed");
}
void VisaSession::discardEvents(Events eventType, EventMechanism mechanism)
{
const ViStatus status = viDiscardEvents(getVi(), Event2EventType(eventType), static_cast<ViUInt16>(mechanism));
checkAndThrow(status, "viDiscardEvents failed");
}
void VisaSession::installHandler(Events eventType, ViHndlr handler, ViAddr userHandle)
{
const ViStatus status = viInstallHandler(getVi(), Event2EventType(eventType), handler, userHandle);
checkAndThrow(status, "viInstallHandler failed");
}
void VisaSession::uninstallHandler(Events eventType, ViHndlr handler, ViAddr userHandle)
{
const ViStatus status = viUninstallHandler(getVi(), Event2EventType(eventType), handler, userHandle);
checkAndThrow(status, "viUninstallHandler failed");
}
ViUInt16 VisaSession::readSTB()
{
ViUInt16 stb = 0;
ViStatus status = viReadSTB(getVi(), &stb);
checkAndThrow(status, "viReadSTB failed.");
return stb;
}
void VisaSession::setAttribute(ViAttr attrName, ViAttrState attrValue)
{
const ViStatus status = viSetAttribute(getVi(), attrName, attrValue);
checkAndThrow(status, "viSetAttribute failed");
}
ViAttrState VisaSession::getAttribute(ViAttr attrName )
{
ViAttrState attrValue = 0;
const ViStatus status = viGetAttribute(getVi(), attrName, &attrValue);
checkAndThrow(status, "viGetAttribute failed");
return attrValue;
}
void VisaSession::clear()
{
ViStatus status = viClear(getVi());
checkAndThrow(status, "viClear failed.");
}
ViEventType VisaSession::Event2EventType(Events eventType)
{
ViEventType result = 0;
switch (eventType)
{
case Events::IO_COMPLETION:
result = VI_EVENT_IO_COMPLETION;
break;
case Events::EVENT_TRIG:
result = VI_EVENT_TRIG;
break;
case Events::SERVICE_REQ:
result = VI_EVENT_SERVICE_REQ;
break;
case Events::CLEAR:
result = VI_EVENT_CLEAR;
break;
case Events::EXCEPTION:
result = VI_EVENT_EXCEPTION;
break;
case Events::GPIB_CIC:
result = VI_EVENT_GPIB_CIC;
break;
case Events::GPIB_TALK:
result = VI_EVENT_GPIB_TALK;
break;
case Events::GPIB_LISTEN:
result = VI_EVENT_GPIB_LISTEN;
break;
case Events::VXI_VME_SYSFAIL:
result = VI_EVENT_VXI_VME_SYSFAIL;
break;
case Events::VXI_VME_SYSRESET:
result = VI_EVENT_VXI_VME_SYSRESET;
break;
case Events::VXI_SIGP:
result = VI_EVENT_VXI_SIGP;
break;
case Events::VXI_VME_INTR:
result = VI_EVENT_VXI_VME_INTR;
break;
case Events::PXI_INTR:
result = VI_EVENT_PXI_INTR;
break;
case Events::TCPIP_CONNECT:
result = VI_EVENT_TCPIP_CONNECT;
break;
case Events::USB_INTR:
result = VI_EVENT_USB_INTR;
break;
case Events::ALL_EVENTS:
result = VI_ALL_ENABLED_EVENTS;
break;
}
return result;
}
void VisaSession::checkAndThrow(ViStatus status, const std::string& msg)
{
if (status < VI_SUCCESS)
{
throw VisaException(status, msg);
}
}