C++ Instrument Catalog
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 
14 int 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 
MTL::Instrument::PT2026Types::kStatusEvent
@ kStatusEvent
Definition: PT2026Types.h:30
MTL::Instrument::CPT2026Instrument::ParmSearchSet
bool ParmSearchSet(const sSearch< uParm > &rSearch)
Configure the search operation, including detection level, frequency steps and the search bounds spec...
Definition: PT2026.cpp:698
MTL::Instrument::CPT2026Instrument::~CPT2026Instrument
virtual ~CPT2026Instrument()
Definition: PT2026.cpp:249
MTL::Instrument::PT2026Types::sStatusRegister
Definition: PT2026Types.h:34
MTL::Instrument::CResourceList
Definition: VISAInstrumentTypes.h:23
MTL::Instrument::eEventType::ServiceRequest
@ ServiceRequest
MTL::Instrument::PT2026Types::sInputTrigger::Source
eInputTriggerSource Source
Definition: PT2026Types.h:251
MTL::Instrument::PT2026Types::sSearch::HighLimit_UNITS
ParmType< F64 > HighLimit_UNITS
Definition: PT2026Types.h:339
MTL::Instrument::PT2026Types::sArbitraryMeasurements::Channel
bool Channel
Definition: PT2026Types.h:621
MTL::SCPI::tChannelList
std::vector< tChannel > tChannelList
SCPI channel list
Definition: SCPIParsing.h:26
MTL::Instrument::CPT2026Instrument::ParmSearchSetLimits
bool ParmSearchSetLimits(F64 LowLimit_UNITS, F64 HighLimit_UNITS)
Set the search bounds in the selected unit.
Definition: PT2026.cpp:850
MTL::Instrument::CVISAResourceManager::FindResources
bool FindResources(CResourceList &rList, std::string Filter="?*")
Definition: VISAInstrument.cpp:121
MTL::Instrument::CVISAInstrument::EnableEvent
bool EnableEvent(eEventType Type, eEventMechanism Mechanism)
Definition: VISAInstrument.cpp:612
MTL::Instrument::CPT2026Instrument::SetFormat
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:3163
MTL::Instrument::CVISAInstrument::WaitOnAllEvents
bool WaitOnAllEvents(ViUInt32 Timeout)
Definition: VISAInstrument.cpp:660
MTL::Instrument::PT2026Types::uStatusByte::sStatusByte::OSB
U8 OSB
Definition: PT2026Types.h:54
MTL::Instrument::PT2026Types
Definition: PT2026TypeConversions.h:13
MTL::Instrument::PT2026Types::sMeasure::Mode
eMeasureMode Mode
Definition: PT2026Types.h:290
MTL::Instrument::CPT2026Instrument::StandardStatusSetEnableRegister
bool StandardStatusSetEnableRegister(eStandardStatusRegister Set, U16 DisableMask, U16 EnableMask)
Definition: PT2026.cpp:3466
MTL::Instrument::PT2026Types::kInputTrigSrceImmediate
@ kInputTrigSrceImmediate
Definition: PT2026Types.h:239
MTL::Instrument::PT2026Types::kT
@ kT
Definition: PT2026Types.h:157
MTL::Instrument::PT2026Types::sSearch
Definition: PT2026Types.h:333
MTL::Instrument::CPT2026Instrument::ParmUnitsSet
bool ParmUnitsSet(eUnits Units)
Definition: PT2026.cpp:2134
MTL::Instrument::CPT2026Instrument::StatusGet
bool StatusGet(sStatusRegister Reg, U16 &rStatus)
Definition: PT2026.cpp:3196
MTL::Instrument::PT2026Types::eUnits
eUnits
Definition: PT2026Types.h:156
MTL::Instrument::PT2026Types::sArbitraryMeasurements::Uniformity
bool Uniformity
Definition: PT2026Types.h:620
MTL::Instrument::PT2026Types::kMeasRepeat
@ kMeasRepeat
Definition: PT2026Types.h:214
MTL::Instrument::PT2026Types::kSearchAuto
@ kSearchAuto
Definition: PT2026Types.h:328
PT2026_USB_RESOURCE_PATTERN
#define PT2026_USB_RESOURCE_PATTERN
Definition: Example_ConnectConfigureFetchMeasurements.cpp:11
MTL::Instrument::CPT2026Instrument
PT2026 instrument class.
Definition: PT2026.h:28
MTL::Instrument::CPT2026Instrument::Initiate
bool Initiate(bool Continuous=false)
Starts the search operation and then immediately after NMR resonance detection proceed to NMR measure...
Definition: PT2026.cpp:2673
MTL::Instrument::PT2026Types::kServiceRequestRegister
@ kServiceRequestRegister
Definition: PT2026Types.h:20
MTL::Instrument::PT2026Types::sAdvancedMeasurements::Basic
std::vector< sBasicMeasurement > Basic
Definition: PT2026Types.h:567
MTL::Instrument::PT2026Types::uStatusByte::StatusByte
struct MTL::Instrument::PT2026Types::uStatusByte::sStatusByte StatusByte
MTL::Instrument::PT2026Types::sMeasurementAveraging
Definition: PT2026Types.h:217
main
int main(void)
Definition: Example_ConnectConfigureFetchMeasurements.cpp:14
MTL::Instrument::PT2026Types::sInputTrigger::Count
ParmType< U8 > Count
Definition: PT2026Types.h:254
MTL::Instrument::PT2026Types::sArbitraryMeasurements::Flux
bool Flux
Definition: PT2026Types.h:618
MTL::Instrument::CPT2026Instrument::ChannelsSelect
bool ChannelsSelect(const MTL::SCPI::tChannelList &rChanList)
Definition: PT2026.cpp:479
MTL::Instrument::CPT2026Instrument::ParmMeasureGet
bool ParmMeasureGet(sMeasure< uParm > &rMeasure)
Definition: PT2026.cpp:924
MTL::Instrument::PT2026Types::kComFormatInteger
@ kComFormatInteger
Definition: PT2026Types.h:357
MTL::Instrument::eEventMechanism::Queue
@ Queue
MTL::Instrument::CPT2026Instrument::Connect
bool Connect(U32 InitialTimeout_ms)
Definition: PT2026.cpp:274
MTL::Instrument::PT2026Types::uOPERation::sOPERation::SWEeping
U16 SWEeping
Definition: PT2026Types.h:103
MTL::Instrument::PT2026Types::kStatusOperation
@ kStatusOperation
Definition: PT2026Types.h:24
MTL::Instrument::PT2026Types::uOPERation::sOPERation::NewMeasAvail
U16 NewMeasAvail
Definition: PT2026Types.h:108
MTL::Instrument::PT2026Types::kStatusCondition
@ kStatusCondition
Definition: PT2026Types.h:31
MTL::Instrument::PT2026Types::sArbitraryMeasurements::Deviation
bool Deviation
Definition: PT2026Types.h:619
MTL::Instrument::PT2026Types::sMeasurementAveraging::NoPoints
ParmType< U32 > NoPoints
Definition: PT2026Types.h:219
MTL::Instrument::PT2026Types::sArbitraryMeasurements
Definition: PT2026Types.h:617
MTL::Instrument::CPT2026Instrument::UnlockInterface
bool UnlockInterface()
Definition: PT2026.cpp:332
MTL::Instrument::CPT2026Instrument::GetSearchProgress
bool GetSearchProgress(U8 &rSPR, F64 &rHallMeasurement_UNITS)
Definition: PT2026.cpp:3124
MTL::Instrument::PT2026Types::sAllMeasurements
Definition: PT2026Types.h:633
MTL::Instrument::PT2026Types::uOPERation
Definition: PT2026Types.h:97
MTL::Instrument::CPT2026Instrument::ChannelsGetList
bool ChannelsGetList(MTL::SCPI::tChannelList &rChanList)
Definition: PT2026.cpp:353
MTL::Instrument::PT2026Types::sMeasurementAveraging::Type
eMeasurementAveragingType Type
Definition: PT2026Types.h:218
MTL::Instrument::PT2026Types::uOPERation::RawOPER
U16 RawOPER
Definition: PT2026Types.h:98
MTL::Instrument::PT2026Types::uOPERation::OPERation
struct MTL::Instrument::PT2026Types::uOPERation::sOPERation OPERation
MTL::Instrument::CPT2026Instrument::LockInterface
bool LockInterface()
Definition: PT2026.cpp:312
MTL::Instrument::CPT2026Instrument::ParmTriggerInputSet
bool ParmTriggerInputSet(const sInputTrigger< uParm > &rInputTrig)
Definition: PT2026.cpp:1380
MTL::Instrument::PT2026Types::uStatusByte
Definition: PT2026Types.h:44
MTL::Instrument::CPT2026Instrument::ParmAveragingMeasurementSet
bool ParmAveragingMeasurementSet(const sMeasurementAveraging< uParm > &rMeasAvg)
Definition: PT2026.cpp:1218
MTL::Instrument::CPT2026Instrument::MeasurementsGet
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:2711
MTL::Instrument::PT2026Types::sMeasure
Definition: PT2026Types.h:289
MTL::Instrument::PT2026Types::sAllMeasurements::AdvMeas
sAdvancedMeasurements AdvMeas
Definition: PT2026Types.h:634
VISA_TIMEOUT_MS
#define VISA_TIMEOUT_MS
Definition: Example_ConnectConfigureFetchMeasurements.cpp:12
MTL::Instrument::CPT2026Instrument::Abort
bool Abort(bool CancelContinuous=true)
Abort the current operation (Search or measure). Once the operation is aborted, the instrument will b...
Definition: PT2026.cpp:2682
MTL::Instrument::PT2026Types::sSearch::Mode
eSearchMode Mode
Definition: PT2026Types.h:334
MTL::Instrument::PT2026Types::sArbitraryMeasurements::Status
bool Status
Definition: PT2026Types.h:623
MTL::Instrument::PT2026Types::sSearch::LowLimit_UNITS
ParmType< F64 > LowLimit_UNITS
Definition: PT2026Types.h:338
MTL::Instrument::CPT2026Instrument::ParmSearchGet
bool ParmSearchGet(sSearch< uParm > &rSearch)
Definition: PT2026.cpp:728
MTL::Instrument::CPT2026Instrument::ParmMeasureSet
bool ParmMeasureSet(const sMeasure< uParm > &rMeasure)
Definition: PT2026.cpp:895
MTL::Instrument::PT2026Types::uStatusByte::RawSTB
U16 RawSTB
Definition: PT2026Types.h:45
MTL::Instrument::PT2026Types::kMeasureAuto
@ kMeasureAuto
Definition: PT2026Types.h:279
MTL::Instrument::CPT2026Instrument::StatusSetEnableRegister
bool StatusSetEnableRegister(eStatusRegisterSet Set, U16 DisableMask, U16 EnableMask)
Definition: PT2026.cpp:3332
MTL::Instrument::PT2026Types::sArbitraryMeasurements::Timestamp
bool Timestamp
Definition: PT2026Types.h:622
MTL::Instrument::CVISAResourceManager
VISA Resource Manager class.
Definition: VISAInstrument.h:35
MTL::Instrument::PT2026Types::sInputTrigger
Definition: PT2026Types.h:250
MTL::Instrument::CVISAResourceManager::Initialize
bool Initialize()
Definition: VISAInstrument.cpp:82