Click here to Skip to main content
14,867,123 members
Articles / Web Development / HTML
Posted 19 Feb 2015


36 bookmarked


Rate me:
Please Sign up or sign in to vote.
4.91/5 (36 votes)
14 Oct 2016CPOL3 min read
Base class for OMRON PLC communications

mcONROM ScreenShot


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, ...).


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()

        // 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;

        // 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;
        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>
/// </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)
        // 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.


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.


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


About the Author

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

Comments and Discussions

QuestionDM values morethan Unit16 Pin
Kim Ivan Bay-an4-Oct-20 22:06
MemberKim Ivan Bay-an4-Oct-20 22:06 
QuestionRead/Write cycle takes a long time Pin
heikkis30-Sep-20 6:06
Memberheikkis30-Sep-20 6:06 
QuestionCan't Read value over 5 digit Pin
ALEDDELVIN16-Jul-20 16:41
MemberALEDDELVIN16-Jul-20 16:41 
QuestionProblem when readin/writing Pin
Member 131693601-Jun-20 10:26
MemberMember 131693601-Jun-20 10:26 
AnswerRe: Problem when reading/writing Pin
Member 131693602-Jun-20 10:36
MemberMember 131693602-Jun-20 10:36 
QuestionIs There a method to use NJ Tags instead of addresses? Pin
Foglio772-Mar-20 22:39
MemberFoglio772-Mar-20 22:39 
AnswerRe: Is There a method to use NJ Tags instead of addresses? Pin
Joan Magnet25-Mar-20 3:10
MemberJoan Magnet25-Mar-20 3:10 
QuestionNASD Command Error :34 Pin
Mathews P Jacob11-Oct-19 0:18
MemberMathews P Jacob11-Oct-19 0:18 
QuestionThanks and question Pin
Member 1455491216-Sep-19 16:08
MemberMember 1455491216-Sep-19 16:08 
AnswerRe: Thanks and question Pin
Joan Magnet25-Mar-20 3:10
MemberJoan Magnet25-Mar-20 3:10 
QuestionHow to read and write real values using FINS command Pin
shubhendu mishra23-Aug-19 20:30
Membershubhendu mishra23-Aug-19 20:30 
QuestionWhat about UDP connection? Pin
Member 40393430-Jan-19 0:00
MemberMember 40393430-Jan-19 0:00 
AnswerRe: What about UDP connection? Pin
Joan Magnet25-Mar-20 3:11
MemberJoan Magnet25-Mar-20 3:11 
QuestionError Read PLC Omron Sysmac CP1h Pin
gimenezfrg15-Oct-18 17:36
Membergimenezfrg15-Oct-18 17:36 
QuestionOnly DM and CIO Bit able to read? Pin
Member 1289531220-Jun-18 22:04
MemberMember 1289531220-Jun-18 22:04 
Questionmc.Omron.vx.xx.dll code Pin
KenkoJones14-Jun-18 9:18
MemberKenkoJones14-Jun-18 9:18 
AnswerRe: mc.Omron.vx.xx.dll code Pin
Joan Magnet25-Mar-20 3:12
MemberJoan Magnet25-Mar-20 3:12 
QuestionRegarding implementing event Pin
Member 903162611-Jun-18 19:46
MemberMember 903162611-Jun-18 19:46 
QuestionJoan excellent example. Pin
Member 114541036-Jun-18 10:15
MemberMember 114541036-Jun-18 10:15 
QuestionHow to write 1word ( 4byte) to OMRON PLC using FINS command Pin
Member 1315767029-May-18 19:14
MemberMember 1315767029-May-18 19:14 
QuestionIs it possible to read Extended Memory addresses Pin
Member 1382294511-May-18 8:55
MemberMember 1382294511-May-18 8:55 
QuestionC++ Interface class Pin
sysinteng28-Apr-18 5:07
Membersysinteng28-Apr-18 5:07 
QuestionDoesn't work for me. Any Advice for me ? Pin
Gabriele Pettenò11-Apr-18 22:03
MemberGabriele Pettenò11-Apr-18 22:03 
AnswerRe: Doesn't work for me. Any Advice for me ? Pin
Joan Magnet19-Apr-18 4:57
MemberJoan Magnet19-Apr-18 4:57 
QuestionCX Simulator Pin
banczazsolt20-Dec-17 0:49
Memberbanczazsolt20-Dec-17 0:49 

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.