Click here to Skip to main content
13,193,590 members (42,965 online)
Click here to Skip to main content
Add your own
alternative version

Stats

62.6K views
6K downloads
24 bookmarked
Posted 19 Feb 2015

OMRON PLC TCP Interface

, 14 Oct 2016
Rate this:
Please Sign up or sign in to vote.
Base class for OMRON PLC communications

mcONROM ScreenShot

Introduction

In our company, we use OMRON PLC to manage our production machines. Initially, we had a SCADA application to obtain data stored in the PLC. The truth is that I wanted a simple solution that allows me to create Windows services to get data from PLC, instead of using a dedicated PC for this task. I tried CX-Server Lite, but its components require a WindowsForm application, and cannot be used in Windows services. This is the reason why I decided to write this class.

The library consists of a main class, a transport layer class, currently for TCP, and one class to manage the 'FINS' orders via TCP. The transport and orders layer are based on two interfaces, ITransport, IFINSCommand. This will easily allow to add more functionality (UDP, Serial, ...).

Background

This class uses synchronous sockets to communicate with the PLC. Keep in mind that a communication issue could block the current thread. I think it is advisable to use a secondary thread to avoid this problem. As you can see, this class does not launch message box windows neither console messages, most functions return bool values and you should use .LastError() function to check the result.

The current version implements 3 PLC messages:

  • [1,1] MEMORY AREA READ finsMemoryAreaRead()
  • [1,2] MEMORY AREA WRITE finsMemoryAreaWrite()
  • [5,1] CONTROLLER DATA READ finsConnectionDataRead()

And some methods to deal with DM area:

  • ReadDM()
  • ReadDMs()
  • WriteDM()
  • ClearDMs()

And two new methods to deal with CIO Bit area:

  • ReadCIOBit()
  • WriteCIOBit()

Take a look at tcpFINSCommand.cs and you'll notice that it's really easy to add new methods for another PLC message.

Using the Code

First and foremost, add a reference for the mc.Omron.vx.xx.dll to your project.

Add one mcOMRON.OmronPLC object to your project and initialize it using one transport type (only TCP is available by now):

public partial class TestPLC : Form
{
    //
    // plc class
    //
    mcOMRON.OmronPLC <var>plc;

