Link to home
Start Free TrialLog in
Avatar of Marius0188
Marius0188

asked on

C# Variant Type

Hi All,

I have a interface with a Siemens PBL.
The methods that is exposed to me to read from the PBL takes 7 parameters.
And I am not sure how to declare the parameters and types for the parameters.

Please have a look at the attached screenshot and help me with this.
I am fairly new to C#.


Kind regards :)
OpX.jpg
Avatar of BuggyCoder
BuggyCoder
Flag of India image

Two Things to note here:-
1. Methods That Accept Ref and Out Parameters, method arguments have to be of the same type as type of the parameter.
2. List cannot be passed to a method that accepts array, you have to convert your list to Array by calling its ToArray() method

Here is a code for you:-
                Array arrHandles = new List<int>().ToArray();
                Array arrValues = new List<string>().ToArray();
                Array arrErrors = new List<Exception>().ToArray();
                object quality = "Quality";
                object ts = DateTime.Now;

                IOPCGroup.SyncRead(1, 1, ref arrHandles, out arrValues, out arrErrors, out quality, out ts);

Open in new window

Avatar of Marius0188
Marius0188

ASKER

Thank you for the quick reply.
How would I return the values from the Array then?

Kind regards :)
If trying to read the first index of the arrValues[] array like this:
            MessageBox.Show("Value: " + arrValues[0]);


I get the following compiler error:
Error      2      Cannot apply indexing with [] to an expression of type 'System.Array'      C:\OperationX\OperationX\frmMain.cs      161      41      OperationX
Try this

MessageBox.Show("Value: " + arrValues.GetValue(0));
Okay thanks got that one.
Here is the example for VBscript that does work. I have tested the XLS doc and it was able to read the value from PBL.

Can you probably see where I am going wrong with my C# version of the working VB:

VB Code (working)
Option Explicit  ' every variable has to be declared
Option Base 1    ' every Array starts on Index 1

Private MyOPCServer As OPCServer
Private WithEvents MyOPCGroup As OPCGroup
Private MyOPCItems() As OPCItem

Private Sub cmdConnect_Click()
    Dim i As Integer

    Set MyOPCServer = New OPCServer
    
    'connect the OPC Server
    Call MyOPCServer.Connect(Cells(4, 2))
    
    'add one OPC Group
    Set MyOPCGroup = MyOPCServer.OPCGroups.Add("Gruppe1")
    
    'apply for DataChange
    MyOPCGroup.IsSubscribed = True
    MyOPCGroup.UpdateRate = 500
    
    'add Items
    ReDim MyOPCItems(4)
    
    For i = 1 To 4
        Set MyOPCItems(i) = MyOPCGroup.OPCItems.AddItem(Cells(8 + i, 2), 8 + i)
    Next i
    
    'set buttons
    cmdDisconnect.Enabled = True
    cmdRead.Enabled = True
    cmdWrite.Enabled = True
    cmdConnect.Enabled = False
        
    
End Sub
Private Sub cmdRead_Click()
    
    Dim SHandles(2) As Long 'parameter value
    Dim Values() As Variant 'return value
    Dim Errors() As Long    'return value
    Dim Qual As Variant     'return value
    Dim TS As Variant       'return value
    Dim i As Integer
    
    'save ServerHandles
    SHandles(1) = MyOPCItems(1).ServerHandle
    SHandles(2) = MyOPCItems(2).ServerHandle
        
    Call MyOPCGroup.SyncRead(OPCCache, 2, SHandles, Values, Errors, Qual, TS)
   
   'fill the cells with the read values
   
   For i = 1 To 2
    Cells(8 + i, 5) = Values(i)   'column "read"
    Cells(8 + i, 8) = Qual(i)     'column "quality"
    Cells(8 + i, 9) = TS(i)       'column "timestamp"
   Next i
    
    
    
End Sub

Open in new window


