Connecting To OPC Server with Visual Basic

Sceptre

Member
Join Date
Dec 2003
Posts
28
I am using VB6 with a Moeller PLC and using OPC Server to connect to the PLC. I am trying to work in some error checking in case the PLC is not powered on while the computer tries to connect to it. Right now, if the PLC is not powered on, the program crashes with the message:
Code:
PCU.exe has generated errors and will be closed by Windows. You will need to restart the program.
An error log is being created."

My code for connecting to the PLC is as follows:
Code:
Set server = New OpcClientX15.OPCServer
server.Connect "CoDeSys.OPC.02"
Set group = server.OPCGroups.Add("Group1")
group.UpdateRate = "1"

Dim item As OpcClientX15.OPCItem
Set item = group.OPCItems.AddItem(".PRESSURE", 0)
Label1(0).Tag = item.ServerHandle

I am familiar with error checking in VB (using OnError etc) but I can not seem to trap this one.
Does anyone know how to trap this error? Or maybe there's an easier way to detect if OPC is actually connecting the PLC?

Any information would be greatly appreciated.


Nick
 
It sounds like your opc server for the moeller plc is generating the error. Not your VB program. What is PCU.exe? What does the error log say?

When you single step(F8) through your program, Is the error generated on one of your lines of code? I'm guessing it will happen when you get to additem, or when you initiliaze the server.

Also try a slower update rate at first, use '1000' until things get working.
 
Sceptre,
My advice in not to use (OnError)or its similar functions.

For me (I use Software toolbox OPC servers) I do the following:

before I start updating my controls (pumps, pipes, labels ..), I check for the (Diagnostic Tags)

These tags "tell" the client the status of the communication. They are seven tags (SuccessfulReads, SuccessfulWrites, FailedReads, FailedWrites .. ) you will notice from the names the importance and the function of these "built-in" tags.

So if you are using the same server I use, Use these tags before every every thing (i.e. a big IF Then Else that surrounds every thing).

And if you are not using the same server, search in the documentation for something similar to this.

By the way, Iam interested in Moeller PLC, Can you give us more info about the communication protocol and OPC vendor.
 
Last edited:
hey guys, thanks for the input

Gerry, PCU.exe is my program. Thats the error when I get when I have the program compiled into an exe. You are also correct about when I receieve the error....it initializes all the servers file, creates a group, etc but when it attempts to add the first item, it crashes.

Ayman, I am currently using Metadynamics OPCClientX but lately I find it hard to get documentation on the software. They used to have a good help forum, but have removed it from their site since I have been using the client. The server I am using is provided with the Moeller PLC...its CoDeSys OPC-Server 2.3.7.4


I am doing some searching through some sample programs for metadynamics software, but can't seem to find anything about diagnostics tags...will keep looking. I also have a demo installed that I was using to check all the tags on the PLC a while back when I was having some problems. Its called Softing OPC Toolbox Demo Client....i'm not sure if this is the same one you are using or not. If you can recommend better software than metadynamics, it would be great.

Thank you very much for your time and information.



Nick
 
Sceptre,

I haven't done any real projects yet but have used the Matrikon free OPC Explorer software to get info from an XC201 PLC for demo purposes. Also, you can get their OPC2DDE software for a trial period (I think 30 days). With this I was able to drag and drop OPC tags into Excel 2003.

I also have a sample excel spread sheet from Moeller in Canada that is supposed to get data directly into Excel but it doesn't work with Excel 2003. They claim it works with 2002 and lower but I don't have that software to try. If you want I can e-mail it to you.

I think the Excel 2003 issue probably has something to do with security "features" the Microsoft has added but I haven't had time to track this down. I also am having problems using the software to program the new Moeller HPG units which is also based on Excel.

The Matrikon site is at:
http://www.matrikon.com/

Keep us posted.

nOrM
 
ndzied, thanks for the website...gonna have a look around there now. As for writing into excel, I have my VB program take care of that for me. I needed VB to monitor and change variables on the PLC when certian things happened and also log variables so I implemented into VB the process for writing an Excel file....so far i've tried it on office 2k and office xp and it appears to work well.
 
Sceptre,

It may just be that you have the wrong connection string.

Use the opcbrowser object, or some kind of opc explorer, such as ndzied1 suggested, to make sure the string is correct.

I've had similar problems when my strings are incorrect.

I also use metadynamics except the .net version. I've never called them for help, so I'm not sure what they can offer.
 
what do you mean by connection string?

the code i'm using is in my original post...is that correct?
 
Yes your code looks correct.

What I mean by connection string is this

".PRESSURE"

in this line

Set item = group.OPCItems.AddItem(".PRESSURE", 0)
 
