Instrument Control Lib
Framework to control Oscilloscopes, SMUs, Function Generators and DC Powersupplies via Ethernet.
Device.h
Go to the documentation of this file.
1 
7 #ifndef CE_DEVICE_DEVICE_H
8 #define CE_DEVICE_DEVICE_H
9 
10 
11 #include "ExecArgs.h"
12 #include "ctlib/Logging.hpp"
13 #include "ctlib/Exception.h"
14 
15 
16 #include <string>
17 #include <vector>
18 
19 namespace PIL
20 {
21  class Socket;
22  class Logging;
23 }
24 
30 class Device {
31 
32 public:
33  enum SEND_METHOD {
35  BUFFER_ENABLED = 1
36  };
37 
38  explicit Device(std::string ipAddress, int timeoutInMs, SEND_METHOD mode = DIRECT_SEND, bool throwException = true);
39  explicit Device(std::string ipAddress, int timeoutInMs, PIL::Logging *logger, SEND_METHOD mode = DIRECT_SEND,
40  bool throwException = true);
41  explicit Device(std::string ipAddress, uint16_t srcPort, uint16_t destPort, int timeoutInMs, PIL::Logging *logger,
42  SEND_METHOD mode = DIRECT_SEND, bool throwException = true);
43  ~Device();
44 
45  PIL_ERROR_CODE Connect();
46  PIL_ERROR_CODE Disconnect();
47  [[nodiscard]] bool isOpen() const;
48  [[nodiscard]] bool isBuffered() const;
49 
50  std::string getDeviceIdentifier();
51  PIL_ERROR_CODE Exec(const std::string& command, ExecArgs *args = nullptr, char *result = nullptr, bool br = true,
52  int size = 1024);
53  PIL_ERROR_CODE Exec(const std::string &command, ExecArgs *args, std::string *result, bool br);
54  PIL_ERROR_CODE ExecCommands(std::string &commands);
55 
56  std::string ReturnErrorMessage();
57 
58  std::string getBufferedScript();
59 
60  void changeSendMode(SEND_METHOD mode);
61 
62  PIL_ERROR_CODE delay(double delayTime);
63 
64 protected:
65  PIL_ERROR_CODE handleErrorsAndLogging(PIL_ERROR_CODE errorCode, bool throwException, PIL::Level logLevel,
66  const std::string& fileName, int line, std::string formatStr, ...);
67 
68  static bool errorOccured(PIL_ERROR_CODE errorCode);
69  static PIL_ERROR_CODE postRequest(const std::string &url, std::string &payload);
70  static std::string vectorToStringNL(std::vector<std::string> vector);
71  static std::string replaceAllSubstrings(std::string str, const std::string &from, const std::string &to);
72  static std::vector<std::string> splitString(const std::string &toSplit, const std::string &delimiter);
73 
74  std::string m_IPAddr;
75  PIL_ErrorHandle m_ErrorHandle;
76  std::string m_DeviceName{};
77  PIL::Socket *m_SocketHandle;
78  PIL::Logging *m_Logger;
79  int m_destPort = 5025;
80  int m_srcPort = 5025;
83  std::vector<std::string> m_BufferedScript;
84 };
85 
86 #endif //CE_DEVICE_DEVICE_H
Basic device class This class contains all basic method of all devices, like Connect,...
Definition: Device.h:30
static PIL_ERROR_CODE postRequest(const std::string &url, std::string &payload)
Sends a post request to the given url with the given payload.
Definition: Device.cpp:309
SEND_METHOD m_SendMode
Definition: Device.h:82
bool isOpen() const
Checks if the connection to the device is established.
Definition: Device.cpp:136
static std::string vectorToStringNL(std::vector< std::string > vector)
Transforms the given vector into a string. Each vector entry will be a line in the resulting string.
Definition: Device.cpp:326
bool m_EnableExceptions
Definition: Device.h:81
static std::vector< std::string > splitString(const std::string &toSplit, const std::string &delimiter)
Splits a string by the given delimiter.
Definition: Device.cpp:357
PIL_ERROR_CODE delay(double delayTime)
Stops the execution for the specified amount of time in seconds. If buffering is enabled,...
Definition: Device.cpp:265
PIL_ERROR_CODE Disconnect()
Disconnect from the device.
Definition: Device.cpp:117
PIL_ERROR_CODE Connect()
Establish a connection to the device.
Definition: Device.cpp:100
std::string m_IPAddr
Definition: Device.h:74
std::vector< std::string > m_BufferedScript
Definition: Device.h:83
PIL_ErrorHandle m_ErrorHandle
Definition: Device.h:75
int m_srcPort
Definition: Device.h:80
bool isBuffered() const
Checks if the commands sent to the device get buffered.
Definition: Device.cpp:144
PIL_ERROR_CODE handleErrorsAndLogging(PIL_ERROR_CODE errorCode, bool throwException, PIL::Level logLevel, const std::string &fileName, int line, std::string formatStr,...)
Handle logging messages, logs it based on the previously passed logging object. If exceptions are ena...
Definition: Device.cpp:76
PIL_ERROR_CODE ExecCommands(std::string &commands)
Definition: Device.cpp:242
std::string ReturnErrorMessage()
Definition: Device.cpp:255
PIL::Socket * m_SocketHandle
Definition: Device.h:77
PIL_ERROR_CODE Exec(const std::string &command, ExecArgs *args=nullptr, char *result=nullptr, bool br=true, int size=1024)
execute a (SCPI) command
Definition: Device.cpp:189
static std::string replaceAllSubstrings(std::string str, const std::string &from, const std::string &to)
Replaces all substrings in the string with the given replacement string.
Definition: Device.cpp:342
void changeSendMode(SEND_METHOD mode)
Changes the send mode of this device, i.e. whether the commands get buffered or sent directly to the ...
Definition: Device.cpp:291
~Device()
Definition: Device.cpp:59
std::string getDeviceIdentifier()
Gets the name of the currently connected device.
Definition: Device.cpp:152
PIL::Logging * m_Logger
Definition: Device.h:78
int m_destPort
Definition: Device.h:79
static bool errorOccured(PIL_ERROR_CODE errorCode)
Checks if a error occured given the error code.
Definition: Device.cpp:300
SEND_METHOD
Definition: Device.h:33
@ DIRECT_SEND
Definition: Device.h:34
@ BUFFER_ENABLED
Definition: Device.h:35
std::string m_DeviceName
Definition: Device.h:76
std::string getBufferedScript()
Transforms the current buffered script into a string and returns it.
Definition: Device.cpp:283
Device(std::string ipAddress, int timeoutInMs, SEND_METHOD mode=DIRECT_SEND, bool throwException=true)
Constructor for the Device without passing a logging object.
Definition: Device.cpp:44
ExecArgs class is used to create execution arguments based on a parameter seperator value scheme....
Definition: ExecArgs.h:15
This class contains a implementation of a command line interface. It supports various functions like ...
Definition: CommandLineInterface.h:16