Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

eandres (IS/IT--Management) (OP)
7 Jun 11 10:05
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
 

eandres (IS/IT--Management) (OP)
7 Jun 11 16:31
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
 

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close