DataBuffer
Sorry, nothing useful here yet :(
Description
DataBuffer is useful for managing a bunch of data that needs to be appended. It allows data to be appended without recalculating current length each time.
Use Cases
- Concatentating several strings efficiently
- Concatentating data that is not a String
Overview
Usage
Declaration
Before using a data buffer variable must be declared (datbufBuffer_typ). For all future examples assume the following code,
datbufBuffer_typ myBuf;
Initialization
After declaring a data buffer it must be initalized before usage. This can be done using the datbufInitBuffer function. It is recommended this is done in the init routine but not required to be.
C
// In the init routine
status = datbufInitBuffer(&myBuf, 1000);
// In the cyclic routine
if(!status && !myBuf.pData) {
status = datbufInitBuffer(&myBuf, 1000);
}
ST
// In the init routine
status := datbufInitBuffer(ADR(myBuf), 1000);
// In the cyclic routine
IF (status <> 0) AND (myBuf.pData <> 0) THEN
status := datbufInitBuffer(ADR(myBuf), 1000);
END_IF
Example
C
// Generate string with all messages
if(newMessage) {
datbufClearBuffer(&myBuf);
for(i = 0; i < numMessages; i++) {
status = datbufAppendToBuffer(&myBuf, &message[i], strlen(message[i]));
if(!status) {
// Handle Error
}
}
}
ST
// Generate string with all messages
IF (newMessage) THEN
datbufClearBuffer(ADR(myBuf));
FOR i = 0 TO numMessages-1 DO
status = datbufAppendToBuffer(ADR(myBuf), ADR(message[i]), brsstrlen(ADR(message[i])));
IF (status <> 0) THEN
// Handle Error
END_IF
END_FOR
END_IF
API
datbufBuffer_typ
Direction | Name | Variable Type | Description |
---|---|---|---|
Out | pData | UDINT | Pointer to buffer storage containing concateted data |
Out | currentLength | UDINT | Current length of data in buffer in bytes |
Out | maxLength | UDINT | Max Length of data in buffer in bytes |
DATBUF_ERR_enum
Value | Name | Description |
---|---|---|
50000 | DATBUF_ERR_INVALIDINPUT | Invalid parameters given to function |
50001 | DATBUF_ERR_MEMALLOC | Memory allocation failed |
50002 | DATBUF_ERR_NOTINITALIZED | Buffer was not initialized |
50002 | DATBUF_ERR_BUFFERFULL | Buffer did not have enough space to fit data |
datbufInitBuffer
Direction | Name | Variable Type | Description |
---|---|---|---|
In | pBuffer | UDINT | Pointer to databuffer object to initialize |
In | maxLength | UDINT | Size of internal buffer data storage |
Return | errorId | UDINT | Error number from function call. 0 is success. See DATBUF_ERR_enum |
Possible errors are: DATBUF_ERR_INVALIDINPUT, DATBUF_ERR_MEMALLOC, DATBUF_ERR_NOTINITALIZED
datbufAppendToBuffer
Direction | Name | Variable Type | Description |
---|---|---|---|
In | pBuffer | UDINT | Pointer to databuffer object to initialize |
In | pData | UDINT | Pointer to data to be appended. Note: data can by any type |
In | dataLength | UDINT | Size of data in pData to be appended |
Return | errorId | UDINT | Error number from function call. 0 is success. See DATBUF_ERR_enum |
Possible errors are: DATBUF_ERR_INVALIDINPUT, DATBUF_ERR_NOTINITALIZED, DATBUF_ERR_BUFFERFULL
datbufClearBuffer
Direction | Name | Variable Type | Description |
---|---|---|---|
In | pBuffer | UDINT | Pointer to databuffer object to initialize |
Return | errorId | UDINT | Error number from function call. 0 is success. See DATBUF_ERR_enum |
Possible errors are: DATBUF_ERR_INVALIDINPUT, DATBUF_ERR_NOTINITALIZED