Home Buy Now !


This section gives a brief description of several common Local IO tasks. Each item is followed by links to one or more code examples. These code examples are also shipped with the product. Functional Overview.
 TagServer Startup and Shutdown

The only thing needed to start a tag server is to create it.  If you have servers attached you can connect them all by calling MyTagServer.Connect.  To shutdown you must call Release to free all of the objects and their underlying interfaces.

Examples TS VB Console , C# Console

 Get a List of Network Nodes

The TagServer provides a method FindNetworkNodes() that returns a SortedList of IONodes that represent all of the domain computers.
Example NetworkNodes

 Get a List of OPC Servers on a Node (OPCEnum)


The IONode class provides a method GetKnownServers() that returns a SortedList of IOServers that represent OPC servers registered on the node.  The node can be local or anywhere on the domain.  There are two versions of the method, one by default gets OPC DA 2.0 server and the other allows you to specify the OPC category type by passing a category Guid.


Example OPCEnum


 Connect to an OPC Server Using the Guid or a ProgID


If you get the IOServer from GetKnownServers() the ProgID and Guid are set for you. If you already know the ProgID or Guid you can set their properties and connect to the server without using GetKnowServers().  Either one of the ProgID or the Guid will work.  The Guid is used if it exists otherwise the ProgID is used.


Examples  VB Console , C# Console


 Get the OPC Items from OPC Server (Tag Browsing)


IOServers use the method GetServerTree() to return a tree of all of the OPC Items in the server .  The tree consists of IOTags with braches of IDNBrowsable.  The names are set along with the IOTag's AccessName, AccessRights, etc.


Examples  OPC Tag Browser


 Use the Provided TagServer Form to Create Nodes, Servers, Schedules and Tags


Call the TagServer.Show() or ShowDialog() methods to invoke the main UI form.  This allows users complete access for scheduling, reading and writing IOTags.


Examples TS VB Console , C# Console , VB Form , C# Form


 Create and use Nodes, Servers, Schedules and Tags Programmatically


TagServer contains IONode that contain IOServers that contain IOSchedules that contain IOTags.  Simply start at the top and use AddChild() and RemoveChild() to build the data tree of objects.  Use RemoveChild() to remove items.  Once a child item is added it Parent property can be used to access it's parent and a parent's Children list can be used to access it's child items.  (Do not add or remove children from the Children list.)  Once added to the tree an object can also use the Errored event methods, etc.


Examples VB Console , C# Console


 Send Error Messages


Error messages have three levels of severity, Error, Warning and Information.  You can send error messages via the Error(), Warning() and Information() methods of any IDNBrowsable derived object such as TagServer, IONode, IOServer, IOSchedule or IOTag. 


Examples Errored


 Use the Windows Event Log


Error messages can be sent to the Windows Event Log by enabling each level of severity with the TagServer.EnableWarningLog(), EnableErrorLog() or EnableInformationLog() methods.  You can set the application name that will show up in the Windows Event Log via the TagServer.LogSourceName property.


Examples Errored


 Handle Error Message Events


All error messages generated in the IO assembly will be sent to the client via the TagServer's  Errored event.  You may wish to log these to a file, display them to your user, or send them to the Windows Event Log.  First you need to create the event handler.  The sender of the event is always going to be the TagServer object but the event argument contains the sender of the message.  The event also contains a severity, timestamp the message string.  You will want to receive error messages as soon as possible when initializing, so subscribe to the Errored event right after creating the TagServer object.


Examples Errored, C# Form , VB Form


 Edit Object Properties


Each IDNBrowsable derived object can be edited with a standard form by calling its ShowDialog() method.


Examples C# Form , VB Form


 Handle PropertyChanged Events


Changing any property of an TagServer, IONode, IOServer, IOSchedule or IOTag will fire a PropertyChanged event to any subscribers.  If you want to change several properties of an object during editing, initializing, etc. you can suspend the events until you are done by using the object's BeginLoad() and EndLoad() methods.  Bracket your property changes between these two calls, and a single PropertyChanged event will be sent to each subscriber at EndLoad().  A single event contains a Hashtable which can include any number of property changes.  The senderobject can be cast to its type, and the properties can be accessed directly.  The Hashtable uses string name of the property as the hash key and the associated value is the new property value. You may want to dynamically subscribe and unsubscribe to an object's PropertyChanged events, depending on your application.  This can easily be done.  You can unsubscribe without being subscribed and not generate an error.  Also the server object checks and prevents a target object from subscribing multiple times to the same event.


Examples C# Form (subscribing) , VB Form (subscribing), C# Form (handling) , VB Form (handling)


 Detect Server Communications Failure


IOServers periodically test the underlying connectivity to the OPC server on a separate thread and then disconnect when the serer connection fails or times out.  You can set the watchdog period in milliseconds via the IOServer's WatchdogPeriod property.  Be aware that changing the WatchdogPeriod effects all clients.  An IOServer.ServerMode PropertyChanged event is sent which has the current value of ServerMode.StatusCheckFailure.  PropertyChanged events are also sent for the Connected property that indicates the server is no longer connected.


