Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OPCAutomation SyncWrite help

Status
Not open for further replies.

Ed Andres

IS-IT--Management
Mar 27, 2001
142
US
I am developing a small app to do some very minor work with an OPC server. I can connect and subscribe to tags with no problem but can't figure out how to write a value back. There seems to be tons of examples for VB.Net out on the internet but almost none for C#. Using VB.Net is not an option because I already have the application developed in C# and this OPC addition is a very small part of the application.

Here is some sample code for your review:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OPCAutomation;




namespace OPC2
{
    public partial class Form1 : Form
    {
        public OPCAutomation.OPCServer AnOPCServer;
        public OPCAutomation.OPCServer ConnectedOPCServer;
        public OPCAutomation.OPCGroups ConnectedServerGroup;
        public OPCAutomation.OPCGroup ConnectedGroup;
        public string Groupname;

        int ItemCount;
        Array OPCItemIDs = Array.CreateInstance(typeof(string), 10);
        Array ItemServerHandles = Array.CreateInstance(typeof(Int32), 10);
        Array ItemServerErrors = Array.CreateInstance(typeof(Int32), 10);
        Array ClientHandles = Array.CreateInstance(typeof(Int32), 10);
        Array RequestedDataTypes = Array.CreateInstance(typeof(Int16), 10);
        Array AccessPaths = Array.CreateInstance(typeof(string), 10);
        Array WriteItems = Array.CreateInstance(typeof(string), 10);
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                string IOServer = "Kepware.KEPServerEX.V5";
                string IOGroup = "OPCGroup1";

                ConnectedOPCServer = new OPCAutomation.OPCServer();
                ConnectedOPCServer.Connect(IOServer, "");
                ConnectedGroup = ConnectedOPCServer.OPCGroups.Add(IOGroup);
                ConnectedGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(ObjOPCGroup_DataChange);
                ConnectedGroup.UpdateRate = 1000;
                ConnectedGroup.IsSubscribed = ConnectedGroup.IsActive;


                ItemCount = 2;
                OPCItemIDs.SetValue("Channel2.Device1.R1", 1);
                ClientHandles.SetValue(1, 1);
                OPCItemIDs.SetValue("Channel2.Device1.R2", 2);
                ClientHandles.SetValue(2, 2);

                ConnectedGroup.OPCItems.DefaultIsActive = true;
                ConnectedGroup.OPCItems.AddItems(ItemCount, ref OPCItemIDs, ref ClientHandles, out ItemServerHandles, out ItemServerErrors, RequestedDataTypes, AccessPaths);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            ConnectedOPCServer.Disconnect();
        }

        private void ObjOPCGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
        {
            try
            {
                for (int i = 1; i <= NumItems; i++)
                {
                    if ((Convert.ToInt32(ClientHandles.GetValue(i)) == 1))
                    {
                        textBox1.Text = ItemValues.GetValue(i).ToString();
                    }
                    if ((Convert.ToInt32(ClientHandles.GetValue(i)) == 2))
                    {
                        textBox2.Text = ItemValues.GetValue(i).ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void btnWrite1_Click(object sender, EventArgs e)
        {
            try
            {
                WriteItems.SetValue(textBox4.Text, 1);
                ConnectedGroup.SyncWrite(ItemCount, ref ItemServerHandles, ref WriteItems, out ItemServerErrors);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

    }
}

The error is an array mismatch "Specified Array was not of the Expected Type". I think it is the ItemServerHandles and itemServerErrors arrays that are the culprit but I'm not completely sure.

Any help would be apprecieated.
Thanks,
Ed

Ed
 
Well after a lot of hair loss the solution was pretty simple. The offending array was not the ones I suspected. All I needed to do is change the WriteItems array to an object type and it works.

Array WriteItems = Array.CreateInstance(typeof(object), 10);

Ed

Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top