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:
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
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