[Introduction | Intended Audience
| Pre-requisites | Creating An OPC Server in VB
| How To Expose Tags | #One More Feature
| #Final Summary]
Introduction
Do you have a need to incorporate OPC Server functionality within your
existing Application? or do you need to create an OPC Server from scratch?
If the answer is YES to either of these questions, then this document will take
you step-by-step on how to accomplish this using Visual Basic 6 with the SLIK-DA
ActiveX Control. This detailed step-by-step guide will leave few questions with
regards to how quick and easily you can create your own OPC Server.
Intended Audience
- Developers; experienced and new
Pre-requisites
- Some familiarity with OPC, VB6 and
ActiveX Controls
- Visual Basic 5 or newer
|
Although this
example uses Visual Basic, the SLIK-DA can be used in the following
languages too:
- VB.NET and C#
- C++
- Borland Delphi
The same principles apply to the above
languages as those outlined in this tutorial.
|
Creating
An OPC Server
- Start Visual Basic
- When Visual Basic opens, you will be prompted what type of application to
create... choose the "Standard EXE" project.

..and click on the "Open" button.
- When the development environment opens, you will see your blank VB form.
We now need to include the SLIK-DA ActiveX control into our VB project.
- Right-click on the Visual Basic Toolbar (containing the textbox, label,
combo box etc) and you will see the following menu:

.
. from this menu, click on "Components"
- The window will open and you will scroll-down the list looking for the
"Northern Dynamic's SLIK-DA ActiveX Control"

.
. and then click OK.
- A new icon will be added to your VB Toolbar:

- We now need to place an instance of the ActiveX control onto our empty VB
form. So double-click on the SLIK-DA control (the last image in the above
graphic) and you will see the icon appear on your blank VB Form:

- With the SLIK-DA control still selected (see the handles around the
control in the above diagram) we will now access the Properties of the SLIK-DA
ActiveX.
- The first thing we will do is to give our OPC Server a unique identifier, a
Class ID. This is a long-number that is unique that will identify this OPC
Server. Locate the "CLSID" property in the VB property pane and
click on the drop-down, and choose "AutoGenerate" as shown below:

- The generated number will then appear in the CLSID property. The following
number was created on my computer (which will differ on your computer):
{C7ABE174-144B-4647-A377-7292044F2165}
Please note that these Class IDs are totally unique and are based on the
hardware where the class was generated, so your number will differ to this.
- We now need to do the same again, but for the AppID property. Once again,
choose "AutoGenerate" from the drop-down as shown below:

- We will now complete the "identification" of our OPC Server by
setting the following Properties for the SLIK-DA control in the ACTUAL code
of our program. Simply Double-click on the empty form and the VB code window
will open at the "FORM_LOAD" event. The properties we are about to
set are:
- ProgID = "my.First.OPCServer.1"
This is the name of the Server that users will see when browsing a list
of available OPC Servers on your computer.
IMPORTANT NOTE: The ".1" on the end of the name references the
version number of the OPC Server. It is important that you place a
number here as the SLIK-DA ActiveX control will not allow you to leave
it without a .versionnumber.
- AppName = "My First OPC Server"
This is the humanly-readable name of the OPC Server.
- Description = "My First OPC Server created with SLIK-DA"
This is a humanly-understandable description of what the Server is/for
etc.
- VendorName = "My Company"
The name of your company.
- This information will be entered into the Form_Load event as shown
below:

- We have done everything so far to "Identify" our OPC Server, but
we have not yet Registered it with Windows. We can do this from within our
program by calling the "RegisterServer" method built into
the SLIK-DA control.
We will now append our FORM_LOAD event by adding a line to Register the OPC
Server so it can be seen by OPC Clients, BUT we will do this via a
command-line parameter because we do NOT want to register/unregister the OPC
Server each time the software is opened or closed, this would prevent the
server from being used by OPC Clients while the server is not running.. so the intention here is to allow
the user to register the server via a command-line parameter once
(preferably after the server has been installed), and
likewise to unregister the server too (preferably when the OPC Server is
uninstalled). We only want to register once, and
unregister when the software is being removed... the form_load event will
incorporate the command-line parameter ability and so now will look like
this:

Now we need to compile this application into an executable, so to do that,
we simply go to the FILE menu and use the option "MAKE .... .EXE"
- the project name you saved will appear in place of the ....
Then click on the Windows START button, choose RUN. Now locate the
executable you just built and select it so that it appears in the Browse
dialog as shown below:

... and then edit the line so that we can add the command-line parameter to
register the server:

... now click the OK button. The server will be launched, registered and
closed... this will most likely happen so fast that you will not see
anything happen on screen, this is OK.
The next step is to verify that the OPC Server is listed as an installed
server. So open an OPC Client and browse the available Servers on your
computer, the OPC Server we are creating in this tutorial should be
displayed.
Summary So Far...
So far, we have:
- Created a new VB Standard Application
- Added the SLIK-DA ActiveX Component to our VB application
- Defined our OPC Server by means of identifying it's ProgID, AppID,
AppName,
Description and Vendor properties
- Made the OPC Server able to be Registered and Unregistered via
command-line parameters so we can make it
available/unavailable to the public.
How To Expose
Tags
- We will now define the Tags that our OPC Server will expose to the public.
The SLIK-DA control allows us to define a "Collection" of
OPC Tags which we have full-control of.
We will add a new Procedure called
"CreateTags" that will create our Tags as shown in the
picture below:

NOTE: That the 3rd line shows "Dim
myTags as ISLIKTags", this needs to be defined globally in the
form, i.e. at the very top of the form or in a module somewhere.
- We will now tell our FORM_LOAD event to process the sub-procedure we just
created by calling it and we will also tell our OPC Server to actually Start
by calling the "StartServer" method:

- We are now ready to Test our OPC Server to see if the Tags we have created
are indeed publicly available. The screen below shows the available Tags
from an OPC Client:

- At present, our OPC Server does not do anything other than exist and
expose OPC Tags to the world.
As of now, we can not Read or Write to these Tags,
which is the functionality that we are going to implement right now...
- We are going to modify our Tags so that they have an initial value.
We are going to directly modify the Tags themselves, the same tags we
created earlier in the MyTags collection...
We will later call this method from the Forms load event

- Now to resume by adding our Read Handler...
In the Code window of VB, click on the OBJECT BOX drop-down and select the
SLIKSERVER1 object, as shown in the following example:

- Then in the EVENTS drop-down box, choose "OnRead" as
shown in the following example:

- VB will then create the heading for this event.
- We will add a handling script within this procedure. This Event is fired
when an OPC Client requests a Device Read (IOPCSyncIO::Read()) for any number of Tags. Therefore,
this Event houses a Collection of Tags that will be passed back directly to
the Client, after we have handled them:
The REALLY IMPORTANT thing to note here is that this procedure IS
ONLY required when the client has specifically asked for a READ
on a specific tag, or group of Tags. For MOST OPC Client applications
you will want to use subscription reads, which means that the client does NOT
call this Read method (that we are about to create) but instead they simply
subscribe to a tag and then server automatically "pushes"
the updated tag value to the client as soon as the Tag's value changes, this
is something that the SLIK-DA can automatically do for you. We will take a
look at this in just a moment.
Remember, OPC Clients call this READ method when they absolutely NEED
the value of a tag right NOW. The value needs to be as current as possible.

NOTE: The constant "sdaGood" may need to
be replaced by "sdaSGood" - depending on the version of SLIK-DA you have.
This procedure basically iterates through the collection of Tags as
passed-into the Event. Each Tag is processed in the SELECT..CASE statement
to see which tag is being processed, using the Tag Name as our search basis.
Once the correct tag has been identified, it's value is set to that of the
equivalent Variable we defined earlier.
If your existing application has variables containing values that you want
to provide your OPC Clients, then you can assign those variable-values to
your Tags instead.
- Now that we have created the ability to Read our OPC Tags, and that those
Tags will contain the values of the variables we defined in our VB
application - it is time to test this to see if it works.
- I am using the Software Toolbox OPC Quick Client application to connect to
our OPC Server, to browse the Tags and to write to them using the Sync
Device Read method. This is shown in the following image:

- From within my OPC Client application, I click on the "Synchronous
Device Read" option and wait for the new data value to show-up. On
my test, this was almost instantaneous.
- A moment ago I told you about "subscription reads".
Subscription reads differ from the type of Read that we just did in that
instead of the Client asking the Server for the value of a Tag, the OPC
Server instead updates the client with the tags value whenever the tags
value changes. This is often referred to as "subscription reads"
or "exception based reporting" or "callbacks".
To use this kind of method, you only need to write a value to our SLIK-DA
tag. So lets add a simulation ability to our server by means of a VB Timer.
We will add a VB Timer that will modify our tag values once every second. So
drag-n-drop a VB Timer onto your form. Then set the Timers properties to
enable the timer, and the interval should be set to 1000. See the screen
below:

Now double-click on the Timer itself to open the Timer event code. We will
simply add some code to modify the contents of our Tags:

Now to test these subscriptions, simply open the OPC Quick Client again, and
then browse and add the tags as you did before. But this time, you do not
need to right-click on the tag do see its values. This time, the OPC Server
(SLIK-DA) will automatically PUSH the latest Tag values into the Quick
Client. This "push" is instantaneous, i.e. whenever the SLIK-DA
tag's value changes, it causes the SLIK-DA to push the value to all
"listening" OPC Clients.
In real-world servers where you have data that you have collected from a
device for example, upon reading that data you would directly update the
SLIK-DA Tags as shown in this particular example. By doing this, the OPC
Clients would automatically receive notifications of the new tag-values for
those tags that the OPC Client has subscribed to.
- Now to add the ability for the client to be able to WRITE to our SLIK-DA
Tags.
- Just as you created the "ONREAD" event by selecting it from the
Event-drop-down box, this time you will select the "ONWRITE" event
instead. This time we will enter code that will act and look very similar to
that of our "ONREAD" event. Instead of populating Tag values with
our Variable values, we will instead populate our Variable values from our
current Tag values as passed-in by the client:

NOTE: The constant "sdaGood" may need to
be replaced by "sdaSGood" - depending on the version of SLIK-DA you have.
- Now our OPC Server has the ability to receive both Read and Write
requests.
One More Feature
- let's introduce 2 more events that will be very useful:
- onClientConnect
- onClientDisconnect
- Simply select these Events from the Event-drop-down combo and enter the
desired code. In our example, we will simply display a message box
confirming a connection has occurred, and when a client has disconnected:

Final Summary
In this document, you have learnt:
- How to insert the SLIK-DA ActiveX control into a Visual Basic Project
- How to configure your own OPC Server, Register it with Windows and Start
the OPC Server
- How to Test your OPC Server is available by Browsing for it
- Exposing Tags to the Public
- Testing those Tags are publicly accessible via an OPC Client Tag Browser
- Adding Event handling for "Sync Device Read" and "Sync
Device Write" method calls from an OPC Client.
- Letting the OPC Server automatically update OPC Clients with Tag values as
and when they change (subscription reads).
If you would like the VB6 Source code to this project, then
click on the link below:
Download VB6 Source Code
| Download Microsoft Visual C++ Equivalent