Instrument Control Lib
Framework to control Oscilloscopes, SMUs, Function Generators and DC Powersupplies via Ethernet.
Device Class Reference

Basic device class This class contains all basic method of all devices, like Connect, Exec, WhatAmI etc. More...

#include <Device.h>

Inheritance diagram for Device:
Inheritance graph
Collaboration diagram for Device:
Collaboration graph

Public Types

enum  SEND_METHOD { DIRECT_SEND = 0 , BUFFER_ENABLED = 1 }
 

Public Member Functions

 Device (std::string ipAddress, int timeoutInMs, SEND_METHOD mode=DIRECT_SEND, bool throwException=true)
 Constructor for the Device without passing a logging object. More...
 
 Device (std::string ipAddress, int timeoutInMs, PIL::Logging *logger, SEND_METHOD mode=DIRECT_SEND, bool throwException=true)
 Constructor of Device generates a socket handle without connecting to it. More...
 
 Device (std::string ipAddress, uint16_t srcPort, uint16_t destPort, int timeoutInMs, PIL::Logging *logger, SEND_METHOD mode=DIRECT_SEND, bool throwException=true)
 Base class containing the base functionality of all devices. And parameters to store the name connection type, etc. More...
 
 ~Device ()
 
PIL_ERROR_CODE Connect ()
 Establish a connection to the device. More...
 
PIL_ERROR_CODE Disconnect ()
 Disconnect from the device. More...
 
bool isOpen () const
 Checks if the connection to the device is established. More...
 
bool isBuffered () const
 Checks if the commands sent to the device get buffered. More...
 
std::string getDeviceIdentifier ()
 Gets the name of the currently connected device. More...
 
PIL_ERROR_CODE Exec (const std::string &command, ExecArgs *args=nullptr, char *result=nullptr, bool br=true, int size=1024)
 execute a (SCPI) command More...
 
PIL_ERROR_CODE Exec (const std::string &command, ExecArgs *args, std::string *result, bool br)
 
PIL_ERROR_CODE ExecCommands (std::string &commands)
 
std::string ReturnErrorMessage ()
 
std::string getBufferedScript ()
 Transforms the current buffered script into a string and returns it. More...
 
void changeSendMode (SEND_METHOD mode)
 Changes the send mode of this device, i.e. whether the commands get buffered or sent directly to the device. More...
 
PIL_ERROR_CODE delay (double delayTime)
 Stops the execution for the specified amount of time in seconds. If buffering is enabled, the delay is included in the buffered script. Otherwise this thread sleeps for the given time. More...
 

Protected Member Functions

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 enabled throw an exception otherwise return the error code. More...
 

Static Protected Member Functions

static bool errorOccured (PIL_ERROR_CODE errorCode)
 Checks if a error occured given the error code. More...
 
static PIL_ERROR_CODE postRequest (const std::string &url, std::string &payload)
 Sends a post request to the given url with the given payload. More...
 
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. More...
 
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. More...
 
static std::vector< std::string > splitString (const std::string &toSplit, const std::string &delimiter)
 Splits a string by the given delimiter. More...
 

Protected Attributes

std::string m_IPAddr
 
PIL_ErrorHandle m_ErrorHandle
 
std::string m_DeviceName {}
 
PIL::Socket * m_SocketHandle
 
PIL::Logging * m_Logger
 
int m_destPort = 5025
 
int m_srcPort = 5025
 
bool m_EnableExceptions
 
SEND_METHOD m_SendMode
 
std::vector< std::string > m_BufferedScript
 

Detailed Description

Basic device class This class contains all basic method of all devices, like Connect, Exec, WhatAmI etc.

Member Enumeration Documentation

◆ SEND_METHOD

Enumerator
DIRECT_SEND 
BUFFER_ENABLED 

Constructor & Destructor Documentation

◆ Device() [1/3]

Device::Device ( std::string  ipAddress,
int  timeoutInMs,
SEND_METHOD  mode = DIRECT_SEND,
bool  throwException = true 
)
explicit

Constructor for the Device without passing a logging object.

Parameters
ipAddressIP address of the device
timeoutInMs

◆ Device() [2/3]

Device::Device ( std::string  ipAddress,
int  timeoutInMs,
PIL::Logging *  logger,
SEND_METHOD  mode = DIRECT_SEND,
bool  throwException = true 
)
explicit

Constructor of Device generates a socket handle without connecting to it.

Parameters
ipAddressThe ip address to connect to the device.
timeoutInMsTimeout of the socket in milliseconds.
loggerPass a logging object to configure what types of messages should be logged. If nullptr is passed, logging is disabled.

◆ Device() [3/3]

Device::Device ( std::string  ipAddress,
uint16_t  srcPort,
uint16_t  destPort,
int  timeoutInMs,
PIL::Logging *  logger,
SEND_METHOD  mode = DIRECT_SEND,
bool  throwException = true 
)
explicit

Base class containing the base functionality of all devices. And parameters to store the name connection type, etc.

Authors
Wuhao Liu, Alexander Braml, Florian Frank

◆ ~Device()

Device::~Device ( )
Here is the call graph for this function:

Member Function Documentation

◆ changeSendMode()

void Device::changeSendMode ( SEND_METHOD  mode)

Changes the send mode of this device, i.e. whether the commands get buffered or sent directly to the device.

Parameters
modeThe new send mode to use.

◆ Connect()

PIL_ERROR_CODE Device::Connect ( )

Establish a connection to the device.

Returns
if the connection is established return PIL_NO_ERROR otherwise return error code.
Exceptions
PIL::Exceptionif the connection could not established.
Here is the call graph for this function:

