THM1176InstrumentDriver  1.0
C++ API for Metrolab THM1176
THM1176MeasureImmediateTiming.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 Metrolab Technology S.A., Geneva, Switzerland (www.metrolab.com)
2 // See the included file LICENSE.txt for the licensing conditions.
3 
7 //
8 // THM1176MeasureImmediateTiming.cpp
9 // THM1176Test
10 // Purpose:
11 // Measure the time/measurement of the THM1176 in Immediate trigger mode,
12 // as a function of block size and averaging count.
13 //
14 // Created by Philip on 18.01.18.
15 // Copyright © 2018 Metrolab Technology SA. All rights reserved.
16 //
17 
18 #include <stdio.h>
19 
20 #include "THM1176.h"
21 #include "VISAInstrument.h"
22 #include "Exception.h"
23 #include "Helpers.h"
24 
25 #define THM1176_VISA_RESOURCE_PATTERN "USB[0-9]*::0x1BFA::0x0498::[0-9]+::INSTR"
26 #define THM1176_CONNECT_TIMEOUT 5000 // ms
27 
28 #define BLOCK_SIZE_MIN 256
29 #define BLOCK_SIZE_MAX 4096
30 #define BLOCK_SIZE_INC 256
31 
32 #define AVERAGING_COUNT_MIN 100
33 #define AVERAGING_COUNT_MAX 1000
34 #define AVERAGING_COUNT_INC 100
35 
36 
37 using namespace MTL;
38 using namespace MTL::Instrument;
39 using namespace MTL::Instrument::THM1176Types;
40 
42 {
43  // Create the command.
44  std::string l_Command;
45  l_Command += "*RST"; // Reset to set counts = 1, trigger source = Immediate
46  l_Command += ";:INIT;:FETC:TIM?"; // Perform one measurement and fetch the initial timestamp
47  l_Command += ";:TRIG:COUN " + std::to_string(BlockSize); // Set the trigger count as desired
48  l_Command += ";:AVER:COUN " + std::to_string(AveragingCount); // Set the averaging count as desired
49  l_Command += ";:INIT;:FETC:TIM?"; // Perform the measurements and fetch the final timestamp
50 
51  // Execute the command.
52  CSCPIBuffer l_ReadBuffer(65535);
53  if (!pTHM1176->WriteAndRead(l_Command, l_ReadBuffer))
54  {
55  CErrorList l_ErrorList = pTHM1176->CurrentErrorList();
56  for (auto l_pError = l_ErrorList.begin(); l_pError < l_ErrorList.end(); l_pError++)
57  std::cerr << "Error: " << l_pError->Code << "; " << l_pError->Description << std::endl;
59  }
60 
61  // Parse the results.
62  CSCPIBufferParser l_BP(l_ReadBuffer.begin(), l_ReadBuffer.end());
63  CSCPIBufferParser::tTokens l_Tokens = l_BP.Tokenize();
64  if (l_Tokens.size() != 2)
66 
67  // Retrieve the timestamps and return the time for the requested measurement.
68  std::string l_Token0 = std::string(l_Tokens[0].begin, l_Tokens[0].end);
69  std::string l_Token1 = std::string(l_Tokens[1].begin, l_Tokens[1].end);
70  U64 l_T0 = std::stoull(l_Token0, 0, 0);
71  U64 l_T1 = std::stoull(l_Token1, 0, 0);
72  return (l_T1 - l_T0);
73 }
74 
75 int main(void)
76 {
77  CVISAResourceManager * l_pResourceManager = NULL;
79 
80  try
81  {
82  // Create and initialize the VISA resource manager.
83  l_pResourceManager = new CVISAResourceManager;
84  if (NULL == l_pResourceManager || !l_pResourceManager->Initialize())
85  throw MTL::CException<CTHM1176Instrument<CVISAInstrument, CVISAResourceManager>>("Cannot initialize VISA Resource Manager", MTL__LOCATION__);
86 
87  // Get the resource list.
88  CResourceList l_InstrumentList;
89  l_pResourceManager->FindResources(l_InstrumentList, THM1176_VISA_RESOURCE_PATTERN);
90  if (l_InstrumentList.empty())
92 
93  // Create a new THM1176 object.
94  l_pTHM1176 = new CTHM1176Instrument<CVISAInstrument, CVISAResourceManager>(*l_pResourceManager, l_InstrumentList[0]);
95  if (NULL == l_pTHM1176)
97 
98  // Connect to the THM1176.
99  if (!l_pTHM1176->Connect(THM1176_CONNECT_TIMEOUT))
101 
102  // Loop over block size and averaging count.
103  for (U16 l_BlockSize = BLOCK_SIZE_MIN; l_BlockSize <= BLOCK_SIZE_MAX; l_BlockSize += BLOCK_SIZE_INC)
104  {
105  for (U16 l_AveragingCount = AVERAGING_COUNT_MIN; l_AveragingCount <= AVERAGING_COUNT_MAX; l_AveragingCount += AVERAGING_COUNT_INC)
106  std::cout << l_BlockSize << "\t" << l_AveragingCount << "\t" <<
107  MeasureImmediateTiming(l_pTHM1176, l_BlockSize, l_AveragingCount) << std::endl;
108  }
109 
110  // Shut everything down.
111  l_pTHM1176->Disconnect();
112  delete l_pTHM1176;
113  delete l_pResourceManager;
114  }
116  {
117  std::cerr << rE.what() << std::endl;
118  if (l_pTHM1176)
119  {
120  l_pTHM1176->Disconnect();
121  delete l_pTHM1176;
122  }
123  if (l_pResourceManager)
124  delete l_pResourceManager;
125  return -1;
126  }
127  return 0;
128 }
BLOCK_SIZE_MAX
#define BLOCK_SIZE_MAX
Definition: THM1176MeasureImmediateTiming.cpp:29
MTL::Instrument::CResourceList
List of VISA resource names.
Definition: IEEE488InstrumentTypes.h:26
MTL::Instrument::CVISAResourceManager::FindResources
virtual bool FindResources(CResourceList &rList, std::string Filter="?*")
Find VISA resources.
Definition: VISAInstrument.cpp:144
MTL::Instrument::CSCPIBuffer::end
std::vector< MTL_INSTRUMENT_BUFFER_TYPE >::iterator end()
Return an iterator to the end of the buffer.
Definition: SCPIInstrumentBuffer.h:138
U64
unsigned long long U64
64-bit unsigned integer.
Definition: OSDefines.h:33
AVERAGING_COUNT_MIN
#define AVERAGING_COUNT_MIN
Definition: THM1176MeasureImmediateTiming.cpp:32
AVERAGING_COUNT_INC
#define AVERAGING_COUNT_INC
Definition: THM1176MeasureImmediateTiming.cpp:34
MTL::Instrument::CTHM1176Instrument
THM1176 instrument class.
Definition: THM1176.h:99
THM1176_VISA_RESOURCE_PATTERN
#define THM1176_VISA_RESOURCE_PATTERN
Definition: THM1176MeasureImmediateTiming.cpp:25
MTL::Instrument::CSCPIBufferParser
SCPI buffer parser.
Definition: SCPIInstrumentBuffer.h:217
MTL::Instrument::CSCPIBuffer::begin
std::vector< MTL_INSTRUMENT_BUFFER_TYPE >::iterator begin()
Return an iterator to the beginning of the buffer.
Definition: SCPIInstrumentBuffer.h:122
MTL::Instrument::CSCPIBufferParser::tTokens
std::vector< sToken > tTokens
List of tokens.
Definition: SCPIInstrumentBuffer.h:233
Helpers.h
Collection of utility macros for error messages.
MTL::Instrument::CTHM1176Instrument::Disconnect
void Disconnect()
Close the connection to the instrument.
Definition: THM1176.cpp:879
main
int main(void)
Definition: THM1176MeasureImmediateTiming.cpp:75
MTL
Definition: THM1176.h:74
MTL::Instrument
Definition: THM1176.h:75
MTL::Instrument::THM1176Types
Definition: THM1176TypeConversions.h:20
MTL::Instrument::CTHM1176Instrument::WriteAndRead
bool WriteAndRead(const std::string &rWriteStr, CSCPIBuffer &rReadBuffer)
Write an arbitrary command and read the result.
Definition: THM1176.cpp:296
MTL::Instrument::CSCPIBufferParser::Tokenize
const tTokens Tokenize(const char Separator=';', size_t Offset=0)
Split the buffer into tokens.
Definition: SCPIInstrumentBuffer.h:258
MTL::Instrument::CTHM1176Instrument::CurrentErrorList
const CErrorList & CurrentErrorList()
Fetch current error list.
Definition: THM1176.cpp:794
MTL::Instrument::CTHM1176Instrument::Connect
bool Connect(U32 InitialTimeout, bool Exclusive=true)
Open the connection to the instrument.
Definition: THM1176.cpp:812
MeasureImmediateTiming
U64 MeasureImmediateTiming(CTHM1176Instrument< CVISAInstrument, CVISAResourceManager > *pTHM1176, U16 BlockSize, U16 AveragingCount)
Definition: THM1176MeasureImmediateTiming.cpp:41
MTL::CException
Exception to be thrown.
Definition: Exception.h:17
AVERAGING_COUNT_MAX
#define AVERAGING_COUNT_MAX
Definition: THM1176MeasureImmediateTiming.cpp:33
VISAInstrument.h
C++ wrapper for NI-VISA: interface definition.
BLOCK_SIZE_INC
#define BLOCK_SIZE_INC
Definition: THM1176MeasureImmediateTiming.cpp:30
MTL::Instrument::THM1176Types::CErrorList
List of errors returned by the instrument.
Definition: THM1176Types.h:232
U16
unsigned short U16
16-bit unsigned integer.
Definition: OSDefines.h:31
THM1176.h
Interface definition for C++ API for Metrolab THM1176/TFM1186.
MTL::Instrument::CSCPIBuffer
Instrument Buffer.
Definition: SCPIInstrumentBuffer.h:44
Exception.h
Exception handling utilities.
BLOCK_SIZE_MIN
#define BLOCK_SIZE_MIN
Definition: THM1176MeasureImmediateTiming.cpp:28
MTL::Instrument::CVISAResourceManager
VISA Resource Manager class.
Definition: VISAInstrument.h:40
THM1176_CONNECT_TIMEOUT
#define THM1176_CONNECT_TIMEOUT
Definition: THM1176MeasureImmediateTiming.cpp:26
MTL__LOCATION__
#define MTL__LOCATION__
Definition: Helpers.h:22
MTL::Instrument::CVISAResourceManager::Initialize
virtual bool Initialize()
Initialize the Resource Manager.
Definition: VISAInstrument.cpp:84