0

我正在将 Windows 窗体从 VB 转换为 C#,当我尝试同步读取我的 OPC 标签时遇到错误。

我有这个代码:

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() 读取组中一个或多个项目的值、质量和时间戳信息。返回类型如下所示:

 SyncRead(short Source, int NumItems, ref System.Array ServerHandles, out System.Array Values, out System.Array Errors, out object Qualities, out object TimeStamps);

运行此代码给了我标题中的错误,值不在预期范围内。关于我在这里可能做错了什么的任何想法?

头脑风暴!

4

1 回答 1

2

它还活着!

这是固定代码:

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));
    }
}
于 2012-04-18T08:32:11.400 回答