Overview

In this second example, we’ll write an incrementing integer value to the Scratch Pad area of the OptoMMP memory map.

The goals of this example are:

  • The client will connect and attempt to reconnect if there are any errors.
  • The write requests will trigger as often as possible. Once a request’s response has been processed, the next one will be triggered.

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! example.

Step 1 - Determine the OptoMMP Address to Use

The value will be written to Element 0 of the Scratch Pad 32-bit Integer area.

The OptoMMP memory map address can be determined in two ways.

  1. In the “Appendix A: SCRATCH PAD—READ/WRITE” section of the OptoMMP Protocol Guide (form 1465).
  2. With the MMP Calculator tool in groov Manage. From the Home page, click I/O > I/O Services > MMP Calculator.

The OptoMMP memory map address is 0xF0D81000.

Step 2 - Create and Prepare Project

  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.
  3. Create a new POU named MMP_WRITE_INTEGER with Continuous Function Chart (CFC) as the implementation language.
  4. Add the new program to the Task Configuration.
  5. Add the following variables to the Declaration area of the MMP_WRITE_INTEGER program.

     PROGRAM MMP_WRITE_INTEGER
     VAR
         mmpClient:     OPTO.MmpClient;
         mmpWriteUDINT: OPTO.MmpClientWriteUDINT;
         udiVar :       UDINT;
     END_VAR
    
  6. Drag out a Box onto the CFC editor and assign it the ADD function.
  7. Configure the inputs and outputs like this:

    CODESYS MMP Example

    This will add 1 to udiVar every time the CFC program is called.

Step 3 - Initialize the MmpClient Instance

Often it’s desirable to keep a client connected as much as possible. This can be accomplished by taking the client’s xError output, negating it, and then feeding it back into the xConnect input.

  1. In the declaration editor, double-click the mmpClient variable and drag it into the implementation area.
  2. Assign '127.0.0.1' to sAddress.
  3. Connect the xError output to the xConnect input.
  4. Deselect the new connecing line.
  5. Right-click on the pin for xError and select Negate. This should add a small circle at the beginning of the connecting line leaving xError. Make sure there’s not another circle at the other end of the connecting line.

    CODESYS MMP Example

Connecting xError and xConnect in this way will enable the following behavior:

  • Initially, xError is FALSE, which causes xConnect to be TRUE and begin the connection process.
  • While connecting, xError remains FALSE and xConnect remains TRUE.
  • Once connected, xConnected becomes TRUE, xError remains FALSE, and xConnect remains TRUE.
  • If there’s an error (either while connecting or once connected), xError becomes TRUE which forces xConnect to FALSE.
    • On the next cycle, the process begins again. Due to the state change on xConnect, xError is cleared and goes back to FALSE, sending xConnect to TRUE again.

It may take one or more PLC cycles for any of the states changes to complete.

Step 4 - Initialize the MmpClientWriteUDINT Instance

  1. In the declaration editor, double-click the mmpWriteUDINT variable and drag it into the implementation area.
  2. A few of the inputs need to be set.
    1. Assign 1000000 to udiTimeOut.
    2. Assign 16#F0D81000 to udiMmpAddress.
      • This is the OptoMMP memory map address that was determined in Step 1.
    3. Assign mmpClient to rClient.
      • This is a reference to the client.

    CODESYS MMP Example

As stated in the Overview for this example, we want to write the udiVar value as often as possible. This can be accomplished by taking the MmpClientWriteUDINT block’s xBusy output, negating it, and then feeding it back into the xExecute input.

  1. Connect the xBusy output to the xExecute input.
  2. Deselect the new connecing line.
  3. Right-click on the pin for xBusy and select Negate. This should add a small circle at the beginning of the connecting line leaving xError. Make sure there’s not another circle at the other end of the connecting line.

    CODESYS MMP Example

Connecting xBusy and xExecute in this way will keep writing the value as often as possible.

  • Initially, xBusy is FALSE, which causes xExecute to be TRUE and begin the process.
  • While executing, xBusy becomes and stays TRUE and xExecute becomes FALSE. This does not stop the current request from being processed.
  • Once the response is done being processed, xBusy becomes FALSE, causing xExecute to become TRUE, thus starting the next request.
  • If there’s an error, xError becomes TRUE and xBusy becomes FALSE, which forces xExecute to TRUE, thus starting the next request.
    • Your real-world application might require more sophisticated error handling.
    • A common error would be caused by the MmpClient not being connected, such as when first starting the program or during a networking issue. The request can still run as often as possible, but will quickly report an error and try again. Once the client is connected, the next request should succeed.
  • Additional details are in the CODESYS help documentation for Edge Triggered Function Blocks of the Common Behaviour Model.

Finally, we need to feed th udiVar variable into the write block. This can be done by directly assigning udiVar to the udiData input. Or, for a more visual approach, the ADD block can be connected to the write block, like this:

CODESYS MMP Example

The final program should look like this:

CODESYS MMP Example

Step 5 - Run and View Results

Before running the application, use groov Manage to view the Scratch Pad value.

  1. In groov Manage, navigate to Home > I/O > I/O Services > Scratch Pad.
  2. For Data Type, select 32-bit Integer.
  3. For Index, enter 0.
  4. For Length, enter 1 or more.

Back in CODESYS:

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

If all goes well, there should be a solid blue line between the client’s xError output and xConnect input. The write block’s xBusy should be cycling between TRUE and FALSE, as each request is completed.

CODESYS MMP Example

Back in the Scratch Pad page in groov Manage, the Scratch Pad element should be increasing. 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

Structured Text Version

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

CODESYS MMP Example

Next Step

Continue on to the Reading an Integer Array example.