Applicable SDK Version(s): 5.x.x+



Assuming you have a Buffer you want to work with:


openjaus::system::Buffer buffer(128); // Buffer is 128 bytes long


Get a reference to a BufferWriter when you want to write data to the Buffer.


openjaus::system::BufferWriter& buffWriter = buffer.getWriter();


Clear the BufferWriter to start writing to the front of the Buffer.


buffWriter.clear();


Use the BufferWriter::pack method to write data to the Buffer. Every call to the pack method will increment the current write position of the BufferWriter such that data will be sequentially written into the Buffer.


uint64 bytesWritten = 0;

bytesWritten += buffWriter.pack((uint8)10); // Write an unsigned char value to the Buffer
bytesWritten += buffWriter.pack((uint16)10); // Write an unsigned short value to the Buffer
bytesWritten += buffWriter.pack((uint32)10); // Write an unsigned int value to the Buffer
bytesWritten += buffWriter.pack((uint64)10); // Write an unsigned long value to the Buffer

// bytesWritten = 1 + 2 + 4 + 8 = 15 bytes


Get a reference to a BufferReader when you want to read from the Buffer. 


openjaus::system::BufferReader& buffReader = buffer.getReader();


Reset the BufferReader to start reading from the front of the Buffer.


buffReader.reset();


Use the BufferReader::unpack method to read data from the Buffer. Every call to the unpack method will increment the current read position of the BufferReader such that data will be sequentially read from the Buffer.


uint64 bytesRead = 0;

uint8 ucharValue;
bytesRead += buffReader.unpack(ucharValue); // Read an unsigned byte from the Buffer

uint16 ushortValue;
bytesRead += buffReader.unpack(ushortValue); // Read an unsigned short from the Buffer

uint32 uintValue;
bytesRead += buffReader.unpack(uintValue); // Read an unsigned integer from the Buffer

uint64 ulongValue;
bytesRead += buffReader.unpack(ulongValue); // Read an unsigned long from the Buffer

// bytesRead = 1 + 2 + 4 + 8 = 15


Use the BufferReader::peek method to read data from the Buffer without incrementing the current read position of the BufferReader. 


uint64 bytesRead = 0;

uint8 ucharValue1;
bytesRead += buffReader.peek(ucharValue1); // Peek an unsigned byte from the Buffer

uint8 ucharValue2;
bytesRead += buffReader.peek(ucharValue2); // Peek the same unsigned byte from the Buffer

// bytesRead = 1 + 1 = 2
// ucharValue1 == ucharValue2