0
votes

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.

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. 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!

1
Please add the Exception with stacktrace, indicate which line the the linenumber refers to.Henk Holterman
it looks like ClHandle is never set, so it starts at 0, then you subtract 1 from it which would leave you an array index of -1 which is not a valid index.pstrjds
@pstrjds This is just a small portion of my code, ClHandle is set to 1 at an earlier stage.Charp
Type oOpcGroup.EventName += <Tab><Tab>. After you typed the += it offers to create the assignment which you accept with the first tab. With assignment, I mean the part like this: new ButtonClickDelegate(oOpcGroup_DataChange). The second tab offers to create the not yet existing method oOpcGroup_DataChange.Daniel Hilgarth
You need to put it after the line the initializes oOpcGroup with a value.Daniel Hilgarth

1 Answers

2
votes

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));
    }
}