Data exchange with OPC between c# code and RSLinx - Rockwell PLC [modified]
-
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
-
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
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
-
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
-
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?
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
-
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?
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