[英]Value Does not fall within the expected range. (Runtime Error)
I am converting a Windows Form from VB to C# and I ran in to an error when I attempted to do a synchronized read of my OPC tags. 我正在将Windows窗体从VB转换为C#,当我尝试对OPC标签进行同步读取时,我遇到了错误。
I have this code: 我有这个代码:
public partial class FrmPartialMain : Form
{
RsiOPCAuto.OPCServer oOpcServer;
RsiOPCAuto.OPCGroup oOpcGroup;
int ClHandle; //this is set to 1 in another part of the code.
int SvHandle;
int OpcDsCashe = 1;
int OpcDsDevice = 2;
private void cmdSyncRead_Click(object sender, EventArgs e)
{
int lNumItems = oOpcGroup.OPCItems.Count; // = 3
int[] h = new int[lNumItems];
Array arValues = new int[lNumItems];
Array arHandles;
Array arErrors;
object Qualities;
object Timestamps;
h[ClHandle - 1] = oOpcGroup.OPCItems.Item(ClHandle).ServerHandle;
arHandles = (Array)h;
//Error on the next line bellow.
oOpcGroup.SyncRead((short)OpcDsDevice, lNumItems, ref arHandles, out arValues, out arErrors, out Qualities, out Timestamps);
txtSubValue.Text = Convert.ToString(arValues.GetValue(0));
}
}
oOpcGroup.Read() reads the value, quality and timestamp information for one or more items in a group. oOpcGroup.Read()读取组中一个或多个项的值,质量和时间戳信息。 and the return type looks like this:
返回类型如下所示:
SyncRead(short Source, int NumItems, ref System.Array ServerHandles, out System.Array Values, out System.Array Errors, out object Qualities, out object TimeStamps);
Running this code gives me the error in the title, Value Does not fall within the expected range. 运行此代码会在标题中显示错误,值不在预期范围内。 Any ideas of what i might be doing wrong here?
我在这里做错了什么想法?
Brainstorm away! 头脑风暴!
It's alive! 它还活着!
This is the fixed code: 这是固定代码:
public partial class FrmPartialMain : Form
{
RsiOPCAuto.OPCServer oOpcServer;
RsiOPCAuto.OPCGroup oOpcGroup;
int ClHandle; //this is set to 1 in another part of the code.
int SvHandle;
int OpcDsCashe = 1;
int OpcDsDevice = 2;
private void cmdSyncRead_Click(object sender, EventArgs e) //Sync Read
{
int lNumItems = oOpcGroup.OPCItems.Count;
int[] arH = new int[1 + lNumItems];
Array arValues = new object[1 + lNumItems]; //<-- This needed to be an object array.
Array arHandles;
Array arErrors;
object Qualities;
object Timestamps;
arH[ClHandle] = oOpcGroup.OPCItems.Item(ClHandle).ServerHandle;
arHandles = (Array)arH;
oOpcGroup.SyncRead((short)OpcDsDevice, lNumItems, ref arHandles, out arValues, out arErrors, out Qualities, out Timestamps);
txtSubValue.Text = Convert.ToString(arValues.GetValue(1));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.