Overview

In this first example, we’ll write the string “Hello, world!” to the Scratch Pad area of the OptoMMP memory map.

This example assumes you have basic familiarity with creating and running an application in the CODESYS Development System.

Step 1 - Determine the OptoMMP Address to Use

The “Hello, world!” string will be written to the Scratch Pad String Area. The Scratch Pad is a feature of the I/O services. It’s a convenient and central place for different programs to read and write data.

The correct OptoMMP memory map address must be determined. This can accomplished in two ways.

Method 1 - OptoMMP Protocol Guide

The OptoMMP Protocol Guide (form 1465) contains a full reference for the OptoMMP memory map.

The Scratch Pad area is defined in “Appendix A: SCRATCH PAD—READ/WRITE”.

OptoMMP Reference

We’ll use element 0, and we need the address of the string data, not the length. According the table, the memory address is 0xF0D83002.

Method 2 - MMP Calculator in groov Manage

groov Manage on the groov EPIC processors contains a convenient tool called MMP Calculator for finding and calculcating OptoMMP memory map addresses.

  1. In groov Manage, click Home > I/O > I/O Services > MMP Calculator.
  2. For Area, select Scratch Pad - String.
  3. For Element Index, select 0.

Like the document in Method 1, it shows that the Opto MMP memory address is 0xF0D83002.

CODESYS MMP Example

Step 2 - Add Opto 22 Library

  1. Create or open an application within the CODESYS Development System.
  2. Make sure the Opto 22 Library, version 3.0.0.0 or later, is added to the Library Manager.

    Library Manager

Step 3 - Add POU and Variables

This example needs one Program (POU) and three variables.

  1. Create a new POU named MMP_HELLO_WORLD_CFC with Continuous Function Chart (CFC) as the implementation language.
  2. Add the new program to the Task Configuration.
  3. Add the following variables to the Declaration area of the MMP_HELLO_WORLD_CFC program.

    PROGRAM MMP_HELLO_WORLD_CFC
    VAR
        sMsg : STRING := 'Hello, world!';
    
        mmpClient:           OPTO.MmpClient;
        mmpClientWriteBlock: OPTO.MmpClientWriteBlock;
    END_VAR
    
  • sMsg is the STRING variable that will be written to OptoMMP Scratch Map.
  • mmpClient is the MmpClient function block that will manage the connection and communication with the internal OptoMMP server.
  • mmpClientWriteBlock is the MmpClientWriteBlock function block that will make the specific Write Block request.

    CODESYS MMP Example

Step 4 - Initialize the MmpClient Instance

  1. In the declaration editor, double-click the mmpClient variable and drag it into the implementation area.
  2. A few of the inputs need to be set.
    1. Assign TRUE to xConnect.
    2. Assign '127.0.0.1' to sAddress.
  3. The other inputs can be left at their default values.

    CODESYS MMP Example

Step 5 - Initialize the MmpClientWriteBlock Instance

  1. In the declaration editor, double-click the mmpClientWriteBlock variable and drag it into the implementation area.
  2. A few of the inputs need to be set.
    1. Assign 16#F0D83002 to udiMmpAddress.
      • This is the OptoMMP memory map address that was determined in Step 1.
    2. Assign mmpClient to rClient.
      • This is a reference to the client.
    3. Assign ADR(sMsg) to pData.
      • pData is of type POINTER TO BYTE, and is a pointer to the block of data to be written. In this case, we need the memory address of the string value. The ADR operator is used to get the address of the string’s content.
    4. Assign TO_UINT(LEN(sMsg)) to uiCount.
      • The uiCount input needs the number of bytes to be written. This can be calculcated with the LEN() funtion. LEN() return a UDINT, which must be converted to a UINT with the TO_UINT operator.
    5. Assign TRUE to bSkipByteSwap.

    CODESYS MMP Example

Step 6 - Connect the Function Blocks

This example is very simple. Once the client is connected, the string value will be written one time.

This can be accomplished by simply connecting the xConnected output from the client to the xExecute input of the write request.

CODESYS MMP Example

Step 7 - Run and View Results

Before running the application, use groov Manage to view the OptoMMP memory map address.

  1. In groov Manage, navigate to Home > I/O > I/O Services > Generic MMP.
  2. For Address, enter 0xF0D83002.
  3. For Data Type, select String.
  4. For Encoding, select ASCII.
  5. For Length, enter 20.

    CODESYS MMP Example

Back in CODESYS:

  1. Go into Online mode.
  2. Run the application.

If all goes well, there should be a blue line between the client’s xConnected output and the request block’s xExecute input, and the xDone output should be TRUE.

CODESYS MMP Example

Back in the Generic MMP page in groov Manage, the Scratch Pad string element should show the ‘Hello, world!’ message.

CODESYS MMP Example

Ladder Logic Version

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

CODESYS MMP Example

To keep the code clean, two network rungs were used. There’s still a connection between client’s xConnected output and the xExecute input of the write request.

Structured Text Version

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

CODESYS MMP Example

There’s still a connection between client’s xConnected output and the xExecute input of the write request.

Next Step

Continue on to the Writing an Integer example.