Kepware Technlgies ClientAce: Creating a Simple Windws Frm July, 2013 Ref. 1.03 Kepware Technlgies
Table f Cntents 1. Overview... 1 1.1 Requirements... 1 2. Creating a Windws Frm... 1 2.1 Adding Cntrls t the Frm... 1 2.2 Initializing Glbal Variables... 2 2.2.1 Examples... 2 2.3 Adding the Frm Lad Actins... 3 2.3.1 Advising the Server Management Object fr Callbacks... 5 2.4 Subscribing fr Data... 5 2.5 Setting Items Active fr Plling... 9 2.6 Handling DataChanged Events... 9 2.7 Handling Changes t the Server State... 12 3. Testing the Prject... 13 4. Summary... 13 www.kepware.cm i ClientAce: Creating a Simple Windws Frm
1. Overview This dcument intends t demnstrate the basic steps needed t create a Simple Windws Frm applicatin in Visual Basic.NET (VB.NET) and C-Sharp (C#). Nte: The cding practices used in the fllwing examples are the preference f the develper. The prject cde was develped in Visual Studi 2010 using ClientAce 4.0. Unless nted therwise, the same steps and prcedures can als be used with ClientAce 3.5. 1.1 Requirements Visual Studi 2008 SP1 r higher. ClientAce 3.5 r 4.0. An OPC Server (like KEPServerEX versin 5.x). 2. Creating a Windws Frm 1. T start, create a new VB.NET r C# prject by clicking File New Prject. 2. In the list f prject templates, lcate and select Windws Frms. 3. Name the prject MyFrmApp and then click OK. 4. Next, use the Tlbx t drag several cntrls t the frm. In the VB.NET example belw, tw checkbxes, tw textbxes, ne prgress bar, and a Third-Party gauge cntrl were added. 5. Once finished, add a buttn that will be used t subscribe t the server s data items. T d s, drag the cntrl frm the tlbx t the frm. Nte: Cde must be added in rder t perfrm the OPC prcesses. 2.1 Adding Cntrls t the Frm 1. In Slutin Explrer, right-click n the Main.vb r Main.cs frm. Then, select View Designer. Nte: The new frm will be named Frm1 by default, but can be renamed if desired. www.kepware.cm 1 ClientAce: Creating a Simple Windws Frm
2. Next, add the cntrls t the frm that will be used t display the OPC data (if they haven t been added already). Then, right-click the Main.vb r Main.cs frm and select View Cde. 2.2 Initializing Glbal Variables Glbal variables can be used by all methds and functins in the prject. The fllwing bjects shuld be added and initialized: A Server Management Object. This establishes the OPC DA cnnectin t the server. A Cnnectin Infrmatin Cllectin Object. This specifies the cnnectin parameters fr the Server Management Object cnnectin. A Blean variable. This tests whether the cnnectin t the server succeeded. A Server Subscriptin Handle. This mdifies r cancels an active data subscriptin. Mre than ne handle will be needed when mre than ne subscriptin is being perfrmed in the same server cnnectin. The handle will be returned t the applicatin frm the server. A Client Subscriptin Handle. This identifies the data change callbacks sent by the server t the applicatin fr each subscriptin. It is specified by the user, and must be unique t each subscriptin. An array f item identifier cllectin bjects. These specify the prperties fr each item that is added t a particular subscriptin. 2.2.1 Examples The VB.NET cde is displayed belw. Public Class Main Dim WithEvents daservermgt As New Kepware.ClientAce.OpcDaClient.DaServerMgt Dim cnnectinf As New Kepware.ClientAce.OpcDaClient.CnnectInf Dim cnnectfailed As Blean Dim activeserversubscriptinhandle As Integer Dim itemidentifiers(4) As Kepware.ClientAce.OpcDaClient.ItemIdentifier Dim clientsubscriptinhandle As Integer The C# cde is displayed belw. public partial class Main : Frm Kepware.ClientAce.OpcDaClient.DaServerMgt DAserver = new Kepware.ClientAce.OpcDaClient.DaServerMgt(); Kepware.ClientAce.OpcDaClient.CnnectInf cnnectinf = new Kepware.ClientAce.OpcDaClient.CnnectInf(); bl cnnectfailed; int activeserversubscriptinhandle; int clientsubscriptinhandle; ItemIdentifier[] itemidentifiers = new ItemIdentifier[5]; www.kepware.cm 2 ClientAce: Creating a Simple Windws Frm
2.3 Adding the Frm Lad Actins In this example, a cnnectin t the OPC Server will be established when the frm lads. 1. In the prject, duble-click n the Windws Frm. The shell cde fr Frm Lad Events will be added autmatically by Visual Studi Intellisense. 2. Next, add the cde needed t initialize the bjects fr cnnectin t the server. URL. This string bject specifies the type f OPC Cnnectin, the lcatin f the server, and the Server ID. OPC Cnnectin ptins include DA, XML-DA, and UA. Example OPC DA URL: The OPC DA URL fr cnnectin is pcda://lcalhst/kepware.kepserverex.v5/b3af0bf6-4c0c- 4804-A122-6F3B160F4397. The Class ID included in the URL is ptinal. Example OPD UA URL: The OPC UA Endpint URL (with security) fr cnnectin is pc.tcp://127.0.0.1:49320. The UA server that is being cnnected must allw cnnectins that have nt been secured; hwever, users can create secure cnnectins thrugh additinal cding. CnnectinInf. This bject specifies the fllwing parameters: LcaleID: This parameter specifies the client applicatin s language, number, and date frmat preferences. KeepAliveTime: This parameter specifies the interval at which the server status will be checked. RetryAfterCnnectinErrr: This parameter specifies whether the server bject will attempt t recnnect t the server if the cnnectin fails after the initial cnnectin. RetryInitialCnnectin: This parameter specifies whether the server bject will retry a cnnectin attempt if the initial attempt fails. ClientName: This parameter allws the client applicatin t prvide additinal identity infrmatin fr the client cnnectin in the frm f a plain language identifier. This parameter is new in ClientAce versin 4.0. ServerHandle. This unique identifier is assigned by the client applicatin fr each server bject with which it initializes and establishes a cnnectin t the server. 3. Once finished, set the Cnnectin Failed variable t False. www.kepware.cm 3 ClientAce: Creating a Simple Windws Frm
Nte: The VB.NET cde is displayed belw. ' Define the server cnnectin URL Dim url As String = "pcda://lcalhst/kepware.kepserverex.v5/b3af0bf6-4c0c-4804-a122-6f3b160f4397" ' Initialize the cnnect inf bject data cnnectinf.lcalid = "en" cnnectinf.keepalivetime = 1000 cnnectinf.retryaftercnnectinerrr = True cnnectinf.retryinitialcnnectin = False cnnectinf.clientname = "Simple VB Frm Example" cnnectfailed = False 'define a client handle fr the cnnectin Dim ServerHandle As Integer = 1 Nte: The C# cde is displayed belw. // Define the server cnnectin URL string url = "pcda://lcalhst/kepware.kepserverex.v5/b3af0bf6-4c0c-4804-a122-6f3b160f4397"; // Initialize the cnnect inf bject data cnnectinf.lcalid = "en"; cnnectinf.keepalivetime = 1000; cnnectinf.retryaftercnnectinerrr = true; cnnectinf.retryinitialcnnectin = false; cnnectinf.clientname = "CS Simple Client"; cnnectfailed = false; ///define a client handle fr the cnnectin /int clienthandle = 1; 4. Next, add the cde needed t manage the OPC DA cnnectin t the server. 5. Then, set a Try Catch Blck t catch and handle any exceptins that are encuntered during the cnnectin attempt. Nte: The VB.NET cde is displayed belw. Call the API cnnect methd: Try 'Try t cnnect daservermgt.cnnect(url, ServerHandle, cnnectinf, cnnectfailed) ' Handle result: If cnnectfailed Then ' Tell user cnnectin attempt failed: MsgBx("Cnnect failed") End If Catch ex As Exceptin MsgBx("Handled Cnnect exceptin. Reasn: " & ex.message) ' Make sure fllwing cde knws cnnectin failed: cnnectfailed = True 'Stp prcessing Exit Sub End Try www.kepware.cm 4 ClientAce: Creating a Simple Windws Frm
Nte: The C# cde is displayed belw. //Try t cnnect with the API cnnect methd: try DAserver.Cnnect (url, clienthandle, ref cnnectinf, ut cnnectfailed); catch (Exceptin ex) MessageBx.Shw ("Handled Cnnect exceptin. Reasn: " + ex.message); // Make sure fllwing cde knws cnnectin failed: cnnectfailed = true; // Handle result: if (cnnectfailed) // Tell user cnnectin attempt failed: MessageBx.Shw ("Cnnect failed"); //Subscribe t events SubscribeTOPCDAServerEvents(); 2.3.1 Advising the Server Management Object fr Callbacks The C# cde has an additinal prcess that it calls t advise the server r bject fr callbacks. The OPC DA specificatin allws users t acquire data thrugh three methds: Synchrnus, Asynchrnus, and Unslicited Asynchrnus. The Asynchrnus methds require that the Server Management Object advise r subscribe fr change events. s created using C# d this separately frm the Server Management Object declaratin. A separate functin is used t subscribe t events. //Subscribe t server events private vid SubscribeTOPCDAServerEvents() //DAserver.ReadCmpleted += new DaServerMgt.ReadCmpletedEventHandler(DAserver_ReadCmpleted); //DAServer.WriteCmpleted += new DaServerMgt.WriteCmpletedEventHandler(DAserver_WriteCmpleted); DAserver.DataChanged += new DaServerMgt.DataChangedEventHandler(DAserver_DataChanged); DAserver.ServerStateChanged += new DaServerMgt.ServerStateChangedEventHandler(DAserver_ServerStateChanged); Nte: s created with VB.NET d this when declaring the bject. The WithEvents keywrd specifies that the named bject instance can raise events. The example belw was dne in the frm declaratins. Dim WithEvents daservermgt As New Kepware.ClientAce.OpcDaClient.DaServerMgt 2.4 Subscribing fr Data Items must be added thrugh a subscriptin in rder t get data in an Unslicited Asynchrnus callback event (r a DataChanged Event) in ClientAce. The examples belw use a buttn t initiate the subscriptin. The buttn makes tw calls. The first call is t the Subscribe2Data functin, where items are subscribed but inactive. The secnd call is t the MdifySubscriptin functin, which sets the subscriptin active. www.kepware.cm 5 ClientAce: Creating a Simple Windws Frm
Nte: The VB.NET cde is displayed belw. Private Sub Buttn1_Click(sender As System.Object, e As System.EventArgs) Handles Buttn1.Click 'Subscribe t the pc data items Subscibe2Data() SubscriptinMdify(True) End Sub Nte: The C# cde is displayed belw. private vid buttn1_click(bject sender, EventArgs e) //Call the methd fr subscribing t data Subscribe2Data(); MdifySubscriptin(true); The Subscribe2Data functin des the fllwing: Initializes the ClientSubscriptin handle variable. This identifies data in the DataChanged Event that is specific t this subscriptin. Each subscriptin shuld have a unique handle. Initializes the Active variable t False. This specifies whether the subscribed items will be actively plled frm the server. Initializes the UpdateRate variable. This specifies the rate at which the subscribed items will be plled. Initializes the Deadband variable. This specifies the amunt f change that is required befre a DataChange Event is sent t the client. T disable Deadband, enter zer. Initializes the RevisedUpdateRate variable. This value is returned frm the server t indicate the update rate that the server will supprt. Fr example, althugh a client might request a rate f 1024 millisecnds, the server might revise it t 1030 millisecnds. Initializes each element f the ItemIdentifiers array as the ClientAce ItemIdentifier and its attributes are initialized. The fllwing fur items will be added t the subscriptin (indexes 0-4): ItemName: This attribute is the fully-qualified OPC Item Name fr the desired item in the server. ClientHandle: This attribute is a unique number that identifies the item in callbacks frm the server. DataType: This attribute specifies the item s data type. Users can add items with r withut specifying the data type. In bth cases, the server will return a RevisedDataType fr each item: this attribute is als part f the ItemIdentifier bject. Once all the variables and array elements have been initialized, users can subscribe t the server fr data using a Try Catch s that any exceptin errrs will be caught and handled. Once the subscriptin is cmplete, the ResultID fr the first item will be checked t see whether it subscribed successfully. If it was, the applicatin will mve n. In a live prductin applicatin, every item that is subscribed shuld be checked fr success. www.kepware.cm 6 ClientAce: Creating a Simple Windws Frm
Nte: The VB.NET cde is displayed belw. Sub Subscibe2Data() 'initialize the client subscriptin handle clientsubscriptinhandle = 1 'Parameter t specify if the subscriptin will be added as active r nt Dim active As Blean = False ' The updaterate parameter is used t tell the server hw fast we ' wuld like t see data updates. Dim updaterate As Integer = 1000 ' The deadband parameter specifies the minimum deviatin needed ' t be cnsidered a change f value. 0 is disabled Dim deadband As Single = 0 ' The revisedupdaterate parameter is the actual update rate that the ' server will be using. Dim revisedupdaterate As Integer 'initialize the itemidentifier values itemidentifiers(0) = New Kepware.ClientAce.OpcDaClient.ItemIdentifier itemidentifiers(0).itemname = "Channel1.Device1.Bl1" itemidentifiers(0).clienthandle = 0 itemidentifiers(0).datatype = Nthing itemidentifiers(1) = New Kepware.ClientAce.OpcDaClient.ItemIdentifier itemidentifiers(1).itemname = "Channel1.Device1.Bl2" itemidentifiers(1).clienthandle = 1 itemidentifiers(1).datatype = Nthing itemidentifiers(2) = New Kepware.ClientAce.OpcDaClient.ItemIdentifier itemidentifiers(2).itemname = "Channel1.Device1.Shrt1" itemidentifiers(2).clienthandle = 2 itemidentifiers(2).datatype = Nthing itemidentifiers(3) = New Kepware.ClientAce.OpcDaClient.ItemIdentifier itemidentifiers(3).itemname = "Channel1.Device1.Shrt2" itemidentifiers(3).clienthandle = 3 itemidentifiers(3).datatype = Nthing itemidentifiers(4) = New Kepware.ClientAce.OpcDaClient.ItemIdentifier itemidentifiers(4).itemname = "Channel1.Device1.Lng5" itemidentifiers(4).clienthandle = 4 itemidentifiers(4).datatype = Nthing 'Call the Subscribe API methd: Try daservermgt.subscribe(clientsubscriptinhandle, active, updaterate, revisedupdaterate, deadband, itemidentifiers, activeserversubscriptinhandle) ' Check item result ID: If itemidentifiers(0).resultid.succeeded = False Then ' Shw a message bx if an item culd nt be added t subscriptin. ' Yu wuld prbably use sme ther means t alert ' the user t failed item enrllments in an actual applicatin. MsgBx("Failed t add item " & itemidentifiers(0).itemname & " t subscriptin") End If Catch ex As Exceptin MsgBx("Handled Subscribe exceptin. Reasn: " & ex.message) End Try End Sub www.kepware.cm 7 ClientAce: Creating a Simple Windws Frm
Nte: The C# cde is displayed belw. private vid Subscribe2Data() // Define parameters fr Subscribe methd: int itemindex; //initialize the client subscriptin handle clientsubscriptinhandle = 1; //Parameter t specify if the subscriptin will be added as active r nt bl active = false; // The updaterate parameter is used t tell the server hw fast we // wuld like t see data updates. int updaterate = 1000; // The deadband parameter specifies the minimum deviatin needed // t be cnsidered a change f value. 0 is disabled Single deadband = 0; // The revisedupdaterate parameter is the actual update rate that the // server will be using. int revisedupdaterate; //Initialize the item identifier values itemidentifiers[0] = new ItemIdentifier(); itemidentifiers[0].clienthandle = 0; itemidentifiers[0].datatype = Type.GetType("System.Blean"); itemidentifiers[0].itemname = "Channel1.Device1.Bl1"; itemidentifiers[1] = new ItemIdentifier(); itemidentifiers[1].clienthandle = 1; itemidentifiers[1].datatype = Type.GetType("System.Blean"); itemidentifiers[1].itemname = "Channel1.Device1.Bl2"; itemidentifiers[2] = new ItemIdentifier(); itemidentifiers[2].clienthandle = 2; itemidentifiers[2].datatype = Type.GetType("System.Int16"); itemidentifiers[2].itemname = "Channel1.Device1.Shrt1"; itemidentifiers[3] = new ItemIdentifier(); itemidentifiers[3].clienthandle = 3; itemidentifiers[3].datatype = Type.GetType("System.Int16"); itemidentifiers[3].itemname = "Channel1.Device1.Shrt2"; itemidentifiers[4] = new ItemIdentifier(); itemidentifiers[4].clienthandle = 4; itemidentifiers[4].datatype = Type.GetType("System.Int32"); itemidentifiers[4].itemname = "Channel1.Device1.Lng5"; try DAserver.Subscribe(clientSubscriptinHandle, active, updaterate, ut revisedupdaterate, deadband, ref itemidentifiers, ut activeserversubscriptinhandle); fr (itemindex = 0; itemindex <= 4; itemindex++) if (itemidentifiers[itemindex].resultid.succeeded == false) MessageBx.Shw("Failed t add item " + itemidentifiers[itemindex].itemname + " t subscriptin"); catch (Exceptin ex) MessageBx.Shw("Handled Subscribe exceptin. Reasn: " + ex.message); www.kepware.cm 8 ClientAce: Creating a Simple Windws Frm
2.5 Setting Items Active fr Plling Items may be set active fr plling after they have been added with a subscriptin mdificatin functin, which is called frm the buttn click after subscribing t the server fr items. The functin is passed, and the Blean value that is used is the SubscriptinMdify Methd f the Server Management Object. This methd uses the ActiveServerSubscriptin Handle (which was returned frm the server in the data subscriptin) and the Actin value that was sent when it was called. In this case, the Actin value is True. Nte: The VB.NET cde is displayed belw. Sub SubscriptinMdify(ByVal actin As Blean) 'Set the subscriptin active r inactive daservermgt.subscriptinmdify(activeserversubscriptinhandle, actin) End Sub Nte: The C# cde is displayed belw. private vid MdifySubscriptin(bl actin) //Mdify the Subscriptin t set it active DAserver.SubscriptinMdify(activeServerSubscriptinHandle, actin); 2.6 Handling DataChanged Events If all the items were successfully subscribed, the server shuld nw be plling the device at the update rate specified in the subscriptin. Any f the subscribed items that change in value r quality will be sent t the client applicatin in a DataChanged Event. In the cde examples belw, the events are received and then prcessed thrugh an event handler n a new thread thrugh the BeginInvke() Methd. Nte: The VB.NET cde is displayed belw. Private Sub daservermgt_datachanged(clientsubscriptin As Integer, allqualitiesgd As Blean, nerrrs As Blean, itemvalues() As Kepware.ClientAce.OpcDaClient.ItemValueCallback) Handles daservermgt.datachanged BeginInvke(New Kepware.ClientAce.OpcDaClient.DaServerMgt.DataChangedEventHandler(AddressOf DataChanged), New Object() clientsubscriptin, allqualitiesgd, nerrrs, itemvalues) End Sub Nte: The C# cde is displayed belw. public vid DAserver_DataChanged(int clientsubscriptin, bl allqualitiesgd, bl nerrrs, ItemValueCallback[] itemvalues) bject[] DCevHndlrArray = new bject[4]; DCevHndlrArray[0] = clientsubscriptin; DCevHndlrArray[1] = allqualitiesgd; DCevHndlrArray[2] = nerrrs; DCevHndlrArray[3] = itemvalues; BeginInvke(new DaServerMgt.DataChangedEventHandler(DataChanged), DCevHndlrArray); www.kepware.cm 9 ClientAce: Creating a Simple Windws Frm
In the invked DataChange handler, each item returned in the callback will be evaluated. The bjects being used t display values n the frm will be updated. The unique client handle will als be checked fr each item in rder t ensure that the prper bject is updated. Nte: The VB.NET cde is displayed belw. Private Sub DataChanged(ByVal clientsubscriptin As Integer, ByVal allqualitiesgd As Blean, ByVal nerrrs As Blean, ByVal itemvalues() As Kepware.ClientAce.OpcDaClient.ItemValueCallback) Dim itemvalue As Kepware.ClientAce.OpcDaClient.ItemValueCallback Fr Each itemvalue In itemvalues 'Update a frm cntrl based n the returned client handle. The values culd be Null if the quality is bad Select Case itemvalue.clienthandle Case 0 If IsNthing(itemValue.Value) Then CheckBx1.Checked = vbnull Else CheckBx1.Checked = itemvalue.value End If Case 1 If IsNthing(itemValue.Value) Then CheckBx2.Checked = vbnull Else CheckBx2.Checked = itemvalue.value End If Case 2 If IsNthing(itemValue.Value) Then TextBx1.Text = "Unknwn" Else TextBx1.Text = itemvalue.value.tstring() End If Case 3 If IsNthing(itemValue.Value) Then TextBx2.Text = "Unknwn" Else TextBx2.Text = itemvalue.value.tstring() End If Case 4 If IsNthing(itemValue.Value) Then PrgressBar1.Value = vbnull GaugeAngular1.Value.AsInteger = vbnull Else PrgressBar1.Value = itemvalue.value GaugeAngular1.Value.AsInteger = itemvalue.value End If End Select Next End Sub www.kepware.cm 10 ClientAce: Creating a Simple Windws Frm
Nte: The C# cde is displayed belw. public vid DataChanged(int clientsubscriptin, bl allqualitiesgd, bl nerrrs, ItemValueCallback[] itemvalues) try Fr each (ItemValueCallback itemvalue in itemvalues) int itemindex = (int)itemvalue.clienthandle; switch (itemindex) case 0: if (itemvalue.value == null) checkbx1.checked = false; else checkbx1.checked = Cnvert.TBlean(itemValue.Value); case 1: if (itemvalue.value == null) checkbx2.checked = false; else checkbx2.checked = Cnvert.TBlean(itemValue.Value); case 2: if (itemvalue.value == null) textbx1.text = "Unknwn"; else textbx1.text = itemvalue.value.tstring(); case 3: if (itemvalue.value == null) textbx2.text = "Unknwn"; else textbx2.text = itemvalue.value.tstring(); case 4: if (itemvalue.value == null) prgressbar1.value = 0; gaugeangular1.value.asinteger = 0; else prgressbar1.value = Cnvert.TInt32(itemValue.Value); gaugeangular1.value.asinteger = Cnvert.TInt32(itemValue.Value); catch (Exceptin ex) MessageBx.Shw("Handled Data Changed exceptin. Reasn: " + ex.message); www.kepware.cm 11 ClientAce: Creating a Simple Windws Frm
2.7 Handling Changes t the Server State This prject als handles changes in the server state. The server state will be checked using the Keep Alive interval specified in the server bject s Cnnect Methd. This event is best prcessed by using a handler in anther thread. Nte: The VB.NET cde is displayed belw. Private Sub daservermgt_serverstatechanged(byval clienthandle As Integer, ByVal state As Kepware.ClientAce.OpcDaClient.ServerState) Handles daservermgt.serverstatechanged BeginInvke(New Kepware.ClientAce.OpcDaClient.DaServerMgt.ServerStateChangedEventHandler(AddressOf ServerStateChanged), New Object() clienthandle, state) End Sub Nte: The C# cde is displayed belw. public vid DAserver_ServerStateChanged(int clienthandle, ServerState state) bject[] SSCevHndlrArray = new bject[2]; SSCevHndlrArray[0] = clienthandle; SSCevHndlrArray[1] = state; BeginInvke(new DaServerMgt.ServerStateChangedEventHandler(ServerStateChanged), SSCevHndlrArray); The server state s significant events are as fllws: Discnnected: This event has an enumerated value f 1. ErrrShutdwn: This event has an enumerated value f 2. It prvides the applicatin time t cleanly discnnect frm the server. ErrrWatchDg: This event has an enumerated value f 3. Cnnected: This event has an enumerated value f 4. The server bject has additinal prperties (such as IsCnnected and ServerState) that are generally updated after the ServerStateChanged Event. As such, they are useful fr status but nt fr cnditin warnings. Nte: The VB.NET cde is displayed belw. Private Sub ServerStateChanged(ByVal clienthandle As Integer, ByVal state As Kepware.ClientAce.OpcDaClient.ServerState) Try '~~ Prcess the callback infrmatin here. Select Case state Case 1 MsgBx("Server is Discnnected") Case 2 'Unsubscribe() 'DiscnnectOPCServer() MsgBx("The server is shutting dwn. The client has autmatically tried t recnnect.") Case 3 ' Retry Cnnectin errr in cnnectin inf is checked s the API will cntinue ' trying t recnnect until the prject is clsed. MsgBx("Server cnnectin has been lst. Client will keep attempting t recnnect.") Case 4 'MsgBx("Cnnected t server.") End Select Catch ex As Exceptin '~~ Handle any exceptin errrs here. End Try End Sub www.kepware.cm 12 ClientAce: Creating a Simple Windws Frm
Nte: The C# cde is displayed belw. public vid ServerStateChanged(int clienthandle, ServerState state) try //~~ Prcess the callback infrmatin here. switch (state) case ServerState.ERRORSHUTDOWN: //Unsubscribe(); //DiscnnectOPCServer(); MessageBx.Shw("The server is shutting dwn. The client has autmatically discnnected."); case ServerState.ERRORWATCHDOG: MessageBx.Shw("Server cnnectin has been lst. Client will keep attempting t recnnect."); case ServerState.CONNECTED: MessageBx.Shw("ServerStateChanged, cnnected"); case ServerState.DISCONNECTED: MessageBx.Shw("ServerStateChanged, discnnected"); default: MessageBx.Shw("ServerStateChanged, undefined state fund."); catch (Exceptin ex) MessageBx.Shw("Handled Server State Changed exceptin. Reasn: " + ex.message); 3. Testing the Prject 1. T test the prject, run it in Debug Mde. T d s, press the F5 key while in the Visual Studi Develpment Envirnment. The example belw displays VB.NET. 2. If the server is running, data will be displayed in the frm. 4. Summary At this time, users shuld have a better understanding f hw t create a simple ClientAce Windws Frm applicatin. www.kepware.cm 13 ClientAce: Creating a Simple Windws Frm