Overview

In this third example, we’ll read an array of integer values from the Scratch Pad area of the OptoMMP memory map.

The goals of this example are:

  • The client will connect and stay connected if there are any errors.
  • Five elements of the Integer 32 Scratch Pad will be read into an integer array.
  • The read requests will trigger five seconds after the previous one is finished.

This example assumes you have basic familiarity with creating and running an application in the CODESYS Development System. It also assumes you’ve read the Hello, world! and Write an Integer examples.

Continuous Function Chart

The variables for this example are:

PROGRAM MMP_READ_SCRATCH_PAD
VAR
    mmpClient:     OPTO.MmpClient;
    mmpReadBlock:  OPTO.MmpClientReadBlock;
    audiValues:    ARRAY[0..5] OF UDINT;
    tonReadDelay:  TON;
END_VAR

The implementation is quite similar to the previous examples:

CODESYS MMP Example

Important details:

  1. The memory address of where the data will be stored must be passed into the pData input. The ADR operator is used to get the memory address of the audiValues array of integers.
  2. The number of bytes to read is passed into the uiCount input. The SIZEOF operator can be used to get the number of bytes used for a variable’s storage.
  3. Like with the Write Integer example, the xBusy output of the request block is negated and wired into the xExecute input.
  4. To limit how often the values are updated, a TON turn-on delay function block is used to between the negated xBusy output and the xExecute input.
    1. When a request is triggered and the xBusy output turns TRUE, the value is negated to FALSE and the TON block will detect a falling edge on the IN input and reset the timer.
    2. When a request is finished and the xBusy output turns FALSE, the value is negated to TRUE and the TON block will detect a rising edge on the IN input and start the timer.

Ladder Logic

This example in Ladder Logic (LD) is very similar. Here’s one way it could be written:

CODESYS MMP Example

Structured Text

This example in Structure Text (ST) is very similar. Here’s one way it could be written:

CODESYS MMP Example