Examples  C# Form (handling) , VB Form (handling)


 Use the UI to Select an Item from the TagServer


Once the TagServer is populated with objects you can use the TagServer.SelectItem() method to allow the user to pick an item of a specified type from the tree.


Examples  C# Form (handling) , VB Form (handling)


 Poll Tags for Data Changes


Your application may work better if you poll the tags in the TagServer for changes instead of managing events from each tag.  The Quick Data client uses this method to update the main form's grids but uses events to update the tags data tab form.  Each tag has a Changed property that is set when new data arrives from the OPC Group.  It is intended that the tag's client clear the flag when the new value is used.  This allows you to quickly go down a list of tags and update only those that have changed.


Examples VB Console , C# Console


 Handle Updated Events from Tags


Your application design may be better supported by handling individual tag events or all tag event with the same event handler.  In this case your event handler will be called as soon as a new value arrives from the OPC Group.  Each tag Updated event contains a Sample object with the Value, Timestamp and Quality of the last update.  You can also access all of the the tag's properties as it is the event's source.


Examples  C# Form (handling) , VB Form (handling)


 Handle Updated Events from Schedules


You can subscribe to IOSchedules' Updated events.  IOSchedules do not pass Sample objects, but instead keep a list of the tags that were last updated.  Access these using the UpdatedTags property of the IOSchedule which returns a SortedList of references to the tags. Traverse the list and access each tag's values directly.


Examples C# Form (subscribing) , VB Form (subscribing), C# Form (handling) , VB Form (handling)


 Write Values to a Tag Using the Provided Form


IOTags provide a Write form that handles normal and array type tags.  To use the forms just call Write() on the tag.  The forms handle the validation and writing.  The forms can also accept the standard IEC 16# prefix for hex values (i. e. 16#fff0) or 2# prefix for binary (i. e. 2#00001111,00001111).  Values are written asynchronously so  the method returns before the write is complete.  A write timeout Error message is sent if the write confirmation is not received from the server within the timeout period.  Set the write timeout period at the IOServer level. 


Examples C# Form , VB Form  


 Write Values to a Tag Programmatically


You can programmatically write a tag by passing a system Object containing the new value to be written.  When using the programmatic method you should make sure the system Object passed in is of the same type as the IOTag's Value property.  If the IOTag's value is an array type, make sure that you pass in a simple array of system Objects.  Also, make sure the IOTag is writable by testing the AccessRights property for the AccessRights.Write bit. If the IOTag is read-only the Write methods return Status.errorReadOnly.


Writing to tags is an asynchronous operation. The write methods initiate the write and return immediately.  The containing IOSchedule object watches for a validation event from the lower level protocol handler and matches it up with the right tag.  If no validation event is received for the write operation within the timeout period, an error message ("Write timeout: TagName") is generated by a timer running in the IOTag.  The timeout period for write operations is configurable on a per-IOServer basis via the IOServer's WriteTimeoutPeriod property.


Save and Load the TagServer Configuration


The TagServer supports the ISerializable interface. You can serialize the entire TagServer configuration on a stream which will save or restore all of the parent/child objects and relationships.  There is also a TagServer.Dirty property that you can use to determine if there have been any property changes in any of the objects.  See the various Save and Open examples in the Form examples. Binary or SOAP (XML) formatters can be used.  Serialization propagates down the tree structure such that if, for example, you serialize the TagServer, all objects in the tag server configuration are saved/loaded.  If you serialize a specific IOSchedule object, the schedule and all of its IOTags a serialized.


Examples C# Form , VB Form  


 Save the Session State


The TagServer class has a State property that is a Hashtable of the session settings.  These include the last radix used, the forms' positions and sizes, the forms' splitter positions and grids' column positions.  If you want to save these settings between sessions simply serialize the State property to a file when exiting and de-serialize it back into the TagServer when starting up.



The following code examples saves and loads the TagServer state.

[Visual Basic]
stream = New FileStream("MyStateFile.dat", FileMode.Create, FileAccess.Write)
If Not (stream Is Nothing) Then
    formatter.Serialize(stream, MyTagServer.State)
End If


Dim stream As Stream = Nothing
If File.Exists("MyStateFile.dat") Then
    stream = New FileStream("MyStateFile.dat", FileMode.Open, FileAccess.Read)
End If
If Not (stream Is Nothing) Then
    Dim formatter As New BinaryFormatter()
    MyTagServer.State = CType(formatter.Deserialize(stream), Hashtable)
End If



Stream stream = null;
stream = new FileStream("MyStateFile.dat", FileMode.Create, FileAccess.Write);
if (stream != null)
    formatter.Serialize(stream, MyTagServer.State);


Stream stream = null;

    stream = new FileStream("MyStateFile.dat", FileMode.Open, FileAccess.Read);
if (stream != null)
    BinaryFormatter formatter = new BinaryFormatter();
    MyTagServer.State = (Hashtable) formatter.Deserialize(stream);

Copyright © 2004-2017 Industrial DOT NET, Inc.