Connect to OPC Server using Vb.net

Hi Experts,

I need help to connect to the OPC server using VB.net and read its values and also write values to it.

Please help.

Thank u.
RadhaKrishnaKiJayaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
Which OPC Server ? Does it have an API available?
http://www.opcconnect.com/freesrv.php
0
RadhaKrishnaKiJayaAuthor Commented:
Thank you for your reply. It is Moxa Active OPC server. It came with IOLogik device.
0
Bob LearnedCommented:
Without really knowing anything about Moxa, my first inclination would be to find some kind of supported API for .NET:

Effortless Linux I/O Programming with Moxa MXIO library
http://www.moxa.com/Event/Net/2010/Linux_MXIO/
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

RadhaKrishnaKiJayaAuthor Commented:
I downloaded the library. It has the API for .net . But not sure how to use it.  I am tring to connect to the OPC server but having hard time. It is saying the command is

MXEIO_Connect(ByVal szIP() As Byte, ByVal iPort As UInt16, ByVal nTimeOut As UInt32, ByRef hConnection As Integer) As Integer

where szIP : - IP address of the Ethernet I/O device to be connected. When I give the IP address as "***.***.***.***" form it says can not convert string into byte array. I have no idea how to supply the IP address.

Thanks.
0
ChloesDadCommented:
It probably wants the ip address as a byte array

dim IPAddress as new byte() {a,b,c,d)
0
Bob LearnedCommented:
Here is a reference for the API:

ioLogik MXIO DLL API Reference - Moxa
http://www.moxa.com/doc/manual/ioLogik%5CMXIO_library%5CioLogik_MXIO_DLL_API_Reference_v2.pdf

Use MXEIO_Connect to connect to the Ethernet I/O using IP:Port (e.g., 192.168.8.1:502).
MXEIO_Connect should return a handle.
0
RadhaKrishnaKiJayaAuthor Commented:
Thank you very much for trying to help me. Actually I am using IOLogik 1200 series. I have downloaded the library. Not sure how to use it. I am not getting any sample code anywhere.

Tomorrow I will try to give the IP address in byte array and let you know.

Thank you.
0
RadhaKrishnaKiJayaAuthor Commented:
Hi,

I gave the IP address as,
 dim IPAddress as new byte() {192,168,1,245)
It is giving me error "End of statement required". If you can tell me what is wrong here then that will be very helpful.

Thanks
0
Bob LearnedCommented:
You have a syntax error.  The last character should be a brace not a parenthesis
0
RadhaKrishnaKiJayaAuthor Commented:
I am sorry. But I changed that still getting the same error.

Thanks.
0
RadhaKrishnaKiJayaAuthor Commented:
Hi,

I got some code to connect to OPC server. But I do not know how to get these two import class.
Imports OPCAutomation
Imports System.Runtime.InteropServices

Any help is greatly appreciated.

Thank you.



Code
----------
Imports OPCAutomation
Imports System.Runtime.InteropServices

Public Class Form1
Dim WithEvents ObjOPCServer As OPCAutomation.OPCServer
Dim WithEvents ObjOPCGroups As OPCAutomation.OPCGroups
Dim WithEvents ObjOPCGroup As OPCAutomation.OPCGroup
Dim ObjOPCItems As OPCAutomation.OPCItems
Dim ObjOPCItem As OPCAutomation.OPCItem
Const NoOfItems = 2

Dim Array_Items(NoOfItems) As String
Dim Array_ClientHanlers(NoOfItems) As Integer
Dim Array_ServerHandlers(NoOfItems) As Integer
Dim Array_Values(NoOfItems) As Object

'########## Form Load Event  ###################################
Private Sub Form1_Load(ByVal sender As System.Object, _
                        ByVal e As System.EventArgs) Handles MyBase.Load
 Call StartOPCClient("MOXA_ACTIVE_OPC_SERVER\IO1")
 
End Sub

Private Sub StartOPCClient(ByVal OPCServerName As String)
      Dim liCounter As Integer
      Dim gpServerHandlers As Array
      Dim gsErrors As Array
      Try
            ObjOPCServer = New OPCAutomation.OPCServer
         '##### Initialize OPC Server ################################
            ObjOPCServer.Connect(OPCServerName, "")
            ObjOPCGroups = ObjOPCServer.OPCGroups
            ObjOPCGroup = ObjOPCGroups.Add("OPCGroup1")
         'Add OPCGroup
            ObjOPCGroup.UpdateRate = 1000
            ObjOPCGroup.IsActive = False
            ObjOPCGroup.IsSubscribed = ObjOPCGroup.IsActive
            ObjOPCItems = ObjOPCGroup.OPCItems
            'Build OPCItems Array
            For liCounter = 1 To NoOfItems
                Array_Items(liCounter) = "OPCClient.Device" & liCounter
                Array_ClientHanlers(liCounter) = liCounter
            Next
            'Add OPCItems
            ObjOPCItems.AddItems(NoOfItems, Array_Items,Array_ClientHanlers, _
                                 gpServerHandlers, gsErrors)
            'Get the server handlers
            For liCounter = 1 To NoOfItems
                If gsErrors(liCounter) = 0 Then
                    Array_ServerHandlers(liCounter) = gpServerHandlers(liCounter)
                Else
                    MsgBox("Item" & liCounter & "has problem", _
                           MsgBoxStyle.Critical, "OPC Client")
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "OPC Client")
        End Try
    End Sub
