Functions

All functions take the buffer by address in pBuffer and return a UINT status: 0 on success, otherwise a DATBUF_ERR_enum value.

datbufInitBuffer

Initializes a buffer. All members of the buffer are reset, maxLength bytes of memory are allocated for the data with TMP_alloc, and the new memory is zeroed.

Direction Name Type Description
In pBuffer UDINT Address of the datbufBuffer_typ to initialize
In maxLength UDINT Size of the data storage to allocate, in bytes. Must be greater than 0.
Return status UINT 0 on success, otherwise a DATBUF_ERR_enum value
Error Cause
DATBUF_ERR_INVALIDINPUT pBuffer is 0, or maxLength is 0
DATBUF_ERR_MEMALLOC The memory could not be allocated

Call this function once per buffer, in the _INIT routine. TMP_alloc may only be called in _INIT or _EXIT, so this function must not be called cyclically. The library does not free the allocated memory, so calling it again on a buffer that is already initialized allocates a new block and the old one stays allocated until the controller restarts. B&R notes that allocations larger than about 70,000 bytes can cause cycle time violations in task class 1.

status = datbufInitBuffer((UDINT)&myBuf, 1000);

datbufAppendToBuffer

Copies dataLength bytes from pData to the end of the buffer’s current data and adds dataLength to currentLength.

Direction Name Type Description
In pBuffer UDINT Address of the initialized datbufBuffer_typ to append to
In pData UDINT Address of the data to append. The data can be of any type.
In dataLength UDINT Number of bytes to append from pData
Return status UINT 0 on success, otherwise a DATBUF_ERR_enum value
Error Cause
DATBUF_ERR_INVALIDINPUT pBuffer is 0, or pData is 0
DATBUF_ERR_NOTINITIALIZED pData is 0: the buffer has not been initialized with datbufInitBuffer
DATBUF_ERR_BUFFERFULL The data did not fit in the remaining space. The part that fit was appended.

If currentLength + dataLength exceeds maxLength, only the first maxLength - currentLength bytes are copied, currentLength becomes maxLength, and the function returns DATBUF_ERR_BUFFERFULL. No null terminator is written; see Working with Strings.

status = datbufAppendToBuffer((UDINT)&myBuf, (UDINT)myString, strlen(myString));

datbufClearBuffer

Empties the buffer: currentLength is set to 0 and all maxLength bytes of the data storage are set to 0. The allocated memory is kept for reuse.

Direction Name Type Description
In pBuffer UDINT Address of the initialized datbufBuffer_typ to clear
Return status UINT 0 on success, otherwise a DATBUF_ERR_enum value
Error Cause
DATBUF_ERR_INVALIDINPUT pBuffer is 0
DATBUF_ERR_NOTINITIALIZED pData is 0: the buffer has not been initialized with datbufInitBuffer

Clearing zeroes the entire storage, not only the part in use, so its execution time grows with maxLength.

status = datbufClearBuffer((UDINT)&myBuf);