C++Instrument Catalog MFC 1.2
Metrolab C++InstrumentCatalog for MFC
Loading...
Searching...
No Matches
Example_ConnectConfigureFetchMeasurements.cpp
Go to the documentation of this file.
1#include <conio.h>
2#include <cerrno>
3
4#include <iostream>
5#include <iomanip>
6#include <chrono>
7#include <thread>
8
9#include <PT2026>
10
11#define PT2026_USB_RESOURCE_PATTERN "USB[0-9]*::0x1BFA::0x07EA::[0-9]+::INSTR"
12#define VISA_TIMEOUT_MS 8000
13
14int main(void)
15{
17 MTL::Instrument::CPT2026Instrument* l_pInstrument = NULL;;
18
19 using namespace std::chrono_literals;
20 using namespace MTL::Instrument::PT2026Types;
21
22 // Initialize our resource manager (mandatory).
23 l_RM.Initialize();
24
25 // Try to find a PT2026 or abort when the user hit any key.
26 bool _KeyboardKeyPressed = false;
27 do
28 {
29 // Find a PT2026 resource
32 // Create our instrument interface
33 if (!l_RSL.empty())
34 l_pInstrument = new MTL::Instrument::CPT2026Instrument(l_RM, l_RSL.front());
35
36 _KeyboardKeyPressed = _kbhit();
37
38 if ((NULL == l_pInstrument) && !_KeyboardKeyPressed)
39 std::this_thread::sleep_for(1000ms);
40
41 } while ((NULL == l_pInstrument) && !_KeyboardKeyPressed);
42
43 if (_KeyboardKeyPressed)
44 {
45 std::cout << "Instrument detection aborted by the user." << std::endl;
46 return ECANCELED;
47 }
48
49 // Connect to the PT2026 we found
50 l_pInstrument->Connect(VISA_TIMEOUT_MS);
51
53 // Initialize
55
56 // Make sure that we are the only one actively operating the instrument
57 l_pInstrument->LockInterface();
58 // Ensure that we start in known condition by aborting previously started operations.
59 l_pInstrument->Abort(true);
60 // Define the format in which the measurement shall be returned.
61 l_pInstrument->SetFormat(kComFormatInteger);
62 // Define the unit in which the measurement shall be returned (Tesla)
63 l_pInstrument->ParmUnitsSet(kT);
64
66 // Connect the active probe
68 MTL::SCPI::tChannelList l_ChanList;
69 // Get the list of channels
70 l_pInstrument->ChannelsGetList(l_ChanList);
71 // We assume that we have only one probe connected
72 l_pInstrument->ChannelsSelect(l_ChanList);
73
75 // Set the measurement and search in automatic mode.
77 sSearch<uParm> l_Search;
78
79 // Set the search in fully automated mode.
80 l_pInstrument->ParmSearchGet(l_Search);
81 l_Search.Mode = kSearchAuto;
82 l_pInstrument->ParmSearchSet(l_Search);
83
84 // Set the search limits.
85 // Here, we collect the search limits as defined in the probe.
86 // One could have set a smaller search range to accelerate the search operation.
87 sSearch<sBoundedParm> l_BoundedSearchParameters;
88 l_pInstrument->ParmSearchGet(l_BoundedSearchParameters);
89 l_pInstrument->ParmSearchSetLimits(l_BoundedSearchParameters.LowLimit_UNITS.Min, l_BoundedSearchParameters.HighLimit_UNITS.Max);
90
91 // Set the measurement in fully automated mode.
92 sMeasure<uParm> l_Measure;
93 l_pInstrument->ParmMeasureGet(l_Measure);
94 l_Measure.Mode = kMeasureAuto;
95 l_pInstrument->ParmMeasureSet(l_Measure);
96
98 // Set the measurement averaging
101 l_MeasAvg.Type = kMeasRepeat;
102 // This value gives a 1s measurement rate for a pulse period of 50ms
103 l_MeasAvg.NoPoints = 20;
104 l_pInstrument->ParmAveragingMeasurementSet(l_MeasAvg);
105
107 // Configure the trigger
109 sInputTrigger<uParm> l_InputTrig;
110 l_InputTrig.Source = kInputTrigSrceImmediate;
111 l_InputTrig.Count = 1;
112 l_pInstrument->ParmTriggerInputSet(l_InputTrig);
113
115 // Configure the service requests
116 // and Initiate a measurement
118
119 // The variables to store the measurements
120 sArbitraryMeasurements l_ArbSel;
121 sAllMeasurements l_ArbMeas;
122 eUnits l_ArbUnits;
123 // Set some default values.
124 l_ArbSel.Flux = true;
125 l_ArbSel.Deviation = true;
126 l_ArbSel.Uniformity = true;
127 l_ArbSel.Channel = true;
128 l_ArbSel.Timestamp = true;
129 l_ArbSel.Status = true;
130
131 // Service request readings
133 uStatusByte l_StatusSRQ;
134 l_StatusSRQ.StatusByte.OSB = 1;
135 l_pInstrument->StandardStatusSetEnableRegister(kServiceRequestRegister, 0x00, l_StatusSRQ.RawSTB);
136 uOPERation l_Oper;
137 l_Oper.OPERation.NewMeasAvail = 1;
138 l_pInstrument->StatusSetEnableRegister(kStatusOperation, 0, l_Oper.RawOPER);
139
140 // Start the measurement. This will also force an NMR signal search.
141 //
142 l_pInstrument->Initiate(true);
143
144 // Gather the measurements from the instrument and display them "à la" PT2025.
145 do
146 {
147 // States which status we are interested into.
148 std::vector<sStatusRegister> l_StatusList = { sStatusRegister(kStatusOperation, kStatusCondition), sStatusRegister(kStatusOperation, kStatusEvent) };
149 std::vector<U16> l_StatRes;
150
151 l_pInstrument->StatusGet(l_StatusList, l_StatRes);
152
153 if (l_pInstrument->WaitOnAllEvents(200))
154 {
155 uOPERation l_OpEv = { l_StatRes[1] };
156
157 // A new measurement is available.
158 if (l_OpEv.OPERation.NewMeasAvail)
159 {
160 // Collect the measurement.
161 l_pInstrument->MeasurementsGet(1, l_ArbSel, l_ArbMeas, l_ArbUnits);
162 // Display it "à la" PT2025.
163 std::cout << "L" << std::fixed << std::showpoint << std::setprecision(8) << l_ArbMeas.AdvMeas.Basic[0].Flux_UNITS << "T" << std::endl;
164 }
165 }
166 else
167 {
168 uOPERation l_OperCond = { l_StatRes[0] };
169
170 // We are in search mode.
171 if (l_OperCond.OPERation.SWEeping)
172 {
173 U8 l_SPR;
174 F64 l_HallMeas;
175 l_pInstrument->GetSearchProgress(l_SPR, l_HallMeas);
176 // Compute an equivalent frequency to behave as if we were operating a PT2025. The actual frequency
177 // might be very different as the search operation on the PT2026 doesn't consist in a linear sweep.
178 F64 _CurrentUnlockedFrequency = l_SPR / 100.0 * (l_BoundedSearchParameters.HighLimit_UNITS.Max - l_BoundedSearchParameters.LowLimit_UNITS.Min) + l_BoundedSearchParameters.LowLimit_UNITS.Min;
179 // Display it "à la" PT2025.
180 std::cout << "N" << std::fixed << std::showpoint << std::setprecision(8) << _CurrentUnlockedFrequency << "T" << std::endl;
181 }
182 std::this_thread::sleep_for(200ms);
183
184 }
185 // Leave the loop if any key on the keyboard is hit.
186 } while (!_kbhit());
187
189 // Leave the application
191 l_pInstrument->Abort(true);
192 l_pInstrument->UnlockInterface();
193 l_pInstrument->~CPT2026Instrument();
194}
195
std::vector< tChannel > tChannelList
SCPI channel list
Definition SCPIParsing.h:26
PT2026 instrument class.
Definition PT2026.h:29
bool ParmSearchSet(const sSearch< uParm > &rSearch, F64 Lgc0122_DetectionLevel_V=0.15)
Configure the search operation, including frequency steps and the search bounds specified in the sele...
Definition PT2026.cpp:755
bool ParmMeasureGet(sMeasure< uParm > &rMeasure)
Definition PT2026.cpp:1025
bool MeasurementsGet(U32 NoMeasurements, std::vector< tFlux > &rFlux, eUnits &rUnits)
Returns the NMR magnetic field density measurements, including the unit used. A measurement vector is...
Definition PT2026.cpp:2812
bool ParmSearchGet(sSearch< uParm > &rSearch)
Definition PT2026.cpp:796
bool GetSearchProgress(U8 &rSPR, F32 &rHallMeasurement_UNITS)
Definition PT2026.cpp:3225
bool Abort(bool CancelContinuous=true)
Abort the current operation (Search or measure). Once the operation is aborted, the instrument will b...
Definition PT2026.cpp:2783
bool ParmSearchSetLimits(F64 LowLimit_UNITS, F64 HighLimit_UNITS)
Set the search bounds in the selected unit.
Definition PT2026.cpp:951
bool ChannelsSelect(const MTL::SCPI::tChannelList &rChanList)
Definition PT2026.cpp:524
bool ParmUnitsSet(eUnits Units)
Definition PT2026.cpp:2235
bool SetFormat(eCommunicationFormat Format)
One can request the data in ASCII or binary format. The binary format, which needs extra operation to...
Definition PT2026.cpp:3269
bool ParmTriggerInputSet(const sInputTrigger< uParm > &rInputTrig)
Definition PT2026.cpp:1481
bool StandardStatusSetEnableRegister(eStandardStatusRegister Set, U16 DisableMask, U16 EnableMask)
Definition PT2026.cpp:3571
bool ParmAveragingMeasurementSet(const sMeasurementAveraging< uParm > &rMeasAvg)
Definition PT2026.cpp:1319
bool StatusGet(sStatusRegister Reg, U16 &rStatus)
Definition PT2026.cpp:3302
bool ParmMeasureSet(const sMeasure< uParm > &rMeasure)
Definition PT2026.cpp:996
bool Connect(U32 InitialTimeout_ms)
Definition PT2026.cpp:283
bool ChannelsGetList(MTL::SCPI::tChannelList &rChanList)
Definition PT2026.cpp:398
bool StatusSetEnableRegister(eStatusRegisterSet Set, U16 DisableMask, U16 EnableMask)
Definition PT2026.cpp:3438
bool Initiate(bool Continuous=false)
Starts the search operation and then immediately after NMR resonance detection proceed to NMR measure...
Definition PT2026.cpp:2774
struct MTL::Instrument::PT2026Types::uStatusByte::sStatusByte StatusByte
struct MTL::Instrument::PT2026Types::uOPERation::sOPERation OPERation
VISA Resource Manager class.
bool FindResources(CResourceList &rList, std::string Filter="?*")
bool EnableEvent(eEventType Type, eEventMechanism Mechanism)
bool WaitOnAllEvents(ViUInt32 Timeout)