Motion Master
|
Handles TCP communication with SOMANET devices over Ethernet. More...
#include <ethernet_client.h>
Public Member Functions | |
EthernetDevice (const std::string &ip, unsigned short port) | |
Constructs an EthernetDevice object with the specified IP address and port. More... | |
~EthernetDevice () | |
Destructor for the EthernetDevice class. More... | |
uint16_t | incrementSeqId () |
Increments the sequence ID atomically and wraps it around at the maximum value. More... | |
bool | connect () |
Establishes a connection to the remote server. More... | |
bool | isConnected () |
Checks if the Ethernet socket is currently open. More... | |
bool | disconnect () |
Closes the Ethernet socket connection. More... | |
EthernetMessage | exchangeWithTimeout (const EthernetMessage &request, const std::chrono::steady_clock::duration expiryTime) |
Exchanges a message with a remote server and waits for a response with a timeout. More... | |
uint8_t | getState (const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(3000)) override |
Sends a request to read the state of the device and returns the state value. More... | |
bool | setState (uint8_t state, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(3000)) override |
Sends a state control command to the Ethernet device and checks the response. More... | |
std::vector< uint8_t > | readFile (const std::string &filename, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000)) override |
Reads the contents of a file over Ethernet. More... | |
std::vector< std::string > | readFileList (const bool stripSizeSuffix=true, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(3000)) |
Reads a list of file names from the device. More... | |
bool | removeFile (const std::string &filename, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(3000)) |
Removes a file from the device. More... | |
bool | writeFile (const std::string &filename, const std::vector< uint8_t > &data, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000)) override |
Sends a file in chunks to the remote server via Ethernet. More... | |
bool | triggerFirmwareUpdate (const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(2000)) |
Sends a firmware update request to the connected Integro device. More... | |
std::vector< common::Parameter > | getParameters (bool readValues=false, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(1000)) |
std::vector< uint8_t > | readSdo (uint16_t index, uint8_t subindex, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(1000)) |
Reads an SDO (Service Data Object) from an Ethernet device. More... | |
bool | writeSdo (uint16_t index, uint8_t subindex, const std::vector< uint8_t > &data, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(1000)) |
Writes an SDO (Service Data Object) to an Ethernet device. More... | |
void | loadParameters (bool readValues=false, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(9000)) override |
Loads parameters from the device and stores them locally. More... | |
void | clearParameters () override |
Clears all loaded object dictionary parameters. More... | |
common::Parameter & | findParameter (uint16_t index, uint8_t subindex) override |
Finds and returns a reference to a parameter by its index and subindex. More... | |
common::Parameter & | upload (const uint16_t index, const uint8_t subindex, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(3000)) override |
Uploads a parameter from the device via SDO and updates the local parameter store. More... | |
template<typename T > | |
T | upload (uint16_t index, uint8_t subindex, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000)) |
Uploads a parameter from the device and returns its value as the specified type. More... | |
void | download (uint16_t index, uint8_t subindex, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000)) override |
Downloads a parameter to the device using SDO communication. More... | |
void | download (uint16_t index, uint8_t subindex, const common::ParameterValue &value, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000)) override |
Sets a parameter value in the local store and downloads it to the device. More... | |
template<typename T > | |
void | download (uint16_t index, uint8_t subindex, const T &value, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000)) |
Sets and downloads a parameter value to the device using the specified type. More... | |
std::vector< uint8_t > | sendAndReceiveProcessData (const std::vector< uint8_t > &data, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(1000)) |
Sends process data to a remote device and receives the response. More... | |
void | exchangeProcessDataAndUpdateParameters () override |
Exchanges process data with the remote device and updates local parameters accordingly. More... | |
Handles TCP communication with SOMANET devices over Ethernet.
This class provides methods for connecting to a remote server, sending messages, and receiving responses over a TCP connection using Boost.Asio. It manages the underlying socket, connection, and I/O operations required for client-server communication.
EthernetDevice::EthernetDevice | ( | const std::string & | ip, |
unsigned short | port | ||
) |
Constructs an EthernetDevice object with the specified IP address and port.
Initializes the client with the target server's IP address and port number.
ip | The IP address of the server to connect to. |
port | The port number to use for the connection. |
EthernetDevice::~EthernetDevice | ( | ) |
Destructor for the EthernetDevice class.
Closes the socket and disconnects from the server if the socket is open.
|
overridevirtual |
Clears all loaded object dictionary parameters.
This function removes all entries from the internal parameter map, effectively resetting the parameter list of the slave device.
Implements common::Device.
bool EthernetDevice::connect | ( | ) |
Establishes a connection to the remote server.
Attempts to connect to the specified IP address and port provided during construction. This method blocks until the connection is either successfully established or fails.
true
if the connection was successfully established; false
otherwise. bool EthernetDevice::disconnect | ( | ) |
Closes the Ethernet socket connection.
This function attempts to close the Ethernet socket. It handles any errors that may occur during the process, logging the result. If the socket is successfully closed, it returns true
; otherwise, it returns false
.
true
if the socket was closed successfully, false
if an error occurred or an exception was thrown during the operation.
|
overridevirtual |
Sets a parameter value in the local store and downloads it to the device.
This function updates the specified parameter in the local store with the provided value, serializes it, and then performs an SDO download to transfer the data to the device.
index | The 16-bit index of the parameter in the object dictionary. |
subindex | The 8-bit subindex of the parameter. |
value | The new parameter value to set and download. |
expiryTime | The maximum duration to wait for the SDO download operation to complete. |
std::runtime_error | If the parameter data is empty after setting the value, or if the SDO download operation fails. |
Implements common::Device.
|
overridevirtual |
Downloads a parameter to the device using SDO communication.
This function sends the binary data of the specified parameter (retrieved from the local store) to the device. The operation fails if the parameter's data is empty.
index | The 16-bit index of the parameter in the object dictionary. |
subindex | The 8-bit subindex of the parameter. |
expiryTime | The maximum duration to wait for the SDO download operation to complete. |
std::runtime_error | If the parameter data is empty or the SDO download fails. |
Implements common::Device.
|
inline |
Sets and downloads a parameter value to the device using the specified type.
This templated overload wraps the given value in a common::ParameterValue
, updates the local parameter store, and performs an SDO download to transfer the data to the device.
T | The type of the value to set and download. |
index | The 16-bit index of the parameter in the object dictionary. |
subindex | The 8-bit subindex of the parameter. |
value | The new parameter value to set and download. |
expiryTime | The maximum duration to wait for the SDO download operation. Defaults to 5000 milliseconds. |
std::runtime_error | If the parameter data is empty after setting the value, or if the SDO download operation fails. |
|
overridevirtual |
Exchanges process data with the remote device and updates local parameters accordingly.
This function prepares the process data to send by collecting the current data from parameters mapped as RxPDOs. It then sends this data and receives updated process data from the remote device. The received data is used to update local parameters mapped as TxPDOs.
The bitlength of each PDO entry is converted to byte size, rounding up to handle non-byte-aligned bit lengths. If received data is smaller than expected for a given PDO, an error is logged and the update for that PDO is skipped. After processing all PDOs, if there is any extra received data not mapped to parameters, a warning is logged.
std::runtime_error | if findParameter throws (e.g., if parameter not found). |
Implements common::Device.
EthernetMessage EthernetDevice::exchangeWithTimeout | ( | const EthernetMessage & | request, |
const std::chrono::steady_clock::duration | expiryTime | ||
) |
Exchanges a message with a remote server and waits for a response with a timeout.
This method serializes the request message, sends it to the server using an asynchronous write operation, and waits for a response with a timeout. If the operation takes longer than the specified expiry time, the operation will be canceled and an error will be thrown.
requestMessage | The message to send to the remote server. |
expiryTime | The duration to wait for the socket operation (read/write) before timing out. |
std::runtime_error | If the write or read operation fails, or if the operation times out. |
|
overridevirtual |
Finds and returns a reference to a parameter by its index and subindex.
This function looks up a parameter in the internal parameter map using the specified index and subindex. If the parameter is found, a reference to it is returned. If not, a runtime exception is thrown.
index | The index of the parameter to find. |
subindex | The subindex of the parameter to find. |
std::runtime_error | If the parameter with the specified index and subindex is not found in the map. |
Implements common::Device.
std::vector< common::Parameter > EthernetDevice::getParameters | ( | bool | readValues = false , |
const std::chrono::steady_clock::duration | expiryTime = std::chrono::milliseconds(1000) |
||
) |
Retrieves a list of parameters from the device, potentially updating their values.
This function communicates with the device to request and retrieve parameter data in multiple segments. It constructs request messages to retrieve parameter data and processes the received responses, accumulating all parameter data in the content
buffer. Once all data has been retrieved, it parses the content buffer into individual parameter objects and returns them as a std::vector<common::Parameter>
.
readValues | If true, the function will attempt to read parameter values from the device. If false, it will use the existing data from the content buffer. |
expiryTime | The duration to wait for the socket operation (read/write) before timing out. Defaults to 1000 milliseconds. |
common::Parameter
objects containing the parameters retrieved from the device.boost::system::system_error | if sending or receiving fails. |
std::runtime_error | if the response buffer is too small to parse. |
|
overridevirtual |
Sends a request to read the state of the device and returns the state value.
This function sends a request to the device using the EthernetMessageType::STATE_READ
message type. It increments the sequence ID, constructs the request message, serializes it, and sends it to the device. After sending the request, the function waits for the response, parses the response message, and returns the state value as a uint8_t
.
The returned state corresponds to the EtherCAT state machine states, where:
expiryTime | The duration to wait for the socket operation (read/write) before timing out. Defaults to 3000 milliseconds. |
boost::system::system_error | if sending or receiving fails. |
std::runtime_error | if the response buffer is too small to parse. |
Implements common::Device.
uint16_t EthernetDevice::incrementSeqId | ( | ) |
Increments the sequence ID atomically and wraps it around at the maximum value.
This function atomically increments the seq_id
(a 16-bit unsigned integer). When the value reaches the maximum value of uint16_t
(0xFFFF), it wraps around to 0. It ensures thread safety when used in multithreaded environments.
bool EthernetDevice::isConnected | ( | ) |
Checks if the Ethernet socket is currently open.
Returns true if the underlying socket is open, indicating an active or initialized connection. Returns false otherwise.
|
overridevirtual |
Loads parameters from the device and stores them locally.
This function retrieves a list of parameters using the getParameters
method. It then stores each parameter in a map for later use, keyed by a pair consisting of the parameter's index and subindex.
readValues | If true, the values of the parameters are read from the device; otherwise, only the parameter metadata is retrieved. |
expiryTime | The duration to wait for the socket operation (read/write) before timing out. Defaults to 9000 milliseconds. |
Implements common::Device.
|
overridevirtual |
Reads the contents of a file over Ethernet.
This function sends a series of Ethernet messages to request file data in segments. It uses a loop to send requests and receive responses from a remote server until the entire file is read. The file content is accumulated in a vector and returned.
filename | The name of the file to be read. |
expiryTime | The duration to wait for the socket operation (read/write) before timing out. Defaults to 3000 milliseconds. |
std::vector<uint8_t>
containing the file data received from the remote server.boost::system::system_error | if sending or receiving fails. |
std::runtime_error | if the response buffer is too small to parse. |
Implements common::Device.
std::vector< std::string > EthernetDevice::readFileList | ( | const bool | stripSizeSuffix = true , |
const std::chrono::steady_clock::duration | expiryTime = std::chrono::milliseconds(3000) |
||
) |
Reads a list of file names from the device.
Sends a request to read the file list using the "fs-getlist" command, waits up to the specified timeout, and parses the result into individual lines. Each line represents a file name. Handles both Unix (\n
) and Windows (\r\n
) line endings.
stripSizeSuffix | If true, removes the ", size: <bytes>" suffix from each line. Defaults to true. |
expiryTime | The duration to wait for the socket operation (read/write) before timing out. Defaults to 3000 milliseconds. |
std::vector< uint8_t > EthernetDevice::readSdo | ( | uint16_t | index, |
uint8_t | subindex, | ||
const std::chrono::steady_clock::duration | expiryTime = std::chrono::milliseconds(1000) |
||
) |
Reads an SDO (Service Data Object) from an Ethernet device.
This function sends a request to read an SDO at a specified index and subindex, then receives and parses the response. If the response indicates a failure, it logs an error and returns an empty vector. Otherwise, it returns the SDO data.
index | The index of the SDO to read. |
subindex | The subindex of the SDO to read. |
expiryTime | The duration to wait for the socket operation (read/write) before timing out. Defaults to 1000 milliseconds. |
boost::system::system_error | if sending or receiving fails. |
std::runtime_error | if the response buffer is too small to parse. |
bool EthernetDevice::removeFile | ( | const std::string & | filename, |
const std::chrono::steady_clock::duration | expiryTime = std::chrono::milliseconds(3000) |
||
) |
Removes a file from the device.
Sends a file removal request via Ethernet by attempting to read the file with a special prefix (fs-remove=
). It verifies that the response content begins with the expected success message.
filename | The name of the file to be removed. |
expiryTime | The duration to wait for the socket operation (read/write) before timing out. Defaults to 3000 milliseconds. |
true
if the file was successfully removed, false
otherwise. std::vector< uint8_t > EthernetDevice::sendAndReceiveProcessData | ( | const std::vector< uint8_t > & | data, |
const std::chrono::steady_clock::duration | expiryTime = std::chrono::milliseconds(1000) |
||
) |
Sends process data to a remote device and receives the response.
Sends a process data message over Ethernet to the remote device and waits for a corresponding response. This method increments the sequence ID, constructs and serializes the request message, transmits it via the socket, and parses the received response.
data | The payload data to transmit to the device. |
expiryTime | The duration to wait for the socket operation (read/write) before timing out. Defaults to 1000 milliseconds. |
|
overridevirtual |
Sends a state control command to the Ethernet device and checks the response.
This method constructs a STATE_CONTROL
type EthernetMessage
with the given state, sends it over the socket, and waits for a response. It then parses the received message and returns true
if the operation was acknowledged with OK
status.
state | The new state to set on the remote device. |
expiryTime | The duration to wait for the socket operation (read/write) before timing out. Defaults to 3000 milliseconds. |
true
if the response status is OK
, false
otherwise.boost::system::system_error | if sending or receiving fails. |
std::runtime_error | if the response buffer is too small to parse. |
Implements common::Device.
bool EthernetDevice::triggerFirmwareUpdate | ( | const std::chrono::steady_clock::duration | expiryTime = std::chrono::milliseconds(2000) | ) |
Sends a firmware update request to the connected Integro device.
Constructs and transmits a firmware update request using the custom Ethernet communication protocol. Waits for the device's response and verifies whether the request was successfully acknowledged.
This request must be issued after uploading one or both of the following files:
expiryTime | The duration to wait for the socket operation (read/write) before timing out. Defaults to 2000 milliseconds. |
|
overridevirtual |
Uploads a parameter from the device via SDO and updates the local parameter store.
This function reads a parameter value from the device using SDO communication, updates the corresponding local Parameter
object with the received value, and returns a reference to it.
index | The 16-bit parameter index in the object dictionary. |
subindex | The 8-bit subindex of the parameter. |
expiryTime | The maximum duration to wait for the SDO upload operation to complete. |
common::Parameter
object.std::runtime_error | If the upload fails or returns an empty payload. |
Implements common::Device.
|
inlinevirtual |
Uploads a parameter from the device and returns its value as the specified type.
This templated overload uploads a parameter using the given index and subindex, updates the corresponding parameter in the local store, and returns its value as type T
.
T | The type to extract from the uploaded parameter value. |
index | The 16-bit index of the parameter in the object dictionary. |
subindex | The 8-bit subindex of the parameter. |
expiryTime | The maximum duration to wait for the SDO upload operation. Defaults to 5000 milliseconds. |
T
.std::runtime_error | If the SDO upload fails or the value cannot be extracted as type T . |
std::bad_variant_access | If the internal variant does not hold the requested type T . |
Implements common::Device.
|
overridevirtual |
Sends a file in chunks to the remote server via Ethernet.
This function splits the file data into smaller chunks and sends them to the server in multiple segments. The file name is sent as the first segment, followed by the file data in subsequent chunks.
The server's response to each chunk is parsed, and the next chunk is sent until the entire file is transmitted.
filename | The name of the file to be written. |
data | The data to be written to the file. |
expiryTime | The duration to wait for the socket operation (read/write) before timing out. Defaults to 5000 milliseconds. |
Implements common::Device.
bool EthernetDevice::writeSdo | ( | uint16_t | index, |
uint8_t | subindex, | ||
const std::vector< uint8_t > & | data, | ||
const std::chrono::steady_clock::duration | expiryTime = std::chrono::milliseconds(1000) |
||
) |
Writes an SDO (Service Data Object) to an Ethernet device.
This function sends a request to write an SDO to a specified index and subindex, along with the provided data. It then receives and parses the response. If the response indicates an error, it logs an error message and returns false
. Otherwise, it returns true
to indicate that the write operation was successful.
index | The index of the SDO to write. |
subindex | The subindex of the SDO to write. |
value | A vector containing the data to be written to the SDO. |
expiryTime | The duration to wait for the socket operation (read/write) before timing out. Defaults to 1000 milliseconds. |
true
if the write operation was successful, false
otherwise.boost::system::system_error | if sending or receiving fails. |
std::runtime_error | if the response buffer is too small to parse. |