The Communication class provides access to microcontroller communication. More...
#include <communication.h>
Public Member Functions | |
Communication (const std::string &configPath) | |
Communication constructor. More... | |
void | connect () |
Tries to setup a connection to a serial devive with a certain device-tag and specified baudrate. More... | |
void | disconnect () |
Terminates a connection to a serial devive with a certain device-tag. More... | |
void | enableDebugMessages (debugCallbackPtr debug) |
Enable pass through of every line in the input buffer. More... | |
void | enableRawCommunication () |
Enables the transmission of raw messages to the serial device. More... | |
void | registerErrorCallback (debugCallbackPtr error) |
Register a callback to handle transmission/communication errors. More... | |
void | registerSensorGroupCallback (const unsigned char &grpNumber, responseCallback cbPtr) |
Register a callback to handle sensor group messages from the serial device. More... | |
bool | registerSensorGroups (const std::string &cmdName, unsigned int timeout=100000) |
Register all predefined sensor groups on the serial device. More... | |
void | registerTextCallback (debugCallbackPtr text) |
Register a callback to handle plain text messages from the serial device. More... | |
bool | sendCommand (const std::string &command, const Parameter::ParameterMap &inputParams, Parameter::ParameterMap &outputParams, unsigned int timeout=100000) |
Send a command to the servial device. More... | |
bool | sendCommand (const std::string &command, const std::vector< std::string > &options, const Parameter::ParameterMap &inputParams, Parameter::ParameterMap &outputParams, unsigned int timeout=100000) |
Send a command to the servial device with additional options. More... | |
void | sendRawMessage (const std::string &msg) |
Method to send a string directly to the serial device. More... | |
void | startCommunication () |
Start reading the serial input buffer and start parsing threads. More... | |
void | stopCommunication () |
Stop reading the serial input buffer and shutdown parsing threads. More... | |
~Communication () | |
Communication default destructor. More... | |
Private Member Functions | |
void | configSerialInterface () |
Configures the serial connection setup with the provided parameters. More... | |
Private Attributes | |
CommunicationConfig | comCfg |
std::unordered_map< std::string, std::shared_ptr< Command > > | commands |
std::condition_variable | cv |
ThreadDispatcher * | dispatcher |
std::mutex | mtx |
bool | rawCommunicationEnabled |
ReadingThread * | rxPolling |
std::unordered_map< unsigned char, std::shared_ptr< SensorGroup > > | sensorGroups |
SensorGroupThread * | sensorGroupThread |
std::shared_ptr< SerialInterfaceParams > | serialParams |
std::shared_ptr< Syntax > | syntax |
The Communication class provides access to microcontroller communication.
An instance of this Class is needed to gain access to a connected serial device (e.g. microcontroller). Member functions of this class are used to open and close a connection, as well as sending commands and receiving responses and sensory data.
To instantiate an object of this class, a valid path to a config folder is required. The folder must contain the following files: channels.yml, command_options.yml, commands.yml, gerneral_syntax.yml, sensor_groups.yml, serial_interface_config.yml
Definition at line 36 of file communication.h.
Communication::Communication | ( | const std::string & | configPath | ) |
Communication constructor.
[in] | configPath | path to the config folder containing all required config files |
std::exception |
Definition at line 9 of file communication.cpp.
Communication::~Communication | ( | ) |
Communication default destructor.
Definition at line 27 of file communication.cpp.
|
private |
Configures the serial connection setup with the provided parameters.
std::exception |
Definition at line 34 of file communication.cpp.
void Communication::connect | ( | ) |
Tries to setup a connection to a serial devive with a certain device-tag and specified baudrate.
std::exception |
Definition at line 44 of file communication.cpp.
void Communication::disconnect | ( | ) |
Terminates a connection to a serial devive with a certain device-tag.
std::exception |
Definition at line 54 of file communication.cpp.
void Communication::enableDebugMessages | ( | debugCallbackPtr | debug | ) |
Enable pass through of every line in the input buffer.
This will call the registered callback whenever there is a new line in the input buffer. The callback decides what to do with that information.
[in] | debug | Function pointer of the debug callback. |
std::exception |
Definition at line 60 of file communication.cpp.
void Communication::enableRawCommunication | ( | ) |
Enables the transmission of raw messages to the serial device.
std::exception |
Definition at line 66 of file communication.cpp.
void Communication::registerErrorCallback | ( | debugCallbackPtr | error | ) |
Register a callback to handle transmission/communication errors.
The registered function will be called whenever an error during transmission of an outgoing, or parsing of an incoming message occurs. The callback decides what to do with that information.
[in] | error | Function pointer of the error callback. |
std::exception |
Definition at line 168 of file communication.cpp.
void Communication::registerSensorGroupCallback | ( | const unsigned char & | grpNumber, |
responseCallback | cbPtr | ||
) |
Register a callback to handle sensor group messages from the serial device.
The registered function will be called whenever a sensor message with the registred group number has been received from the serial device. The callback decides what to do with that information.
[in] | grpNumber | The group number is the unique identifier of a sensor group. |
[in] | cbPtr | Function pointer of the sensor group callback. |
std::exception |
Definition at line 181 of file communication.cpp.
bool Communication::registerSensorGroups | ( | const std::string & | cmdName, |
unsigned int | timeout = 100000 |
||
) |
Register all predefined sensor groups on the serial device.
[in] | cmdName | unique identifier of the command that provides the functionality to create sensor groups on the serial device. |
[in] | timeout | how long to wait for a response in micro seconds (Default: 100000) |
std::exception |
Definition at line 140 of file communication.cpp.
void Communication::registerTextCallback | ( | debugCallbackPtr | text | ) |
Register a callback to handle plain text messages from the serial device.
The registered function will be called whenever a plain text message has been received from the serial device. The callback decides what to do with that information.
[in] | text | Function pointer of the text callback. |
std::exception |
Definition at line 176 of file communication.cpp.
bool Communication::sendCommand | ( | const std::string & | command, |
const Parameter::ParameterMap & | inputParams, | ||
Parameter::ParameterMap & | outputParams, | ||
unsigned int | timeout = 100000 |
||
) |
Send a command to the servial device.
Use this method to send a predefined command to the serial device. Parameters required in the command message need to be provided in the inputParams ParameterMap. Returned parameters from the response will be put in the provided outputParams ParameterMap.
[in] | command | unique identifier of the command |
[in] | inputParams | provided input parameter values |
[out] | outputParams | returned parameter values |
[in] | timeout | how long to wait for a response in micro seconds (Default: 100000) |
std::exception |
Definition at line 81 of file communication.cpp.
bool Communication::sendCommand | ( | const std::string & | command, |
const std::vector< std::string > & | options, | ||
const Parameter::ParameterMap & | inputParams, | ||
Parameter::ParameterMap & | outputParams, | ||
unsigned int | timeout = 100000 |
||
) |
Send a command to the servial device with additional options.
Use this method to send a predefined command to the serial device. By providing a list of unique command option identifiers this command may be modified according to these options. Parameters required in the command message need to be provided in the inputParams ParameterMap. Returned parameters from the response will be put in the provided outputParams ParameterMap.
[in] | command | unique identifier of the command |
[in] | options | list of unique command option identifiers |
[in] | inputParams | provided input parameter values |
[out] | outputParams | returned parameter values |
[in] | timeout | how long to wait for a response in micro seconds (Default: 100000) |
std::exception |
Definition at line 110 of file communication.cpp.
void Communication::sendRawMessage | ( | const std::string & | msg | ) |
Method to send a string directly to the serial device.
This will send the input string as is. If there is some kind of end-of-message-symbol required from the serial device, it will not be appended automatically.
[in] | msg | Input string to be transmitted |
std::exception |
Definition at line 71 of file communication.cpp.
void Communication::startCommunication | ( | ) |
Start reading the serial input buffer and start parsing threads.
std::exception |
Definition at line 50 of file communication.cpp.
void Communication::stopCommunication | ( | ) |
Stop reading the serial input buffer and shutdown parsing threads.
std::exception |
Definition at line 52 of file communication.cpp.
|
private |
Object that stores all given configuration parameters.
Definition at line 176 of file communication.h.
|
private |
List of predefined Command objects
Definition at line 179 of file communication.h.
|
private |
Condition variable.
Definition at line 186 of file communication.h.
|
private |
Thread object that manages multi threaded functionality.
Definition at line 181 of file communication.h.
|
mutableprivate |
Mutex object.
Definition at line 185 of file communication.h.
|
private |
is direct communication with the serial device enabled?
Definition at line 182 of file communication.h.
|
private |
Thread object which polls the serial input buffer.
Definition at line 183 of file communication.h.
|
private |
List of predefined SensorGroup objects
Definition at line 180 of file communication.h.
|
private |
Thread object which distributes sensor group messages.
Definition at line 184 of file communication.h.
|
private |
Object that stores serial connection parameters.
Definition at line 177 of file communication.h.
|
private |
Object that stores syntax parameters.
Definition at line 178 of file communication.h.