◆ delay()

PIL_ERROR_CODE Device::delay ( double  delayTime)

Stops the execution for the specified amount of time in seconds. If buffering is enabled, the delay is included in the buffered script. Otherwise this thread sleeps for the given time.

Parameters
delayTimeThe delay in seconds.
Returns
The received error code.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Disconnect()

PIL_ERROR_CODE Device::Disconnect ( )

Disconnect from the device.

Returns
if the connection is disconnected return PIL_NO_ERROR otherwise return error code.
Exceptions
PIL::Exceptionif the connection could not disconnected.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ errorOccured()

bool Device::errorOccured ( PIL_ERROR_CODE  errorCode)
staticprotected

Checks if a error occured given the error code.

Parameters
errorCodeThe error code to check.
Returns
true if and only if there is an error.
Here is the caller graph for this function:

◆ Exec() [1/2]

PIL_ERROR_CODE Device::Exec ( const std::string &  command,
ExecArgs args,
std::string *  result,
bool  br 
)
Here is the call graph for this function:

◆ Exec() [2/2]

PIL_ERROR_CODE Device::Exec ( const std::string &  command,
ExecArgs args = nullptr,
char *  result = nullptr,
bool  br = true,
int  size = 1024 
)

execute a (SCPI) command

Parameters
messagea (SCPI) command
result[out]The string will contain the result message(Max length: 1024)
brwhether add a '\n' at the end of the command
Note
Different companies, devices may have different levels of support for SCPI. Some device may require '\n', some may not. That is why here is a br param.
Todo:
Deal with execution timeout;
Warning
Some commands may timeout
KST3000 k.Exec("RSTater?", buffer);
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
Mixed Single Oscilloscope(Oscillator) Commend operations of a single generator are defined....
Definition: KST3000.h:26
Here is the caller graph for this function:

◆ ExecCommands()

PIL_ERROR_CODE Device::ExecCommands ( std::string &  commands)
Here is the call graph for this function:

◆ getBufferedScript()

std::string Device::getBufferedScript ( )

Transforms the current buffered script into a string and returns it.

Returns
The currently buffered script as a string.
Here is the call graph for this function:

◆ getDeviceIdentifier()

std::string Device::getDeviceIdentifier ( )

Gets the name of the currently connected device.

Returns
The name of this device.
Here is the call graph for this function:

◆ handleErrorsAndLogging()

PIL_ERROR_CODE Device::handleErrorsAndLogging ( PIL_ERROR_CODE  errorCode,
bool  throwException,
PIL::Level  logLevel,
const std::string &  fileName,
int  line,
std::string  formatStr,
  ... 
)
protected

Handle logging messages, logs it based on the previously passed logging object. If exceptions are enabled throw an exception otherwise return the error code.

Parameters
errorCodeerror code to identify the error.
throwExceptionif true, throw an exception.
logLevellog level of the message.
fileNamename of the file where the error occurred.
lineline number where the error occurred.
formatStrformat string for the error message.
...variable arguments for the format string.
Returns
error code.
Here is the caller graph for this function:

◆ isBuffered()

bool Device::isBuffered ( ) const

Checks if the commands sent to the device get buffered.

Returns
true if connection is buffering is enabled, otherwise false.
Here is the caller graph for this function:

◆ isOpen()

bool Device::isOpen ( ) const

Checks if the connection to the device is established.

Returns
true if connection is established, otherwise false.
Here is the caller graph for this function:

◆ postRequest()

PIL_ERROR_CODE Device::postRequest ( const std::string &  url,
std::string &  payload 
)
staticprotected

Sends a post request to the given url with the given payload.

Parameters
urlThe url to send the post request to.
payloadThe payload to send.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ replaceAllSubstrings()

std::string Device::replaceAllSubstrings ( std::string  str,
const std::string &  from,
const std::string &  to 
)
staticprotected

Replaces all substrings in the string with the given replacement string.

Parameters
strString to replace substrings in.
fromThe substring to replace.
toThe string to replace all matching substrings with.
Returns
The processed string
Here is the caller graph for this function:

◆ ReturnErrorMessage()

std::string Device::ReturnErrorMessage ( )

◆ splitString()

std::vector< std::string > Device::splitString ( const std::string &  toSplit,
const std::string &  delimiter 
)
staticprotected

Splits a string by the given delimiter.

Parameters
toSplitThe string to split.
delimiterThe delimter to split the string by.
Returns
A vector of substrings of toSplit.
Here is the caller graph for this function:

◆ vectorToStringNL()

std::string Device::vectorToStringNL ( std::vector< std::string >  vector)
staticprotected

Transforms the given vector into a string. Each vector entry will be a line in the resulting string.

Parameters
vectorThe vector to transform.
Returns
The given vector as string.
Here is the caller graph for this function:

Member Data Documentation

◆ m_BufferedScript

std::vector<std::string> Device::m_BufferedScript
protected

◆ m_destPort

int Device::m_destPort = 5025
protected

◆ m_DeviceName

std::string Device::m_DeviceName {}
protected

◆ m_EnableExceptions

bool Device::m_EnableExceptions
protected

◆ m_ErrorHandle

PIL_ErrorHandle Device::m_ErrorHandle
protected

◆ m_IPAddr

std::string Device::m_IPAddr
protected

◆ m_Logger

PIL::Logging* Device::m_Logger
protected

◆ m_SendMode

SEND_METHOD Device::m_SendMode
protected

◆ m_SocketHandle

PIL::Socket* Device::m_SocketHandle
protected

◆ m_srcPort

int Device::m_srcPort = 5025
protected

The documentation for this class was generated from the following files: