Configuring Structured Text Sensor ID Mapping Defaults

Jason IP2 min read
Other ManufacturerOther TopicPLC Programming
Licensed PE Working through this on a live machine? A Maine-licensed engineer can take it from here — included with IMD hardware, by the hour for everything else. Book an engineer

Map each Modbus-read sensor ID to its factory serial number and assign a default string before evaluating the lookup. The default remains active whenever the input ID has no matching entry.

Define the lookup behavior

The function block input is a DWORD, while the factory serial number is a STRING. The confirmed mappings are:

Input sensor ID Factory serial number
10091474 52580
10091464 52585
10090645 52597
10089558 52599
10089559 52604

Assign the default before the IF statements

Write the unknown-ID result at the start of every execution, then overwrite it only when a comparison succeeds. This prevents the output from retaining a serial number produced during an earlier execution.

FactorySerial := 'Not in list';

IF Input = 10091474 THEN
    FactorySerial := '52580';
END_IF;
IF Input = 10091464 THEN
    FactorySerial := '52585';
END_IF;
IF Input = 10090645 THEN
    FactorySerial := '52597';
END_IF;
IF Input = 10089558 THEN
    FactorySerial := '52599';
END_IF;
IF Input = 10089559 THEN
    FactorySerial := '52604';
END_IF;

This is the smallest correction to the existing logic. If none of the comparisons evaluates true, FactorySerial remains 'Not in list'.

Scale the mapping with a constant structure array

For approximately 150 mappings, the evidence also supports storing input and serial values together in a constant array, scanning it with a FOR loop, and leaving the loop with EXIT after a match. The demonstrated declaration syntax applies to CoDeSys 2.3; confirm that the target ST environment supports constant arrays of structures before adopting it.

  1. Define a structure containing an input value and its corresponding serial string.
  2. Declare the mapping array and a constant representing its element count.
  3. Set the output to 'Not in list' before starting the loop.
  4. Compare each array entry with the input, copy the matching serial number, and execute EXIT.
  5. When adding mappings, update both the array contents and its declared element count.

Verify known and unknown IDs

Test at least one known value, such as 10091474, and confirm that the output becomes '52580'. Then apply an ID absent from the table and confirm that the output becomes 'Not in list'. Finally, change from a known ID to an unknown ID during consecutive executions; the output must change to the default instead of retaining the previous serial number.

FAQ

How do I return “Not in list” for an unknown ID in Structured Text?

Assign FactorySerial := 'Not in list'; before the lookup statements. A matching comparison then overwrites that default.

Why does the serial-number output retain the previous sensor value?

The output was not reset before the lookup. Assign the default during every execution so an unmatched ID cannot leave the earlier result in place.

How should I maintain 150 sensor ID mappings in ST?

If the target environment supports it, place the ID and serial pairs in a constant array of structures and search it with a FOR loop. Use EXIT immediately after finding a match.

Back to blog