GXDN: Gurux Developer Network
GuruxClient component
In this walkthrough, you learn to use Gurux GXCom components (GXCom) in the Visual Studio .NET environment, using the C# language.

Adding GXCom support to the project

  1. Start Visual Studio .NET.
  2. Open the project to which you want to add the GXCom support, or create a new one.
  3. Open Toolbox.
  4. Select Customize Toolbox.

In the opened dialog, select COM Components tab. Select GXClient, GXPacket and GXNet components, and click OK to finish.


Using components with .NET

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,
select Add Reference option in Project menu.
The dialog shows available components, of which
you choose the components to use.

In this case select GXClient, GXPacket and GXNet Components,
and click OK to finish.

The following example shows, how to create GXCom components.


using GuruxClient;
using GuruxPacket;
using GuruxNet;
//Create new GXClient
GuruxClient.GXClient axGXClient1 = new GXClientClass();
//Create new GXPacket
GuruxPacket.GXPacket axGXPacket1 = new GXPacketClass();
//Create new Media component
GuruxNet.GXNet axGXNet1 = new GXNetClass();
Selecting media

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

After the media is selected, set the Protocol properties.
You can either select appropriate settings in Properties dialog, or set the settings programmatically, as follows:
axGXClient1.HeaderSize = 0;
axGXClient1.DataSize = 0;
//Set BOP and EOP.
axGXClient1.SetBOP("01", GuruxCommon.GX_VARTYPE.GX_VT_BYTE);
axGXClient1.SetEOP("01", GuruxCommon.GX_VARTYPE.GX_VT_BYTE);
axGXClient1.MediaOpen();
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; create packet and send it. If it is sent successfully, the sent packet is filled with the content of the received packet.
//Send packet
string receiver = "";
axGXClient1.Send(axGXPacket1, true, ref receiver);
if (axGXPacket1.Status == GuruxCommon.GX_STATUS.GX_ST_OK)
{
    //Handle received packet
}
else
{
    //Handle occured error
}
Sending data asynchronously

To receive events sent by the GXClient1 component, first, select the component on the form, and then, select Properties. Create handlers for OnError and Received methods.

To create a handler for a specific event, first, select the event, and then, give a name for the handler.

The following example converts received data to string and stores it.
private void axGXClient1_Received(object sender, AxGuruxClient._IGXClientEvents_OnReceivedEvent e)
{
    //Update data to the edit box 
    Byte[] bytes = (Byte[]) e.data; 
    ReceiveDataText.Text += System.Text.Encoding.ASCII.GetString(bytes); 
    //Update Sender Information to the edit box.
    SenderText.Text = e.senderInfo.ToString();
}

private void axGXClient1_OnError(object sender, AxGuruxClient._IGXClientEvents_OnErrorEvent e)
{
    MessageBox.Show(sender.ToString());
}
Error Handling

It is 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 component returns an error.

The following example shows how to handle errors in .NET:
try
{
    axGXClient1.Send(axGXPacket1, true, ref receiver);    
}
catch(Exception ex)
{
    MessageBox.Show(ex.ToString());
}

Using the components from development environment


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