Colway Solutions
Modbus Member Logo

Using the Data Formatter to map 'C' data types to Modbus

 

MSPL-C provides you an extension to the Modbus specifications by supplying a set of functions in file CSPL_Utils.c that map the low level Modbus types (bits and words) to high level 'C' data types (floats, integers and strings) with due consideration to the ENDIAN format of your platform.

 

There are two categories of functions:

-       Functions that convert an array of raw data bytes as received via Modbus to an array of higher level 'C' data type. They are usually called in MSPL_WriteUserData to interpret the raw Modbus data as per the corresponding higher level 'C' datatype of the user database.

-       Functions that convert an array of some higher level 'C' data type into an array of raw data bytes that can be transmitted via Modbus. They are usually called in MSPL_ReadUserData to provide the library with user data in a Modbus compliant format.

 

Following is a brief description of each function:

Function name

Description

CSPL_PackBits

This function bit-packs a destination buffer with bit status information provided in a source buffer. The source buffer is expected to contain bit status (i.e a value of 0 or 1) in one byte per bit. This data is bit-packed as 8-bits per byte in the destination buffer. For e.g. if the input buffer is of this type:

CSPL_U8 srcBuffer[] = {0,1,1,0,1,1,0,0}

then the destination buffer's first element will be stuffed with the following value - 0x6C;

CSPL_UnPackBits

This function extracts bit status information from the source buffer by unpacking the bits and copies it to the destination buffer. The source buffer is expected to have bit packed data with one byte holding the status of 8 bits. For e.g. if the if the input buffer is of this type:

CSPL_U8 srcBuffer[]={0xF1, 0xAB}

then this function will unpack this data to create the following in the destination buffer

CSPL_U8 dstBuffer[]={1,1,1,1, 0,0,0,1, 1,0,1,0, 1,0,1,1}

CSPL_16BitIntsToBuffer

This function will copy data from a short integer array (i.e. two byte integer) to an array of single byte values.

CSPL_32BitIntsToBuffer

This function is similar to CSPL_16BitIntsToBuffer with the difference that it operates on 4-byte integer array.

CSPL_FloatsToBuffer

This function is similar to CSPL_16BitIntsToBuffer with the difference that it operates on a floating point array. Each floating point value is represented by four bytes in the destination buffer.

CSPL_StringToBuffer

This function copies a 'C' string into an array of bytes including the terminating NULL character. It can be used send a character string from the user database to a Modbus master.

CSPL_BufferTo16BitInts

This function performs the reverse task as the CSPL_16BitIntsToBuffer function by mapping an array of bytes to a short integer array.

CSPL_BufferTo32BitInts

This function performs the reverse task as the CSPL_32BitIntsToBuffer function by mapping an array of bytes to a long integer (4-byte) array.

CSPL_BufferToFloats

This function performs the reverse task as the CSPL_FloatsToBuffer function by mapping an array of bytes to a floating point array

CSPL_BufferToString

This function performs the reverse task as the CSPL_StringToBuffer function by mapping an array of bytes to a 'C' string.

 

Back to Index

Privacy Policy Site Map FAQ Contact Us