Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Data exchange with OPC between c# code and RSLinx - Rockwell PLC [modified]

Data exchange with OPC between c# code and RSLinx - Rockwell PLC [modified]

Scheduled Pinned Locked Moved C#
helpcsharpsysadmindata-structurestesting
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rodrigorodriquez
    wrote on last edited by
    #1

    Sorry I made a mistake in the previous post. I highlight tho point in the code with the error with the bold characters. Hello I am writing very simple code with C# to read and write data from Rockwell Automation PLC through their communication server (OPC srever) RSLINX. I can read data but I cannot write because executable exit with error when I call the

    RSLinxOCgroup.SyncWrite(item, ref syncserverhandles, ref datatowritetoplcmemory, out SyncErrors)

    (dll: RsiOPCAuto.OPCGroupClass.SyncWrite(Int32 NumItems, Array& ServerHandles, Array& Values, Array& Errors) ) error : Specified array was not of the expected type. the error, I suppose, refers to the datatowritetoplcmemory that is declared as :

    System.Array datatowritetoplcmemory; datatowritetoplcmemory = Array.CreateInstance(System.Type.GetType("System.Int32"), 10)

    and then filled for example like this: for (i = 0; i < 10; i++) { datatowritetoplcmemory.SetValue (i * i, i) } Can somebody help me? Thank you Best regards

    rodrigo

    modified on Friday, December 10, 2010 10:49 AM

    P 1 Reply Last reply
    0
    • R rodrigorodriquez

      Sorry I made a mistake in the previous post. I highlight tho point in the code with the error with the bold characters. Hello I am writing very simple code with C# to read and write data from Rockwell Automation PLC through their communication server (OPC srever) RSLINX. I can read data but I cannot write because executable exit with error when I call the

      RSLinxOCgroup.SyncWrite(item, ref syncserverhandles, ref datatowritetoplcmemory, out SyncErrors)

      (dll: RsiOPCAuto.OPCGroupClass.SyncWrite(Int32 NumItems, Array& ServerHandles, Array& Values, Array& Errors) ) error : Specified array was not of the expected type. the error, I suppose, refers to the datatowritetoplcmemory that is declared as :

      System.Array datatowritetoplcmemory; datatowritetoplcmemory = Array.CreateInstance(System.Type.GetType("System.Int32"), 10)

      and then filled for example like this: for (i = 0; i < 10; i++) { datatowritetoplcmemory.SetValue (i * i, i) } Can somebody help me? Thank you Best regards

      rodrigo

      modified on Friday, December 10, 2010 10:49 AM

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You didn't need to post again. You can edit your posts if they are incorrect. Please use the pre tags for code snippets and remove the other post as it serves no point.

      I'm not a stalker, I just know things. Oh by the way, you're out of milk.

      Forgive your enemies - it messes with their heads

      My blog | My articles | MoXAML PowerToys | Onyx

      R 1 Reply Last reply
      0
      • P Pete OHanlon

        You didn't need to post again. You can edit your posts if they are incorrect. Please use the pre tags for code snippets and remove the other post as it serves no point.

        I'm not a stalker, I just know things. Oh by the way, you're out of milk.

        Forgive your enemies - it messes with their heads

        My blog | My articles | MoXAML PowerToys | Onyx

        R Offline
        R Offline
        Rayh059
        wrote on last edited by
        #3

        I've been a Rockwell user for many years, sounds like a good project. I'd think that your code should interface with any OPC server and not necessarily specific to RSLinx, that's the point right?

        P R 2 Replies Last reply
        0
        • R Rayh059

          I've been a Rockwell user for many years, sounds like a good project. I'd think that your code should interface with any OPC server and not necessarily specific to RSLinx, that's the point right?

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          I think you addressed this to the wrong person.

          I'm not a stalker, I just know things. Oh by the way, you're out of milk.

          Forgive your enemies - it messes with their heads

          My blog | My articles | MoXAML PowerToys | Onyx

          1 Reply Last reply
          0
          • R Rayh059

            I've been a Rockwell user for many years, sounds like a good project. I'd think that your code should interface with any OPC server and not necessarily specific to RSLinx, that's the point right?

            R Offline
            R Offline
            rodrigorodriquez
            wrote on last edited by
            #5

            Hello Sorry for my late reply. In this case the application is dedicated to the communication with RSLinx. Here I attach the code where I cut many non intersting lines of code so it will not run if you try. Then if yo need I can attach the complete version. Anyway the lines that i am interested in are highlighted.

            using System;
            using System.Collections.Generic;
            using System.Runtime.InteropServices;
            using System.Text;
            using System.Timers;
            using RsiOPCAuto;
            using System.Windows.Forms;
            using System.IO;
            using System.Threading;

            namespace opc_ali
            {
            class Program
            {
            //static Opc.Server LinxOPCServer;

                static RsiOPCAuto.OPCServer RSLinxOPCserver;
                static RsiOPCAuto.OPCGroup RSLinxOPCgroup;
                static RsiOPCAuto.OPCItem RSLinxOPCItem\_1;
                static RsiOPCAuto.OPCItem RSLinxOPCItem\_2;
                static RsiOPCAuto.OPCItem RSLinxOPCItem\_3;
                static RsiOPCAuto.OPCItem RSLinxOPCItem\_4;
            
                static int prec\_sec\_bl = 0;
                static int prec\_millis\_bl = 0;
                static int TimeNow\_Sec, TimeNow\_mSec;
                static int tempo\_p, tempo\_s, delta;
            
                static int\[\] DDE\_IN\_INT = new int\[50\];
                static int\[\] DDE\_OUT\_INT = new int\[50\];
                static double\[\] DDE\_IN\_DOU = new double\[50\];
                static double\[\] DDE\_OUT\_DOU = new double\[50\];
            
                static int numero\_word\_I;
                static int numero\_word\_O;
                static int tempo\_campionamento;
                static int numero\_campioni;
                static int new\_line;
            
                public static void Main(string\[\] args)
                {
                    try
                    {
                        RSLinxOPCserver = new RsiOPCAuto.OPCServer();
                        RSLinxOPCserver.Connect("RSLinx OPC Server", "");
                        RSLinxOPCgroup = RSLinxOPCserver.OPCGroups.Add("Prova");
                        RSLinxOPCgroup.IsActive = true;
                        RSLinxOPCgroup.UpdateRate = 50;
                        RSLinxOPCgroup.IsSubscribed = true;
            
            
                        RSLinxOPCItem\_1 = RSLinxOPCgroup.OPCItems.AddItem("\[ali\]Program:scrittura\_dati\_per\_out.dde\_OUT\_INT\[0\],L1,C1", 1);
                        RSLinxOPCItem\_2 = RSLinxOPCgroup.OPCItems.AddItem("\[ali\]Program:scrittura\_dati\_per\_out.dde\_OUT\_INT\[0\],L" + numero\_word\_I + ",C1", 2);
                        RSLinxOPCItem\_3 = RSLinxOPCgroup.OPCItems.AddItem("\[ali\]Program:scrittura\_dati\_per\_out.dde\_IN\_INT\[0\],L1,C1", 3);
                        RSLinxOPCItem\_4 = RSLinxOPCgroup.OPCItems.AddItem("\[ali\]Program:scrittura\_dati\_per\_out.dde\_IN\_INT\[0\],L" + numero\_word\_O + ",C1", 4);
                        Lett
            
            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups