StreamProtocol
General
The StreamProtocol ensures the correctness while exchanging data between different systems. For example between a microcontroller and a computer. The protocol detects the begin of a message, checks the lengths and converts automaticly the data to big endian or little endian. The data can be checked by hardware or software Crc to ensure the correctness.
The protocol’s format:

The StreamProtocol doesn’t know the binary format of the payload to send or receive. For having the payload in the correct endian format, use the DataStream class.
In the following example we initialize the StreamProtocol for a STM32F7, send and receive strctured data with 19 bytes length.
Initialization
#include "HardwareAbstraction/Stm32F7/stm32f7uart.h" #include "Processing/datastream.h" #include "Communication/Protocols/streamprotocol.h" extern UART_HandleTypeDef huart3; // Communication hardware, e.g. UART semf::Stm32F7Uart uart(huart3); // Buffer for storing received data in. uint8_t readBuffer[19]; // Endian setting, used for the data on the UART bus semf::DataStream::Endian endianSetting = semf::DataStream::Endian::ENDIAN_LITTLE; // hardware and software CRC supported semf::Crc32Software crc; semf::StreamProtocol streamProtocol( uart, /* Communicating through UART */ readBuffer, /* Buffering received data in */ sizeof(readBuffer), /* Size of receive buffer */ endianSetting, /* BIG or LITTE endian setting possible */ crc); /* User specific CRC can be used */ // DataStream for structured read data information semf::DataStream writeDataStream( readBuffer, // buffer, same as for the StreamProtocol sizeof(readBuffer), // buffer size semf::DataStream::Endian::ENDIAN_LITTLE); // User endian setting // DataStream for structured write data information uint8_t writeBuffer[19]; semf::DataStream readDataStream( writeBuffer, // buffer sizeof(writeBuffer), // buffer size semf::DataStream::Endian::ENDIAN_LITTLE); // User endian setting
Write Operation
// Prepare payload by using DataStream uint32_t value_1 = 10; int16_t value_2 = 0x1234; char value_3 = 'E'; double value_4 = 4.8f; float value_5 = 1000.0; writeDataStream.resetWrite(); writeDataStream << value_1; writeDataStream << value_2; writeDataStream << value_3; writeDataStream << value_4; writeDataStream << value_5; // Slot for entering after data has been written void onDataWritten() { // do something } streamProtocol.dataWritten.connect(&onDataWritten); // Now write the data streamProtocol.write(writeBuffer, sizeof(writeBuffer));
Read Operation
For asynchronous and synchronous slave busses, the startRead() function has to be called before receiving anything and after every emitted dataAvailable signal. In the following example we restart the read operation in the slot which is called by the dataAvailable Signal.
// Slot for entering after data has been read void onDataAvailable() { // Read out received data readDataStream.resetRead(); readDataStream >> value_1; readDataStream >> value_2; readDataStream >> value_3; readDataStream >> value_4; readDataStream >> value_5; // do something streamProtocol.startRead(); } streamProtocol.dataAvailable.connect(&onDataAvailable); // Now go into idle receive mode for an asynchronous bus like UART streamProtocol.startRead();