rohdeschwarz  0.2.0
TCPIP socket library for Rohde & Schwarz instrument control
instrument.hpp
Go to the documentation of this file.
1 
7 #ifndef ROHDESCHWARZ_INSTRUMENTS_INSTRUMENT_HPP
8 #define ROHDESCHWARZ_INSTRUMENTS_INSTRUMENT_HPP
9 
10 
11 // rohdeschwarz
15 #include "rohdeschwarz/helpers.hpp"
17 
18 
19 // rs visa
20 #include "rs-visa/visatype.h"
21 
22 
23 // boost
24 #include "boost/format.hpp"
25 
26 
27 // std lib
28 #include <complex>
29 #include <cstddef>
30 #include <memory>
31 #include <string>
32 #include <vector>
33 
34 
36 {
37 
38 
53 {
54 
55 public:
56 
57 
58  // open / close connection
59 
63  bool isOpen() const;
64 
65 
73  bool openVisa(std::string resource, unsigned int timeout_ms = 2000);
74 
75 
85  bool openTcp(std::string host, unsigned int timeout_ms = 2000);
86 
87 
91  void close();
92 
93 
94  // io buffer
95 
96  std::size_t bufferSize_B() const;
97 
98  void setBufferSize(std::size_t size_bytes);
99 
100  std::vector<unsigned char>* buffer();
101 
102  const std::vector<unsigned char>* buffer() const;
103 
104  std::vector<unsigned char> takeData();
105 
106 
107  // timeout
108 
112  int timeout_ms();
113 
114 
120  bool setTimeout(int timeout_ms);
121 
122 
123  // raw io
124 
125  bool readData(unsigned char* buffer, std::size_t bufferSize, std::size_t* readSize = nullptr);
126 
127 
128  bool writeData(const unsigned char* data, std::size_t dataSize, std::size_t* writeSize = nullptr);
129 
130 
131  // raw io with internal buffers
132 
133  bool readData(std::size_t* readSize = nullptr);
134 
135 
136  // string io
137 
138  std::string read();
139 
140 
141  template<class... Args>
142  bool write(std::string scpi_command, Args&&... args)
143  {
144  // format scpi command
145  auto format = boost::format(scpi_command);
146  ([&]
147  {
148  format % args;
149  } (), ...);
150 
151  // get complete command string
152  auto command = format.str();
153 
154  // get data pointer, size
155  using uchar_p = unsigned char*;
156  auto data = uchar_p(command.c_str());
157  auto size = command.size();
158 
159  // write data
160  std::size_t writeSize;
161  if (!writeData(data, size, &writeSize))
162  {
163  // error
164  return false;
165  }
166 
167  // write complete?
168  return writeSize == size;
169  }
170 
171 
172  template<class... Args>
173  std::string query(std::string scpi_command, Args&&... args)
174  {
175  // write
176  if (!write(scpi_command, &args...))
177  {
178  // error
179  return std::string();
180  }
181 
182  // read
183  return read();
184  }
185 
186 
187  // basic type io
188 
189  template<class OutputType>
190  OutputType readValue()
191  {
192  return to_value<OutputType>(read());
193  }
194 
195 
196  template<class OutputType, class... Args>
197  OutputType queryValue(std::string scpi_command, Args&&... args)
198  {
199  return to_value<OutputType>(query(scpi_command, &args...));
200  }
201 
202  // scpi bool io
203 
204  bool readScpiBool();
205 
206  template<class... Args>
207  bool queryScpiBool(std::string scpi_command, Args&&... args)
208  {
209  // write
210  if (!write(scpi_command, &args...))
211  {
212  // error
213  return false;
214  }
215 
216  // parse result
218  }
219 
220 
221  // ascii data vector io
222 
226  std::vector<double> readAsciiVector();
227 
228 
232  std::vector<std::complex<double>> readAsciiComplexVector();
233 
234 
235  // block data io
236 
243 
244 
245  // block data vector io
246 
250  std::vector<double> read64BitVector();
251 
252 
256  std::vector<std::complex<double>> read64BitComplexVector();
257 
258 
259  // status
260 
264  bool isBusError() const;
265 
266 
270  std::string busStatus() const;
271 
272 
273  // common scpi commands
274 
280  std::string id(); // *IDN?
281 
282 
288  std::string options(); // *OPT?
289 
290 
296  void clearErrors(); // *CLS
297 
298 
304  void preset(); // *RST
305 
306 
312  void wait(); // *WAI
313 
314 
318  bool blockUntilOperationComplete(unsigned int timeout_ms = 2000);
319 
320 
321 private:
322 
323  std::shared_ptr<rohdeschwarz::busses::Bus> _bus;
324 
325 
326 }; // Instrument
327 
328 
329 } // rohdeschwarz::instruments
330 #endif // ROHDESCHWARZ_INSTRUMENTS_INSTRUMENT_HPP
rohdeschwarz::scpi::BlockData definition
unsigned char * uchar_p
Definition: bus.cpp:22
rohdeschwarz::busses::Bus class definition
Object-oriented R&S Instrument control.
Definition: instrument.hpp:53
void clearErrors()
Clears SCPI errors.
Definition: instrument.cpp:268
bool readData(unsigned char *buffer, std::size_t bufferSize, std::size_t *readSize=nullptr)
Definition: instrument.cpp:123
std::vector< double > readAsciiVector()
reads ascii data and parses it into vector <double>
Definition: instrument.cpp:157
void wait()
Instructs instrument to perform previous SCPI commands before proceeding.
Definition: instrument.cpp:280
std::vector< std::complex< double > > readAsciiComplexVector()
reads ascii data and parses it into vector <complex <double>>
Definition: instrument.cpp:164
bool isOpen() const
Check for an open connection to an instrument.
Definition: instrument.cpp:31
std::string options()
Queries instrument options string.
Definition: instrument.cpp:262
int timeout_ms()
Query IO timeout time, in milliseconds.
Definition: instrument.cpp:111
bool writeData(const unsigned char *data, std::size_t dataSize, std::size_t *writeSize=nullptr)
Definition: instrument.cpp:129
bool isBusError() const
Checks the bus status for an error.
Definition: instrument.cpp:244
std::vector< std::complex< double > > read64BitComplexVector()
Reads block data and parses it into vector <complex <double>>
Definition: instrument.cpp:229
std::string id()
Queries instrument ID string.
Definition: instrument.cpp:256
void close()
Close the connection to the instrument.
Definition: instrument.cpp:77
bool openVisa(std::string resource, unsigned int timeout_ms=2000)
Open VISA connection to instrument.
Definition: instrument.cpp:37
std::vector< unsigned char > takeData()
Definition: instrument.cpp:105
std::string busStatus() const
Gets the bus status as a human-readable string.
Definition: instrument.cpp:250
OutputType queryValue(std::string scpi_command, Args &&... args)
Definition: instrument.hpp:197
bool setTimeout(int timeout_ms)
Set IO timeout time.
Definition: instrument.cpp:117
std::string query(std::string scpi_command, Args &&... args)
Definition: instrument.hpp:173
bool queryScpiBool(std::string scpi_command, Args &&... args)
Definition: instrument.hpp:207
bool write(std::string scpi_command, Args &&... args)
Definition: instrument.hpp:142
bool openTcp(std::string host, unsigned int timeout_ms=2000)
Open tcp socket connection to an instrument.
Definition: instrument.cpp:57
scpi::BlockData readBlockData()
Read Block Data.
Definition: instrument.cpp:171
std::vector< double > read64BitVector()
Reads block data and parses it into vector <double>
Definition: instrument.cpp:214
bool blockUntilOperationComplete(unsigned int timeout_ms=2000)
Queries *OPC? - block until operation complete.
Definition: instrument.cpp:286
std::vector< unsigned char > * buffer()
Definition: instrument.cpp:95
void setBufferSize(std::size_t size_bytes)
Definition: instrument.cpp:89
void preset()
Perform instrument preset.
Definition: instrument.cpp:274
Object-oriented Block Data storage and manipulation.
Definition: block_data.hpp:24
rohdeschwarz helper function definition
bool toBool(std::string scpi)
Converts SCPI bool to C/C++ bool.
Definition: bool.cpp:18