End Class
0
Bob LearnedCommented:
OPCAutomation would seem like a COM component, because of the reference to System.Runtime.InteropServices.  That sounds like it is the OPC automation wrapper from the OPC foundation:

Automation Wrapper
https://opcfoundation.org/developer-tools/developer-kits-classic/automation-wrapper
0
RadhaKrishnaKiJayaAuthor Commented:
Thank you for reply. I think this is what I need but it is not installing on my machine because of compatibility issue. I have Windows 7. Any other suggestion?
0
Bob LearnedCommented:
If you are looking for free, here is a page that pulls some information together:

Free OPC Software
http://www.opcconnect.com/freestuf.php

If you are looking to pay, there are some options, but I don't know about your specific device.  You might try contacting the manufacturer for your device, and ask them for options.
0
RadhaKrishnaKiJayaAuthor Commented:
Actually we got the device and it has its own OPC server. Main problem is how to connect it from vb.net.

Thank you.
0
Bob LearnedCommented:
Does the OPC server come with an API?
0
RadhaKrishnaKiJayaAuthor Commented:
Yes. (MOXA_ACTIVE_OPC_SERVER\IO1)
0
Bob LearnedCommented:
I am not sure what that descriptor means, but that looks like the server software, which is not an API that interacts with the server.

I did find this:

Moxa Active OPC Server Lite
http://moxa-active-opc-server-lite.software.informer.com/
0
RadhaKrishnaKiJayaAuthor Commented:
Thank you very much for trying to help me. Actually I have this installed on my machine already. But how to access this server's data from my vb.Net program. That is my whole problem.
0
ChloesDadCommented:
To get data from an OPC server involves 4 steps

1 - Connect to the OPC server. This normally returns a server handle that is used in the rest of the process
2 - Create a group on your connection to the server
3 - Add items to the group
4 - perform read/writes on the items in a group.

At work we use https://advosol.com/pc-1-3-opcdanet.aspx

Download their OPC client it has a sample code that may help you. It is payware but the evaluation will allow you to run for 30 minutes before having to restart the program.
0
RadhaKrishnaKiJayaAuthor Commented:
Do u think to connect OPC Server we need to buy OPC Client?

Moxa has some commands like this. Do u think I can use them from VB.Net?

VB.NET
------------
MXEIO_Connect(ByVal szIP() As Byte, ByVal iPort As UInt16, ByVal nTimeOut As UInt32, ByRef hConnection As Integer) As Integer
Arguments:

szIP
 IP address of the Ethernet I/O device to be connected.
 
wPort
 TCP port number of Ethernet I/O device.

Please use 502 for ioLogik 4000 and ioLogik 2000.
 
dwTimeOut
 Timeout value for establishing a network connection with the ioLogik Ethernet Adapter.

The unit is in milliseconds.
 
hConnection
 Handle for the I/O device connection.

Return Values:

Succeed
 MXIO_OK
 
Fail
 Refer to Return Codes.
 


Thank u.
0
ChloesDadCommented:
That looks like a command to connect to the OPC server, the return value will be an error code. Most generic OPC client APIs also need a server name, but it looks like this one will only connect to its own OPC server.

If its on the same PC then the IP address will be 127.0.0.1
0
RadhaKrishnaKiJayaAuthor Commented:
Thank you for your reply. I found the code to connect to the server. But it is giving me the error "Unable to load DLL 'MXIO_NET.dll". But the dll file is present in "C:\Program Files (x86)\Moxa\MXIO_NET\dll\x64\MXIO_NET.dll" . I tried to add a reference to it but it is giving error. Please suggest what can I do.

Private Sub cmdConnect_Click() Handles cmdConnect.Click
        Dim nRet As Long             'Error message

        'creates a connection to the port of the device that is to be controlled
        If txtPassword.Text = "" Then
            nRet = MXIO_VB.MXEIO_E1K_Connect({192, 168, 1, 245}, PORT, TIMEOUT, hConnection, {})
        Else
            'nRet = MXEIO_E1K_Connect(txtIP.Text, PORT, TIMEOUT, hConnection, txtPassword.Text)
        End If

        'checks whether the call succeeded
        If nRet <> MXIO_OK Then
            lblMsg.Text = "MXEIO_E1K_Connect Fail. Error Code : " & CStr(nRet)
        Else
            lblMsg.Text = "Connect OK !"
        End If

    End Sub