Gerry M said:
Yes your code looks correct.

What I mean by connection string is this

".PRESSURE"

in this line

Set item = group.OPCItems.AddItem(".PRESSURE", 0)

ah...ic. The only tags I can use .NAME for is global variables. The rest need locations before the var name.
 
i've been doing a bit of reading on the metadynamics stuff and finally found some status data like OPC Error Codes and Server Status States, among some other useful ones.

the problem is, its labelled as Public Enumerations and i can't figure out, for the life of me, how to use it in Visual Basic 6

if anyone has any information, it would be greatly appreciated.

heres a link to what i've been looking at:
Object Model Classes and Public Enumerations


Nick
 
Greetings Sceptre,
There two things I would like to say:

First: To clarify your problem to me and the others:
Iam understanding that you are having no problem in getting data from you OPC server using the OPC client.
The problem appears only when you are trying to run your VB application at the time of no communication between the PLC and the OPC server .... Am I right?

Second: Actually I don’t know what is the meaning of the term "Public Enumerations" !!

So if your problem still taking a place, I would recommend another OPC client, But I recommend this if and only if :
1- you have the time and the chance to move to another software.
2- You gave up with your current OPC client.

I would recommend www.softwaretoolbox for your OPC client.

These guys are very helpful and have a great technical support. Sometimes I E-mail them several times in the same day and they always reply offering any help they can do.

Another reason is that they have very good documentation and real visual basic examples and codes.

Best luck

Ayman
 
Last edited:
Ayman,

With regards to your first question, you are correct. The program works and runs perfectly. I started out with a separate problem, which is why I want to be able to check OPC Server Status as well. The first time the program runs on a fresh boot of Windows, it takes a few seconds for the OPC Server to initialize. I have some delays in there now, but I wanted to put in a check to watch the status.

thanks a million for the softwaretoolbox site...will definitely have a look into it.


Nick
 
Sceptre wrote on Today 06:44 PM:
Hey,

I am currently trying to convert my OPC code in VB from Metadynamics syntax to Software Toolbox syntax. I am having one problem which I was wondering if you could help me with.

With Metadynamics, if I wanted to change a variable in the PLC, i would assign a var name to the PLC by using something like this:
Code:
Set pressureValve1 = group.OPCItems.AddItem("PLC_PRG.VALVE_CONTROL.PVALVE1", 25)

Then to write the var to the PLC i would go pressureValve1.write (value)

How do I go about writing vars to the PLC using software toolbox??
sorry for the simple question, i just can't seem to find an easy way to do it

Thank you very much for your time and help.


Best Regards,
Nicholas Rose

First of all I would like to say that I am happy that your problem seemed to be resolved.

Seconed: Regarding your question about "writing vars to the PLC" as you said, Here is an example:

Assume that you want to write to a single Bit in the PLC when clicking on "Command1" button, This bit is "addresed" in the OPC server as "Group.Subgroup.item"
Here is the code:
Code:
Private Sub Command1_Click()

Dim [COLOR=blue]VarValue[/COLOR]  As Variant
Dim [COLOR=red]VarName[/COLOR]  As String

[COLOR=blue]VarValue[/COLOR] = "Group.Subgroup.item" 
[COLOR=red]VarName[/COLOR] = 0 [COLOR=green]' Or 1[/COLOR] 

result = Form_name.OPCData1.WriteVariable([COLOR=blue]VarValue[/COLOR], [COLOR=red]VarName[/COLOR], 0)

End Sub
Notes:
1- Form_name is the form that contains the OPC client Active X control.
2- If I still remember, The variable that contains the "address of the tag in the OPC sever (i.e. VarValue = "Group.Subgroup.item") must be Variant type.

Also read the articles in This link. You will find very useful information.

Best regards.
 
Last edited:

Similar Topics

Hello PLC expert i need your help. i want to use OPC communication and Matlab to read and write data from/to a Logix5561. A Topic is...
Replies
0
Views
1,641
I have KEPserverEX on a host machine and FactoryTalk Hostorian SE on a virtual machine. The machines can ping each other so I can in theory...
Replies
1
Views
2,915
I have a moisture meter that I want connected so we can log data from it. The specs mention that it supports OPC-DDE, would that mean I can...
Replies
7
Views
2,592
hi, I am currently working on a project that requires access to the data inside plc using Codesys OPC server v2.3 and I want the data to be...
Replies
0
Views
3,003
Hey guys, I am trying to set up a Matrikon OPC server to connect to a Controllogix gateway. If I use RSLinx, I can see all my PLC5's...
Replies
1
Views
2,272
Back
Top Bottom