    /// <summary>
    /// constructor
    /// </summary>
    public TestPLC()
    {
        InitializeComponent();

        // initialize a new plc object with tcp transport layer
        //
        this.plc = new mcOMRON.OmronPLC(mcOMRON.TransportType.Tcp);
    }
...

Before you Connect() with the PLC, you must set up the TCP connection parameters. As the OmronPlc uses the interface class, you must cast it to the corresponding tcpFINSCommand class and then call SetTCPParams method.

/// <summary>
/// try to connect to the plc
/// </summary>
private void Connect()
{
    if (ip.Text == "") return;
    if (port.Text == "") return;

    try
    {
        // set ip:port for command layer, may cast to tcpFINSCommand to set ip and port
        //
        mcOMRON.tcpFINSCommand tcpCommand = ((mcOMRON.tcpFINSCommand)plc.FinsCommand);
        tcpCommand.SetTCPParams(IPAddress.Parse(ip.Text), Convert.ToInt32(port.Text));
                
        // connection
        //
        if (! plc.Connect())
        {
            throw new Exception(plc.LastError);
        }
...

By default, this class uses Byte, UInt16 and UInt32 variables, and arrays are passed by ref.

/// <summary>
/// read a single DM
/// </summary>
private void ReadDM()
{
    if (dm_position.Text == "") return;
    UInt16 dmval=0;
    try
    {
        if (! plc.ReadDM(Convert.ToUInt16(dm_position.Text), ref dmval))
        {
            throw new Exception(plc.LastError);
        }

        dm_value.Text = dmval.ToString();
        
        dialog.Text = plc.LastDialog("READ DM");
        dialog.AppendText("DM VALUE: " + dmval.ToString());
    }
    catch (Exception ex)
    {
        MessageBox.Show("ReadDM() Error: " + ex.Message);
    }
}

As you can see in the screenshot, I've added a debug functionality. If you call LastDialog() after sending any command, you can get the dialog between PC and PLC in hexadecimal format.

Points of Interest

Would you like to add a handler for new PLC message?

Let me show you the actual method: MemoryAreaWrite()

/// <summary>
/// MEMORY AREA WRITE
/// </summary>
/// <param name="area"></param>
/// <param name="address"></param>
/// <param name="count"></param>
/// <param name="data"></param>
/// <returns></returns>
public bool MemoryAreaWrite(MemoryArea area, UInt16 address, UInt16 count, ref Byte [] data)
{
    try
    {
        // command & subcomand
        //
        this.MC = 0x01;
        this.SC = 0x02;

        // memory area
        //
        this.cmdFins[F_PARAM] = (Byte)area;
        
        // address
        //
        this.cmdFins[F_PARAM + 1] = (Byte)((address >> 8) & 0xFF);
        this.cmdFins[F_PARAM + 2] = (Byte)(address & 0xFF);

        // special flag
        //
        this.cmdFins[F_PARAM + 3] = (Byte)(0);

        // count items
        //
        this.cmdFins[F_PARAM + 4] = (Byte)((count >> 8) & 0xFF);
        this.cmdFins[F_PARAM + 5] = (Byte)(count & 0xFF);

        // set command length (12 + additional params)
        //
        this.finsCommandLen = 18;

        // send the message
        //
        return FrameSend(data);
    }
    catch (Exception ex)
    {
        this._lastError = ex.Message;
        return false;
    }
}

You must set Main code and Subcode properties and use cmdFins[] array to set additional parameters. Finally, set the (finsCommandLen) command length and call FrameSend() to send the message.

History

October 2016

  • There was a bug in MemoryArea values, I was using wrong values.
  • I've added specific funtions for signed values.

November 2016

  • I've added some functions in BTool class to work with Bits (IsBitSet, SetBit, UnsetBit).
  • I've added two methods to access CIO Bit area.

This is it for now.

Please feel free to contact me if you have any questions about this class.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Joan Magnet
Chief Technology Officer Vilardell Purti Group
Spain Spain
No Biography provided

You may also be interested in...

Comments and Discussions

 
SuggestionThanks you code Pin
Member 1347160717hrs 49mins ago
memberMember 1347160717hrs 49mins ago 
QuestionClass to Float and Text from Byte Pin
Victor Alfonso Hernández Estudillo17-Aug-17 12:50
memberVictor Alfonso Hernández Estudillo17-Aug-17 12:50 
QuestionUWP Apps Pin
Member 128244641-Jul-17 8:48
memberMember 128244641-Jul-17 8:48 
QuestionI'm tryed read the value of variable of D1 of CX programmer, but when the value > 9999, returned 0. How i can worked with Int32 or UInt32 of var "ref"? Pin
Member 1226472729-Jun-17 15:42
memberMember 1226472729-Jun-17 15:42 
QuestionReceive variables and their clp values Pin
Member 1226472729-Jun-17 0:42
memberMember 1226472729-Jun-17 0:42 
QuestionProblem with connecting to Omron PLC trough Ethernet card CS1W-ETN11 Pin
Member 1281357323-May-17 3:32
memberMember 1281357323-May-17 3:32 
AnswerRe: Problem with connecting to Omron PLC trough Ethernet card CS1W-ETN11 Pin
Joan Magnet23-May-17 8:53
memberJoan Magnet23-May-17 8:53 
GeneralRe: Problem with connecting to Omron PLC trough Ethernet card CS1W-ETN11 Pin
Member 1281357324-May-17 23:42
memberMember 1281357324-May-17 23:42 
QuestionThank You - We build Omron PLC Driver Pin
Yedi Studio19-Feb-17 3:06
memberYedi Studio19-Feb-17 3:06 
AnswerRe: Thank You - We build Omron PLC Driver Pin
Joan Magnet19-Feb-17 7:27
memberJoan Magnet19-Feb-17 7:27 
QuestionHow to read a variable in Time format? Pin
Member 1295803324-Jan-17 22:00
memberMember 1295803324-Jan-17 22:00 
AnswerRe: How to read a variable in Time format? Pin
Joan Magnet25-Jan-17 10:05
memberJoan Magnet25-Jan-17 10:05 
QuestionWR Pin
kk_chan18-Jan-17 19:54
memberkk_chan18-Jan-17 19:54 
AnswerRe: WR Pin
Joan Magnet21-Jan-17 3:49
memberJoan Magnet21-Jan-17 3:49 
QuestionRe: WR Pin
Member 131175289-Apr-17 20:55
memberMember 131175289-Apr-17 20:55 
QuestionCan I use this class to communicate with Allen-Bradley PLC Pin
Member 1282650822-Dec-16 22:39
memberMember 1282650822-Dec-16 22:39 
AnswerRe: Can I use this class to communicate with Allen-Bradley PLC Pin
Joan Magnet24-Dec-16 4:46
memberJoan Magnet24-Dec-16 4:46 
QuestionReadDM time waste Pin
rsjauqui15-Dec-16 0:42
memberrsjauqui15-Dec-16 0:42 
AnswerRe: ReadDM time waste Pin
Joan Magnet15-Dec-16 21:33
memberJoan Magnet15-Dec-16 21:33 
QuestionDM area reading speed vs CIO area reading speed Pin
Member 1286748913-Dec-16 18:55
memberMember 1286748913-Dec-16 18:55 
AnswerRe: DM area reading speed vs CIO area reading speed Pin
Joan Magnet14-Dec-16 2:57
memberJoan Magnet14-Dec-16 2:57 
GeneralRe: DM area reading speed vs CIO area reading speed Pin
Member 1286748928-Dec-16 11:04
memberMember 1286748928-Dec-16 11:04 
GeneralRe: DM area reading speed vs CIO area reading speed Pin
Joan Magnet29-Dec-16 1:01
memberJoan Magnet29-Dec-16 1:01 
QuestionMultiple PLCs Pin
Member 128244642-Dec-16 10:13
memberMember 128244642-Dec-16 10:13 
AnswerRe: Multiple PLCs Pin
Joan Magnet2-Dec-16 12:15
memberJoan Magnet2-Dec-16 12:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171018.2 | Last Updated 14 Oct 2016
Article Copyright 2015 by Joan Magnet
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid