• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1468
  • Last Modified:

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
0
Marius0188
Asked:
Marius0188
  • 10
  • 4
1 Solution
 
BuggyCoderCommented:
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

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

Kind regards :)
0
 
Marius0188Author Commented:
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
sachinpatil10dCommented:
Try this

MessageBox.Show("Value: " + arrValues.GetValue(0));
0
 
Marius0188Author Commented:
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 :)
0
 
Marius0188Author Commented:
Sorry  I keep on referring to PBL but meant PLC...
:)

My mind is on something else....
0
 
Marius0188Author Commented:
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...
0
 
Marius0188Author Commented:
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

0
 
Marius0188Author Commented:
I tried both options:

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

AND

Array arrValues = new List<string>(1).ToArray();
0
 
BuggyCoderCommented:
do this if you want to achieve this:-
var lstValues = new List<string> {"Joe"};
Array arrValues = lstValues.ToArray();
var val = (string) arrValues.GetValue(0);

Open in new window

0
 
Marius0188Author Commented:
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
0
 
BuggyCoderCommented:
Let me know what syncwrite method looks like.
0
 
Marius0188Author Commented:
Sorry about that:

void SyncWrite(int NumItems, ref Array ServerHandles, ref Array Values, out Array Errors);
0
 
BuggyCoderCommented:
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...
0
 
Marius0188Author Commented:
Thank Buggy :) I agree.
Will close this one and start new one...


Keep well :)
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 10
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now