THM1176InstrumentDriver  1.0
C++ API for Metrolab THM1176
IEEE488InstrumentMiscUtilitiesTest.h
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 #pragma once
9 
10 #include <gtest/gtest.h>
11 #include <regex>
12 
13 #include "IEEE488InstrumentTest.h"
14 
15 using namespace testing;
16 
17 class IEEE488InstrumentMiscUtilitiesTest : public ::testing::Test
18 {
19 protected:
20  static IEEE4888_TEST_RESOURCE_MANAGER_CLASS * pResourceManager;
21  static IEEE4888_TEST_INSTRUMENT_CLASS * pInstrument;
22 
23  static void SetUpTestCase()
24  {
25  ASSERT_EQ(true, ConnectToIEEE488Instrument(pResourceManager, pInstrument));
26  ASSERT_NE(nullptr, pResourceManager);
27  ASSERT_NE(nullptr, pInstrument);
28  ASSERT_EQ(true, pInstrument->IsOpen());
29  ASSERT_EQ(true, pInstrument->Clear());
30  }
31 
32  static void TearDownTestCase()
33  {
34  delete pInstrument;
35  pInstrument = nullptr;
36  delete pResourceManager;
37  pResourceManager = nullptr;
38  }
39 };
40 
41 IEEE4888_TEST_RESOURCE_MANAGER_CLASS * IEEE488InstrumentMiscUtilitiesTest::pResourceManager = nullptr;
42 IEEE4888_TEST_INSTRUMENT_CLASS * IEEE488InstrumentMiscUtilitiesTest::pInstrument = nullptr;
43 
46 {
47  // Write the command and wait until the STB>MAV bit is set.
48  ASSERT_EQ(true, pInstrument->Write ("*IDN?"));
49  U16 l_Status = 0;
50  do
51  {
52  std::this_thread::sleep_for(std::chrono::milliseconds(10));
53  ASSERT_EQ(true, pInstrument->ReadSTB (l_Status));
54  } while ((l_Status & 0x0010) == 0);
55 
56  // A Clear should clear the output buffer, and therefore the MAV bit.
57  pInstrument->Clear();
58  ASSERT_EQ(true, pInstrument->ReadSTB (l_Status));
59  ASSERT_EQ(0, (l_Status & 0x0010));
60  CSCPIBuffer l_Buffer;
61  ASSERT_EQ(false, pInstrument->Read (l_Buffer));
62 
63  // Verify that the *IDN? query still works after all that.
64  ASSERT_EQ(true, pInstrument->Write ("*IDN?"));
65  ASSERT_EQ(true, pInstrument->Read (l_Buffer));
66  EXPECT_EQ(true, CheckIDNResponse (l_Buffer));
67 
68 }
69 
72 {
73  // Start triggered acquisition, with ten triggers.
74  ASSERT_EQ(true, pInstrument->Write(":TRIG:SOUR BUS;COUN 10;:INIT"));
75 
76  // Issue the triggers. Before each one, verify that we're waiting for trigger.
77  CSCPIBuffer l_Buffer;
78  std::string l_Result;
79  U16 l_OperStatus;
80  for (int i = 0; i < 10; i++)
81  {
82  ASSERT_EQ(true, pInstrument->Write (":STAT:OPER?"));
83  ASSERT_EQ(true, pInstrument->Read (l_Buffer));
84  l_Result = std::string(l_Buffer.begin(), l_Buffer.end());
85  l_OperStatus = static_cast<U16>(std::stoul(l_Result));
86  EXPECT_EQ(0x0020, (l_OperStatus & 0x0020));
87  ASSERT_EQ(true, pInstrument->AssertTrigger());
88  std::this_thread::sleep_for(std::chrono::milliseconds(100));
89  }
90 
91  // Now we should no longer be waiting for trigger.
92  ASSERT_EQ(true, pInstrument->Write (":STAT:OPER?"));
93  ASSERT_EQ(true, pInstrument->Read (l_Buffer));
94  l_Result = std::string(l_Buffer.begin(), l_Buffer.end());
95  l_OperStatus = static_cast<U16>(std::stoul(l_Result));
96  EXPECT_EQ(0x0000, (l_OperStatus & 0x0020));
97 
98  // Fetch the measurements. There should be ten of them.
99  ASSERT_EQ(true, pInstrument->Write (":FETC:ARR? 10"));
100  ASSERT_EQ(true, pInstrument->Read (l_Buffer));
101  l_Result = std::string(l_Buffer.begin(), l_Buffer.end());
102  std::regex l_Regex("(([^,]+),){9}([^,]+)");
103  std::smatch l_Match;
104  EXPECT_EQ(true, std::regex_match(l_Result, l_Match, l_Regex));
105 
106  // Verify that the *IDN? query still works after all that.
107  ASSERT_EQ(true, pInstrument->Write ("*IDN?"));
108  ASSERT_EQ(true, pInstrument->Read (l_Buffer));
109  EXPECT_EQ(true, CheckIDNResponse (l_Buffer));
110 
111 }
TEST_F
TEST_F(IEEE488InstrumentMiscUtilitiesTest, Clear)
Test Clear and ReadSTB.
Definition: IEEE488InstrumentMiscUtilitiesTest.h:45
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
IEEE488InstrumentMiscUtilitiesTest::pInstrument
static IEEE4888_TEST_INSTRUMENT_CLASS * pInstrument
Definition: IEEE488InstrumentMiscUtilitiesTest.h:21
IEEE488InstrumentTest.h
Utility functions used to test IEEE488Instrument API.
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
IEEE488InstrumentMiscUtilitiesTest::SetUpTestCase
static void SetUpTestCase()
Definition: IEEE488InstrumentMiscUtilitiesTest.h:23
IEEE488InstrumentMiscUtilitiesTest::pResourceManager
static IEEE4888_TEST_RESOURCE_MANAGER_CLASS * pResourceManager
Definition: IEEE488InstrumentMiscUtilitiesTest.h:20
CheckIDNResponse
bool CheckIDNResponse(const CSCPIBuffer &rBuffer)
Sanity-check of the response to an *IDN? query.
Definition: IEEE488InstrumentTest.cpp:70
IEEE488InstrumentMiscUtilitiesTest::TearDownTestCase
static void TearDownTestCase()
Definition: IEEE488InstrumentMiscUtilitiesTest.h:32
U16
unsigned short U16
16-bit unsigned integer.
Definition: OSDefines.h:31
MTL::Instrument::CSCPIBuffer
Instrument Buffer.
Definition: SCPIInstrumentBuffer.h:44
IEEE488InstrumentMiscUtilitiesTest
Definition: IEEE488InstrumentMiscUtilitiesTest.h:18
ConnectToIEEE488Instrument
bool ConnectToIEEE488Instrument(IEEE4888_TEST_RESOURCE_MANAGER_CLASS *&rpResourceManager, IEEE4888_TEST_INSTRUMENT_CLASS *&rpInstrument)
Connect to an IEEE488Instrument.
Definition: IEEE488InstrumentTest.cpp:40