rM2M_Pack() function

Hi rapidM2M Community!

I don’t exactly understand how the rM2M_Pack() function implemented in the demo application works or why it is necessary to use it in the context of data recording. Can somebody help me with this?

Thanks

Hi @MaFi_mte19

The function “rM2M_Pack()” helps the programmer to convert the different datatyps correctly for the “rM2M_RecData()” function.
Furthermore has the function a return value, which return if the process was successful.

The function needs 4 parameter:
data: Array with the packed content
pos: Byte offset within the array
value: Value to pack
type: Configuration flags for the function

If you looking for more information about rM2M functions take a look at: https://support.microtronics.com/rapidM2M-webhelp/

Hello @MaFi_mte19,

The “rM2M_Pack()” function is designed to pack variables with different types into a byte array as well as unpacks the data back into different types of variables (see 00_common_4_pack.p).

As @ThAm_Microtronics has already mentioned, it can be used to pack the measurement data into an array, which is then transferred to the “rM2M_RecData()” function.

//Temporary memory in which the data record to be saved is compiled.
new aRecData{HISTDATA_SIZE};

/* Compile the data record to be saved in the “aRecData” temporary memory
- The first byte (position 0 in the “aRecData” array) is set to 0 so that the server copies
the data record into measurement data channel 0 upon receipt, as specified during the design
of the connector
- The battery voltage is copied to position 1-2. Data type: s16
- The input voltage is copied to position 3-4. Data type: s16
- The temperature is copied to position 5-6. Data type: s16
- The humidity is copied to position 7-8. Data type: s16
- The GSM Level is copied to position 9. Data type: s8 */
aRecData{0} = 0; // “Split-tag”
rM2M_Pack(aRecData, 1, iBattVIn, RM2M_PACK_BE + RM2M_PACK_S16);
rM2M_Pack(aRecData, 3, iVIn, RM2M_PACK_BE + RM2M_PACK_S16);
rM2M_Pack(aRecData, 5, iTemperature, RM2M_PACK_BE + RM2M_PACK_S16);
rM2M_Pack(aRecData, 7, iHumidity, RM2M_PACK_BE + RM2M_PACK_S16);
rM2M_Pack(aRecData, 9, iGSM_Level, RM2M_PACK_BE + RM2M_PACK_S8);

//Transfer compounded data record to the system to be recorded
rM2M_RecData(0, aRecData, HISTDATA_SIZE);

PAWN is a typeless language. All variables are numeric cells that can hold a signed 32bit integer value. The size of a cell therefore is 4 Byte. If you create variable using the keyword “new”(e.g. new iBattVIn) you always get a 32Bit signed integer. To reduce the required memory for storing the measurement Data and also reduce the data volume when transmitting the Data in the example above a 8Bit signed integer should be used for “iGSM_Level” and 16Bit singed integer should be used for all other variables. The “rM2M_Pack()” function ensures that the variables are correctly transformed from 32Bit signed integer into the desired data type and copied to the correct position within the array. It also pays attention to the endian.

For the last call of the “rM2M_Pack()” function in the example above, this means:
-The s32 type variable “iGSM_Level” is converted to s8 (specified by “RM2M_PACK_S8”)
-The converted variable is stored in the “big endian” format (specified by “M2M_PACK_BE”, not really relevant for a s8 data type )
-The converted variable is copied to the byte with the index 9 of the “aRecData” array.

For more information regarding the “rM2M_Pack()” function see Encoding

One last note: Although the link specified by @ThAm_Microtronics is still available it is recommended to use the new developer documentation (see Developer Documentation)