Symbolic Array / Workaround

Hello,
I try to solve following task:
For example I created in the DDE two blocks

#histdata1 measurements_sensors up
containing 10 channels (s16) named CH_01 to CH_10

and

#config7 Sensor_IDs down
containing 10 fields (astr.8) named with CH_ID_01 to CH_ID_10

In the DLO:
The IDs a stored in a global array static aSensor_IDs[DDE_Sensor_IDs]
Furthermore I receive messages like “ID: s009 / Value: 2340” via the UART.
The Task is to put the value from the Sensor with the ID 009 in the correct channel, which should be identified by the ID 009 in the config.

In detail the field CH_ID_03 may contain the Sensor ID “s009”. So the Value 2340 should be stored in the corresponding Channel CH_03.

My intention was to look for the ID in the config within a loop and store the current Index to write the value in the Channel of the Number [Index]
Is there a possibility to access the config ID within a loop like
for ( Idx) {
aSensor_IDs.CH_ID_ & Idx
}
for a comparison with the ID in the message?

Or is there any workaround to assign the value to the correct channel from a bunch of channels dependent on the ID?

Best regards,
Benjamin

Hello @Funk_Fuchs,

Instead of 10 individual variables you can also create an array with 10 elements in the Data Descriptor.

#histdata1 measurements_sensors up
channels[10] s16 

Unfortunately, this is not possible for Ascii strings (astr). This is due to the limitation to max. two-dimensional arrays in PAWN. Therefore you probably need to use 10 individual variables for the configuration

#config7 Sensor_IDs down
CH_ID_01 astr.8
CH_ID_02 astr.8
CH_ID_03 astr.8
CH_ID_04 astr.8
CH_ID_05 astr.8
CH_ID_06 astr.8
CH_ID_07 astr.8
CH_ID_08 astr.8
CH_ID_09 astr.8
CH_ID_10 astr.8

But to achieve your goal of scaning the configuration with a For loop, you could use the following trick:

When the firmware calls the onUplinkApply_Sensor_IDs function (i.e. a new configuration was received or the configuration is read from the flash at startup), you simply copy the individual variables specified in the Data Descriptor into a global array of 10 elements.

static aCurrent_Sensor_ID[10]{8+1};

onUplinkApply_Sensor_IDs()
{
  new iResult;
  new aReadCfg[DDE_Sensor_IDs];

  iResult = DDE_Sensor_IDs_read(aReadCfg);

  if(iResult == OK)                         // If the config is valid
  {
    aCurrent_Sensor_ID[0] = aReadCfg.CH_ID_01;
    aCurrent_Sensor_ID[1] = aReadCfg.CH_ID_02;
    aCurrent_Sensor_ID[2] = aReadCfg.CH_ID_03;
    aCurrent_Sensor_ID[3] = aReadCfg.CH_ID_04;
    aCurrent_Sensor_ID[4] = aReadCfg.CH_ID_05;
    aCurrent_Sensor_ID[5] = aReadCfg.CH_ID_06;
    aCurrent_Sensor_ID[6] = aReadCfg.CH_ID_07;
    aCurrent_Sensor_ID[7] = aReadCfg.CH_ID_08;
    aCurrent_Sensor_ID[8] = aReadCfg.CH_ID_09;
    aCurrent_Sensor_ID[9] = aReadCfg.CH_ID_10;
  }

  return iResult;
}

Note: In PAWN, arrays, if they have the same structure, can be copied directly with a “=”. A memcpy is not necessary.

At the point in the program where you need your For loop, you simply use the global array. Because the onUplinkApply_Sensor_IDs function is always called automatically when a new configuration is received from the rapidM2M portal, your global array is always up-to-date.

main() {
  new CH_x[DDE_measurements_sensors];
 
	for(new i=0;i<10;i++)
	{
		// If "aCurrent_Sensor_ID[i]{8+1}" = "ID: s009 / Value: 2340" then "CH_x.channels[i]=2340"
	}
}

Kind regards
Josef