And this is my C# version based on VB version.
        private const string PROG_ID = "OPC.SimaticNet.1";
        private OPCSiemensDAAutomation.OPCServer opcServer;
        private OPCSiemensDAAutomation.OPCGroup opcGroup;
        private OPCSiemensDAAutomation.OPCItem[] opcItems = new OPCSiemensDAAutomation.OPCItem[1];

        private void connectOPCServer() 
        {
            try
            {
                opcServer = new OPCSiemensDAAutomation.OPCServer();
                opcServer.Connect(PROG_ID);

                opcGroup = opcServer.OPCGroups.Add("Gruppe1");
                opcGroup.IsSubscribed = true;
                opcGroup.UpdateRate = 500;

                opcItems[0] = opcGroup.OPCItems.AddItem("S7:[S7 connection_1]DB700, STRING8.50", 1);
            }
            catch(Exception ex)
            {
                MessageBox.Show("Exception: " + ex.Message);
            }
        }
            Array arrHandles = new List<int>().ToArray();
            Array arrValues = new List<string>().ToArray();
            Array arrErrors = new List<Exception>().ToArray();
            Object Quality = "Quality";
            Object Ts = DateTime.Now;


            arrHandles.SetValue(opcItems[1].ServerHandle, 0);

            opcGroup.SyncRead(1, 1, ref arrHandles, out arrValues, out arrErrors, out Quality, out Ts);

            MessageBox.Show("Value: " + arrValues.GetValue(0));

Open in new window


I know I am doing something wrong with the passing arrHandles parameter.
As in the VB example it get assigned by the opcITems.

Not sure how to rectify it though.

Please help :)
Sorry  I keep on referring to PBL but meant PLC...
:)

My mind is on something else....
Just a note about the VBScript example.
They are reading 4 dummy values and therefor you will see the loop 1 to 4.
I am only reading ONE value do know the name etc...
Okay I am getting exception index out of bounds on my arrays.

Why is this code failing?

                Array arrHandles = new List<int>().ToArray();
                Array arrValues = new List<string>(1).ToArray();
                Array arrErrors = new List<Exception>().ToArray();
                Object Quality = "Quality";
                Object Ts = DateTime.Now;

                arrValues.SetValue("Joe", 0);
                MessageBox.Show(arrValues.GetValue(0).ToString());

Open in new window

I tried both options:

Array arrValues = new List<string>().ToArray();

AND

Array arrValues = new List<string>(1).ToArray();
ASKER CERTIFIED SOLUTION
Avatar of BuggyCoder
BuggyCoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Okay thanks - one step further now :)

But there seem to be a type mismatch now.
The VBScript method that works is like this:

Private Sub cmdWrite_Click()

    Dim SHandles(2) As Long  'parameter value
    Dim Values(2) As Variant 'parameter value
    Dim Errors() As Long     'return value
    Dim i As Integer

        
    'save ServerHandles
    SHandles(1) = MyOPCItems(3).ServerHandle
    SHandles(2) = MyOPCItems(4).ServerHandle
    
    'find out the values which are written into the following cells (line 11+12, column F)
    'this values should be writen into PLC
    
    For i = 1 To 2
          Values(i) = Cells(10 + i, 6)
          If Values(i) = "" Then Values(i) = 0
    Next i
    
    Call MyOPCGroup.SyncWrite(2, SHandles, Values, Errors)
    
End Sub

Open in new window


And my C# version is this:

        private void btnWrite_Click(object sender, EventArgs e)
        {
                var lstHandles = new List<int>();
                lstHandles.Add(opcItems[0].ServerHandle);
                Array arrHandles = lstHandles.ToArray();

                var lstValues = new List<string>();
                lstValues.Add("Test Value");
                Array arrValues = lstValues.ToArray();

                Array arrErrors = new List<int>().ToArray();

                Object Quality = "Quality";
                Object Ts = DateTime.Now;

                opcGroup.SyncWrite(1, ref arrHandles, ref arrValues, out arrErrors);
        }

Open in new window



And I am receiving the following error when executing the C# Code:
please see attached screen shot....
opc.jpg
Let me know what syncwrite method looks like.
Sorry about that:

void SyncWrite(int NumItems, ref Array ServerHandles, ref Array Values, out Array Errors);
Each array that you are supplying in method have to be of some predefined type. My example included arrays of some arbitrary types that it took as an example...
 
Read the documentation and define the arrays by specifying the correct types.

As of now, it seems that it is unhappy with the type of array being passed to one of the ref/out marked parameters.

Also suggest you close this question and open a new question as your question was regarding how to pass parameters to SyncRead method, the solution to which has already been supplied to you.

you can always open new question for a new problem...
Thank Buggy :) I agree.
Will close this one and start new one...


Keep well :)