| THM1176InstrumentDriver 1.2 C++ API for Metrolab THM1176

Loading...
Searching...
No Matches
IEEE488InstrumentTest.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#include <regex>
9
10#include "IEEE488InstrumentTest.h"
11#include "Helpers.h"
12#include "Exception.h"
13
14 bool FetchIEEE4888Resource (IEEE4888_TEST_RESOURCE_MANAGER_CLASS & rResourceManager, tResourceName & rInstrumentName)
15{
16 try
17 {
18 // Open a connection to the the resource manager
19 if (!rResourceManager.Initialize())
20 throw MTL::CException<CIEEE488Instrument>("Could not initialize resource manager", MTL__LOCATION__);
21
22 // Find all instruments.
23 CResourceList l_InstrumentList;
24 if (!rResourceManager.FindResources(l_InstrumentList, IEEE4888_TEST_RESOURCE_FILTER) || l_InstrumentList.empty())
25 throw MTL::CException<CIEEE488Instrument>("Could not find instrument", MTL__LOCATION__);
26
27 // Run the tests on the first one in the list.
28 rInstrumentName = l_InstrumentList.front();
29 }
30 catch (MTL::CException<CIEEE488Instrument> & rE)
31 {
32 std::cout << "l_CManagerException: " << rE.what() << std::endl;
33 rInstrumentName = "";
34 return false;
35 }
36
37 return true;
38}
39
40 bool ConnectToIEEE488Instrument (IEEE4888_TEST_RESOURCE_MANAGER_CLASS * & rpResourceManager, IEEE4888_TEST_INSTRUMENT_CLASS * & rpInstrument)
41{
42 try
43 {
44 // Create the resource manager.
45 rpResourceManager = new IEEE4888_TEST_RESOURCE_MANAGER_CLASS;
46
47 // Fetch the instrument resource name.
48 tResourceName l_InstrumentName;
49 if (!FetchIEEE4888Resource(*rpResourceManager, l_InstrumentName))
50 throw MTL::CException<CIEEE488Instrument>("Could not find instrument", MTL__LOCATION__);
51
52 // Create the instrument object.
53 rpInstrument = new IEEE4888_TEST_INSTRUMENT_CLASS(*rpResourceManager, l_InstrumentName);
54 if (nullptr == rpInstrument)
55 throw MTL::CException<CIEEE488Instrument>("Unable to create instrument object", MTL__LOCATION__);
56
57 // Connect to the instrument.
58 if (!rpInstrument->Open())
59 throw MTL::CException<CIEEE488Instrument>("Unable to connect to instrument", MTL__LOCATION__);
60 }
61 catch (MTL::CException<CIEEE488Instrument> & rE)
62 {
63 std::cout << "l_CManagerException: " << rE.what() << std::endl;
64 rpInstrument = nullptr;
65 return false;
66 }
67 return true;
68}
69
70 bool CheckIDNResponse (const CSCPIBuffer & rBuffer)
71{
72 std::string l_ID = std::string(rBuffer.begin(), rBuffer.end());
73 std::regex l_Regex("([^,]+), *([^,]+), *([^,]+), *([^,]+)");
74 std::smatch l_Match;
75 if (!std::regex_match(l_ID, l_Match, l_Regex))
76 return false;
77
78 std::string l_Manufacturer = l_Match[1].str();
79 std::string l_Model = l_Match[2].str();
80 std::string l_SerialNumber = l_Match[3].str();
81 std::string l_Versions = l_Match[4].str();
82
83 l_Regex = "(Metrolab.*)|(NATIONAL INSTRUMENTS)";
84 if (!std::regex_match(l_Manufacturer, l_Regex))
85 return false;
86
87 l_Regex = "(0x1234)|([0-9]+)";
88 if (!std::regex_match(l_SerialNumber, l_Regex))
89 return false;
90
91 l_Regex = "((THM1176)|(TFM1186)|(PT2026)|(FDI2056)|(SIMPLE IEEE 488.2 DEVICE)).*";
92 if (!std::regex_match(l_Model, l_Regex))
93 return false;
94
95 if (l_Model.compare(0, 7, "THM1176") == 0)
96 l_Regex = "el(([A-Z])([0-9]+))-pr(([A-Z])([0-9]+))-fw(([0-9]+)\\.([0-9]+))\\n";
97 else if (l_Model.compare("SIMPLE IEEE 488.2 DEVICE") == 0)
98 l_Regex = "0\n";
99 else
100 l_Regex = ".*\n";
101 if (!std::regex_match(l_Versions, l_Regex))
102 return false;
103
104 return true;
105}
Exception.h
Exception handling utilities.
Helpers.h
Collection of utility macros for error messages.
MTL__LOCATION__
#define MTL__LOCATION__
Definition Helpers.h:22
ConnectToIEEE488Instrument
bool ConnectToIEEE488Instrument(IEEE4888_TEST_RESOURCE_MANAGER_CLASS *&rpResourceManager, IEEE4888_TEST_INSTRUMENT_CLASS *&rpInstrument)
Connect to an IEEE488Instrument.
Definition IEEE488InstrumentTest.cpp:40
FetchIEEE4888Resource
bool FetchIEEE4888Resource(IEEE4888_TEST_RESOURCE_MANAGER_CLASS &rResourceManager, tResourceName &rInstrumentName)
Open a connection to the Resource Manager and find an IEEE488Instrument.
Definition IEEE488InstrumentTest.cpp:14
CheckIDNResponse
bool CheckIDNResponse(const CSCPIBuffer &rBuffer)
Sanity-check of the response to an *IDN? query.
Definition IEEE488InstrumentTest.cpp:70
IEEE488InstrumentTest.h
Utility functions used to test IEEE488Instrument API.
MTL::CException
Exception to be thrown.
Definition Exception.h:17
MTL::CException::what
virtual const char * what() const noexcept
Return string describing what happened.
Definition Exception.h:34
MTL::Instrument::CResourceList
List of VISA resource names.
Definition IEEE488InstrumentTypes.h:26
MTL::Instrument::CSCPIBuffer
Instrument Buffer.
Definition SCPIInstrumentBuffer.h:44
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
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::tResourceName
std::string tResourceName
IEEE488 resource name.
Definition IEEE488InstrumentTypes.h:22