Click here to Skip to main content
15,884,176 members
Articles / Web Development / HTML
Tip/Trick

OMRON PLC TCP Interface

Rate me:
Please Sign up or sign in to vote.
4.91/5 (36 votes)
14 Oct 2016CPOL3 min read 390.4K   18K   35   164
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):

C#
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.

C#
/// <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.

C#
/// <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()

C#
/// <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)


Written By
Chief Technology Officer Vilardell Purti Group
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow write DM string Pin
Eduardo Cortez 20236-Jan-24 9:52
Eduardo Cortez 20236-Jan-24 9:52 
AnswerRe: How write DM string Pin
Eduardo Cortez 20236-Jan-24 11:24
Eduardo Cortez 20236-Jan-24 11:24 
AnswerRe: How write DM string Pin
Joan Magnet9-Jan-24 1:58
Joan Magnet9-Jan-24 1:58 
QuestionNASD command error: 33 Pin
Eduardo Cortez 202319-Oct-23 5:34
Eduardo Cortez 202319-Oct-23 5:34 
AnswerRe: NASD command error: 33 Pin
Joan Magnet19-Oct-23 7:47
Joan Magnet19-Oct-23 7:47 
QuestionDoesnt works using multithreading Pin
Eduardo Cortez 20231-Sep-23 12:35
Eduardo Cortez 20231-Sep-23 12:35 
AnswerRe: Doesnt works using multithreading Pin
Joan Magnet8-Sep-23 0:16
Joan Magnet8-Sep-23 0:16 
QuestionLosing connection Pin
Member 153752896-Feb-23 2:40
Member 153752896-Feb-23 2:40 
AnswerRe: Losing connection Pin
Galicia Nava Juan José27-Feb-23 14:07
Galicia Nava Juan José27-Feb-23 14:07 
AnswerRe: Losing connection Pin
Galicia Nava Juan José27-Feb-23 14:07
Galicia Nava Juan José27-Feb-23 14:07 
BugThis DLL Fails while working on Threads Pin
Bharath J Sharma19-Sep-22 1:07
Bharath J Sharma19-Sep-22 1:07 
GeneralRe: This DLL Fails while working on Threads Pin
Joan Magnet19-Sep-22 21:22
Joan Magnet19-Sep-22 21:22 
GeneralRe: This DLL Fails while working on Threads Pin
Gabriel Espaso21-Oct-22 13:25
professionalGabriel Espaso21-Oct-22 13:25 
GeneralRe: This DLL Fails while working on Threads Pin
Bharath J Sharma7-Nov-22 20:09
Bharath J Sharma7-Nov-22 20:09 
QuestionHow to change to UDP Pin
Jenas9294-Sep-22 21:43
Jenas9294-Sep-22 21:43 
AnswerRe: How to change to UDP Pin
Joan Magnet5-Sep-22 5:56
Joan Magnet5-Sep-22 5:56 
GeneralRe: How to change to UDP Pin
Jenas9295-Sep-22 16:09
Jenas9295-Sep-22 16:09 
GeneralRe: How to change to UDP Pin
Joan Magnet5-Sep-22 22:28
Joan Magnet5-Sep-22 22:28 
QuestionConnect to NX1 and NX7 PLC? Pin
Member 1283508819-Jul-22 0:00
Member 1283508819-Jul-22 0:00 
AnswerRe: Connect to NX1 and NX7 PLC? Pin
Gabriel Espaso21-Oct-22 13:23
professionalGabriel Espaso21-Oct-22 13:23 
QuestionMessage Closed Pin
8-May-22 5:43
Member 155957748-May-22 5:43 
QuestionWhere MemoryArea value come form ? Pin
Member 126883975-Apr-22 8:03
Member 126883975-Apr-22 8:03 
QuestionReading W0.0 and W1 Pin
nuubJ7-Feb-22 22:33
nuubJ7-Feb-22 22:33 
AnswerRe: Reading W0.0 and W1 Pin
Joan Magnet8-Feb-22 3:41
Joan Magnet8-Feb-22 3:41 
QuestionGetting Response code error Pin
Member 1472936123-Sep-21 21:38
Member 1472936123-Sep-21 21:38 

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.