THM1176InstrumentManager 1.1
Qt Object abstraction for Metrolab THM1176
Loading...
Searching...
No Matches
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
37using namespace MTL;
38using namespace MTL::Instrument;
39using 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
75int 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())
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}
Exception handling utilities.
Collection of utility macros for error messages.
#define MTL__LOCATION__
Definition Helpers.h:22
unsigned long long U64
64-bit unsigned integer.
Definition OSDefines.h:32
unsigned short U16
16-bit unsigned integer.
Definition OSDefines.h:30
Interface definition for C++ API for Metrolab THM1176/TFM1186.
#define AVERAGING_COUNT_MAX
#define AVERAGING_COUNT_INC
#define AVERAGING_COUNT_MIN
U64 MeasureImmediateTiming(CTHM1176Instrument< CVISAInstrument, CVISAResourceManager > *pTHM1176, U16 BlockSize, U16 AveragingCount)
#define THM1176_VISA_RESOURCE_PATTERN
#define THM1176_CONNECT_TIMEOUT
C++ wrapper for NI-VISA: interface definition.
Exception to be thrown.
Definition Exception.h:17
List of VISA resource names.
std::vector< MTL_INSTRUMENT_BUFFER_TYPE >::iterator end()
Return an iterator to the end of the buffer.
std::vector< MTL_INSTRUMENT_BUFFER_TYPE >::iterator begin()
Return an iterator to the beginning of the buffer.
const tTokens Tokenize(const char Separator=';', size_t Offset=0)
Split the buffer into tokens.
std::vector< sToken > tTokens
List of tokens.
THM1176 instrument class.
Definition THM1176.h:98
void Disconnect()
Close the connection to the instrument.
Definition THM1176.cpp:917
bool WriteAndRead(const std::string &rWriteStr, CSCPIBuffer &rReadBuffer)
Write an arbitrary command and read the result.
Definition THM1176.cpp:323
const CErrorList & CurrentErrorList()
Fetch current error list.
Definition THM1176.cpp:832
VISA Resource Manager class.
virtual bool Initialize()
Initialize the Resource Manager.
virtual bool FindResources(CResourceList &rList, std::string Filter="?*")
Find VISA resources.
List of errors returned by the instrument.