GXDN: Gurux Developer Network
GuruxClient component
In this walkthrough, you learn to use Gurux GXCom components (GXCom) in the Visual Basic environment.

Adding GXCom support to the project

  1. Start Visual Basic
  2. Open project to which you want to add GXCom support, or create a new one.
  3. Select Components options in Project menu.
  4. Select GuruxClient, Gurux Packet component, wanted media, and click OK to finish.

     
Using components with Visual Basic

To use the GXCom components, you can either drag the component to the form, or create the component dynamically.

Creating component dynamically

To create component in run time:
  1. In Project menu, select References option.
  2. Choose the components to use, in the dialog that shows all available components.
    In this case select Gurux GXClient, Gurux Packet, and used media components.
  3. Click OK to finish.

The following example shows, how to create a Gurux Client, and Gurux Packet components:


Dim WithEvents GXClient1 As GXClient
'Create new component
Set GXClient1 = New GXClient
Dim GXPacket1 as GXPacket
'Create new GXPacket
Set GXPacket1 = New GXPacket
WithEvents is important, it allows that component can receive asynchronous calls.

Selecting media

First, select media, and then, set media settings.
'Select new Media
GXClient1.SelectMedia("Net")
Set GXNet1 = GXClient1.GetCurrentMedia
'Set server port and TCP/IP address
GXNet1.HostName = "Localhost"
GXNet1.HostPort = 1234
'Assign new Media
GXClient1.AssignMedia GXNet1
Changing properties

Next, set Protocol properties. You can either select appropriate settings in Properties dialog, or set the settings programmatically, as follows:
'Set Protocol settings.
GXClient1.HeaderSize = 0
GXClient1.DataSize = 0;
GXClient1.SetBOP "01", GX_VT_BYTE
GXClient1.SetEOP "02", GX_VT_BYTE
'or
GXClient1.Properties
Sending and receiving packets


Data can be sent synchronously or asynchronously. If data is sent synchronously, the sent packet is filled with the content of the received packet. If data is sent asynchronously, the response is received through GXClientEvents.Received event message.

Sending data synchronously

Sending data synchronously is easy, just create a packet, and send it. If it is sent successfully, it is filled with the content of the received packet.
'Send packet as synchronous
GXClient1.Send GXPacket1, True, ""
'Receiving event messages
if GXPacket1.Status = GX_ST_OK then
'Handle received packet
else
'Handle error
end if
Sending data asynchronously

Because GXClient1 variable was introduced as WithEvents type, the event messages coming from the system, can be received easily.

A handler for the Received method, is created, as follows:




Above the code window, there are two menus. In the left one, select GXClient1. In the right menu, select the event message, for which to make the handler.
In this example, select Received. Code window then shows the following:
Private Sub SendAsyncData()
    GXClient1.Send GXPacket1, False, ""
end sub

Private Sub GXClient1_Received(ByVal Pack As IGXPacket, ByVal Answer As Boolean)
    'Add code here
End Sub
Note:
If device sends notifications, they come through GXClient1_Received method.

Now create handlers for other events.

Error Handling

It's very important to consider error handling when sending and receiving data. The connection can drop down, or something else unexpected may happen. If something unwanted happens, Gurux Client returns an error. The following example shows how to handle errors in Visual Basic.
Sub SendPacket() On Error
    GoTo GXErrHandler 
    'Send Data
    GXClient1.Send GXClient1, True, ""
    Exit Sub
GXErrHandler:
    'Show occured error
    MsgBox Err.Description
End Sub


Using the components from development environment


Using from .NET | Using from Visual Basic | Using from C++ | Parameter Types