0
ChloesDadCommented:
What error is it giving, what is the CPU type of your application. It may be that the dll is 32 bit, so you should change the CPU type to X86 not Any CPU.
0
RadhaKrishnaKiJayaAuthor Commented:
How to find the CPU type? How to change it to X86? Please help.

Thanks.
0
ChloesDadCommented:
What version of VB.Net are you using?
0
RadhaKrishnaKiJayaAuthor Commented:
Visual studio 2012.
0
ChloesDadCommented:
In that case then you should be able to right click on the project in solution explorer and select configuration settings, you should be able to change the cpu type there.
0
RadhaKrishnaKiJayaAuthor Commented:
I changed it but did not work. Still getting the same error DLL not found.

Thanks.
0
ChloesDadCommented:
I just noticed that the dll is "C:\Program Files (x86)\Moxa\MXIO_NET\dll\x64\MXIO_NET.dll"

Is there an equivalent folder called either x32 or x86 after dll ie

"C:\Program Files (x86)\Moxa\MXIO_NET\dll\x86\MXIO_NET.dll" or

"C:\Program Files (x86)\Moxa\MXIO_NET\dll\x32\MXIO_NET.dll"

If so use that one rather than the 64bit one
0
RadhaKrishnaKiJayaAuthor Commented:
Thank you very much for trying to help me. Actually I am not using the DLL directly. The manual says I need to add the MXIO.VB class file in my project. Then the DLL is called from MXIO.VB file. Do I need to give any reference to MXIO_NET.dll? If so, how can I do it. I tried thru Add reference option but it is giving error.
0
ChloesDadCommented:
Do they give an example project for you to open, or just the vb file. What error do you get (it helps to say this rather than just that it gives an error as we are not mindreaders ;) )
0
RadhaKrishnaKiJayaAuthor Commented:
Yes. They have some sample programs in vb6 and vb.net. Vb.net programs are not opening on my machine. But I got some code from vb6 program. When I run , I getting the error "Unable to load MXIO_Net.dll. The specified module could not be found.".

Dim hConnection As Long             'Connection handle

    Const PORT = 9900                                'TCP port number
    Const TIMEOUT = 2000                            'Timeout

    '*******************
    ' create connection
    '*******************
    Private Sub cmdConnect_Click() Handles cmdConnect.Click
        Dim nRet As Long             'Error message

        'creates a connection to the port of the device that is to be controlled
        nRet = MXEIO_Connect({192, 168, 1, 245}, PORT, TIMEOUT, hConnection)
       

        'checks whether the call succeeded
        If nRet <> MXIO_OK Then
            lblMsg.Text = "MXEIO_E1K_Connect Fail. Error Code : " & CStr(nRet)
        Else
            lblMsg.Text = "Connect OK !"
        End If

    End Sub
0
ChloesDadCommented:
Have you actually installed the MXIO driver on your machine as it sounds like you are missing some files
0
RadhaKrishnaKiJayaAuthor Commented:
I think so. I have downloaded all the libraries the manual is asking to do.

Thanks.
0
ChloesDadCommented:
There is a difference between downloading them and installing them, do you have a link to the library that you are using?
0
RadhaKrishnaKiJayaAuthor Commented:
0
ChloesDadCommented:
Thanks, I have found what I think that the problem is. I opened one of their example projects and in their code they use dllimport, as a result you need to copy the mxio_net.dll file into the bin/debug folder. Once I had done that the program ran successfully.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RadhaKrishnaKiJayaAuthor Commented:
Hi,

My program is in "C:\Users\NitaiNDCh\Documents\Visual Studio 2012\Projects\TestOPC\TestOPC.sln" folder.
I copies the .dll into
"C:\Users\NitaiNDCh\Documents\Visual Studio 2012\Projects\TestOPC\TestOPC\bin\Debug\MXIO_NET.dll" folder. But still getting the error.

Am I doing anything wrong?
0
RadhaKrishnaKiJayaAuthor Commented:
Hi,

The example is working when I copied the .dll file into bin/debug  folder. But still having problem when I am trying to connect from my  program. I have created a form with a "Connect OPC" button. I added the MXIO.vb file and also added the MXIO_NET.dll file into  "C:\Project\ConnectOPC\ConnectOPC\bin\Debug" folder. But still it is not connecting to the OPC server. Now I am getting this error "An attempt was made to load a program with an incorrect format." . Please give me some suggestion.

I really appreciate all your help.

Thank you very much.
0
ChloesDadCommented:
That sounds like a 32/64 bit issue, so change it to 32 bit (x86) and try again.
0
RadhaKrishnaKiJayaAuthor Commented:
Finally it is working. I had to change the .Net frame work version from 4.5 to 3.5. Thank you very much for your excellent help and support.

Thank you again.
0
RadhaKrishnaKiJayaAuthor Commented:
Thank you very much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.