Teste semf



A DataStream is a binary stream of encoded information which is 100% independent of the host computers‘ operating system, CPU or byte order. For example, a DataStream that is written by a e.g. ARM microcontroller can be read by a PC under e.g. Windows or Ubuntu.

You can also use a DataStream to read or write raw unencoded binary data.

The DataStream class implements the serialization of C++’s basic data types, like int8_t … int32_t, uint8_t … uint32_t, bool, float, double.

In the following example we assume you want to send data from a microcontroller through amy communication hardware to a computer.

Microcontroller as transmitter

For using a DataStrem object, you need a working buffer for data handling.

// Size of the buffer to transmit
const size_t dataStreamBufferSize = 16;  
// Buffer to transmit later
uint8_t dataStreamBuffer[dataStreamBufferSize] = {0};  
// Data stream object initialized with the transmit buffer
semf::DataStream dataStream(dataStreamBuffer, dataStreamBufferSize);

Now we create the data to transceive later.

uint8_t byte = 0x10;  // Example byte
int16_t shortint = 0x1234;  // Example short int
const size_t arraySize = 10;  // Size of binary array

// Binary array (will not be shifted by DataStream class, you have to take care about)
uint8_t array[arraySize] = {0};  
for (uint8_t i = 0; i < arraySize; i++)
  array[i] = i;  // Fill binary array

By the following lines, the transmit buffer will be filled.

// resets the pointer for write operation to the 
// first element of the dataStreamBuffer.
dataStream << byte;  // write byte to the buffer
dataStream << shortint;  // write short int to the buffer
dataStream.writeRawData(array, arraySize); // write raw byte array to the buffer

// ...
// write data via any communication hardware you prefer
// ...

Computer as receiver

For using a DataStrem object, you need a working buffer for data handling.

// Size of the buffer to read int
const size_t dataStreamBufferSize = 16;  
// Buffer to read into 
uint8_t dataStreamBuffer[dataStreamBufferSize] = {0};  
// Data stream object initialized with the read buffer
semf::DataStream dataStream(dataStreamBuffer, dataStreamBufferSize);  
// ...
uint8_t byte;
int16_t shortint;
const size_t arraySize = 10;
uint8_t array[arraySize] = {0};

The following function is called, by estimated data has read by the computer.

void onDataAvailable()
  // resets the pointer for read operation to the 
  // first element of the dataStreamBuffer
  // read byte from the buffer
  dataStream >> byte;  
  // read short int from the buffer
  dataStream >> shortint;  
  // write raw byte array to the buffer
  dataStream.readRawData(array, arraySize); 

  printf("%02X\n", byte);
  printf("%04X\n", shortint);
  for (uint8_t i=0; i < arraySize; i++)
    printf("%02X\n", array[i]);



zurück zur Dokumentation