Solved

C# Calling external ActiveX method - array parameter wrong type

Posted on 2012-03-20
8
1,500 Views
Last Modified: 2012-08-22
Good day,

I am building a C# application that writes values to a Siemens PLC.
Using the Siemens' ActiveX control and are following their example of VB.NET code but I get the following error:
"Specified array was not of the expected type".

This is my C# code:
        private const string PROG_ID = "OPC.SimaticNet.1";
        private OPCSiemensDAAutomation.OPCServer opcServer;
        private OPCSiemensDAAutomation.OPCGroup opcGroup;
        private OPCSiemensDAAutomation.OPCItem[] opcItems = new OPCSiemensDAAutomation.OPCItem[1];

        public frmMain()
        {
            InitializeComponent();
            connectOPCServer();
        }

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

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

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

                //var lstErrors = new List<int>();
                //Array arrErrors = lstErrors.ToArray();
                Array arrErrors = new List<long>().ToArray();

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

                opcGroup.SyncWrite(1, ref arrHandles, ref arrValues, out arrErrors);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception: " + ex.Message);
            }
        }

Open in new window


I understand or believe that my array parameter parsing to the method is of wrong type but I can not see which one as I am using identical types to their VB.NET example which is working.


This is a snipped from the Siemens documentation and also the VB example:

7.4.2.2 SyncWrite
Description
This method writes values for one or more items of a group. The function is executed
completely. The values are written to DEVICE; this means that the function should not be
returned until it is certain that the device has accepted the data (or rejected the data).
Syntax
SyncWrite (NumItems As Long,
ServerHandles() As Long,
Values() As Variant,
ByRef Errors() As Long)
NumItems
NumItems specifies the number of items to be written.
ServerHandles
ServerHandles is an array of server handles for the items to be written.
Reference Automation Interface
7.4 The OPCGroup object
Industrial Communication with PG/PC Volume 2 - Interfaces
722 Programming Manual, Release 06/2011, C79000-G8976-C197-10
Values
Values is an array of values.
Errors
Errors is an array of long values that specify whether the items were written successfully.
Remarks
The status of the group or item (active/inactive) has no influence on the write method.
Example
Private Sub WriteButton_Click()
Dim Source As Integer
Dim NumItems As Long
Dim ServerIndex As Long
Dim ServerHandles() As Long
Dim Values() As Variant
Dim Errors() As Long
NumItems = 10
For ServerIndex = 1 to NumItems
' specifies which item will be written
ServerHandles(ServerIndex) = AnOPCItemServerHandles(ServerIndex)
Values(ServerIndex) = ServerIndex * 2
' the value of the item can be any value here
Next ServerIndex
OneGroup.SyncWrite NumItems, ServerHandles, Values, Errors
For ServerIndex = 1 to NumItems
' processes the errors
TextBox(ServerIndex).Text = Errors(ServerIndex)
Next ServerIndex
End Sub

Open in new window


Please assist, thanks all :)
0
Comment
Question by:Marius0188
  • 6
  • 2
8 Comments
 

Author Comment

by:Marius0188
ID: 37740989
I might add:
The error I received is when writing the value when calling sycnwrite() method.

:)
0
 

Author Comment

by:Marius0188
ID: 37741001
And this is a working VB script example as provided by Siemens:

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


Could it be C# does not support Variant types?
What is the work around?

I have tried declaring my Values list<object> but still complaining about wrong array type for parameter.

Please help :)
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 37741167
where in the C# code the error triggered?
which line?
0
 

Author Comment

by:Marius0188
ID: 37741375
Okay I got the code to run without errors but the value that gets write doenst get updated on the PLC (see bold)

Code that fix the error:
var lstHandles = new List<int>();
                lstHandles.Add(opcItems[0].ServerHandle);
                Array arrHandles = lstHandles.ToArray();

                var lstValues = new List<object>();
                lstValues.Add("Toets");
                Array arrValues = lstValues.ToArray();
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Marius0188
ID: 37741381
The documentation is so misleading as they define different types for example: LongInt whereas it should be Int...

Okay next issue is.
Would it be a problem because the working VB script (Excel sheet) that does update the value on PLC uses Option Base 1 iow: all arrays start at ONE (1).

Whereas in C# the arrays are zero based index.
How can I mimic the same in C# if this could be the problem.

Any other suggestions as to what could be causing the problem are welcome :)
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 37741398
i didn't understand what is the problem now that u overcome the type error.
i don't see a problem in regards to zero based arrays.
i'll just try with a single entry array and see how's it runs.
0
 

Accepted Solution

by:
Marius0188 earned 0 total points
ID: 37741417
Okay for future reference only.
I solved the issue myself.

Maybe not clean but it works.
I simply filled the first two indexes of the array with same value and make the PLC believe I am only sending one value (element) array.

Array[0] = "SameValue" -> This will not be read
Array[1] = "SameValue" -> but this index will be read.

As I am always sending only one value in my array this will work for now.

Will leave this open for day or two for some better cleaner solutions.

Thanks...
0
 

Author Closing Comment

by:Marius0188
ID: 38319620
No other real solution that exactly solved my problem :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now