-
-
Notifications
You must be signed in to change notification settings - Fork 17
Communication
The Yadoms shared library provides a set of classes to help your plugin to use a communication port.
An asynchronous port is responsible to establish the connection, and exchange data with the material. It notifies the CEventHandler object of the plugin when connection is established or lost. It transfers received data to an IReceiveBufferHandler object, which notifies the CEventHadler object of the plugin. Data is sent synchronously.
The receive buffer handler manages received data to transmit to plugin only valid messages. Some buffer handlers are already provided by yPluginApi (for fixed message size, for messages with special character ending...). If none of provided handlers meet your need (for example : size of the message is in the first byte of a frame), you will have to create your own. A receive buffer handler have to implement the IReceiveBufferHandler interface. When the condition of a complete message is met, the handler should notify the CEventHandler object of the plugin. See code of the Rfxcom plugin for a custom receive buffer handler example.
First, we have to instantiate the port (here a serial port) :
boost::shared_ptr<shared::communication::IAsyncPort> port(boost::make_shared<shared::communication::CAsyncSerialPort>("COM1", boost::asio::serial_port_base::baud_rate(9600)));
Next, subscribe to connect/disconnect events :
port->subscribeForConnectionEvents(pluginEventHandler, kEvtPortConnection);
And instantiate our buffer handler (here, for 10 bytes length messages) :
boost::shared_ptr<shared::communication::IReceiveBufferHandler> receiveBufferHandler(boost::make_shared<CFixedSizeReceiveBufferHandler>(pluginEventHandler, kEvtPortDataReceived, 10));
port->setReceiveBufferHandler(receiveBufferHandler);
Finally, start the port :
port->start();
Next, in the plugin message loop, handle the 2 event types :
while(1)
{
switch(api->getEventHandler().waitForEvents())
{
case kEvtPortConnection:
if (api->getEventHandler().getEventData<bool>())
... Process connect event ...
else
... Process disconnect event ...
break;
case kEvtPortDataReceived:
const shared::communication::CByteBuffer message = api->getEventHandler().getEventData<const shared::communication::CByteBuffer>();
... Process received message ...
break;
... Other events ...
}
}
Yadoms -- The ultimate house automation solution