GXDN: Gurux Developer Network
GuruxPing component
This guide shows you how to use Gurux Network component (GXPing) in the Visual Studio .NET environment using the C# language. Source code is available at sourceforge.net
Adding GXPing component to the project
  1. Start Visual Studio .NET
  2. Open the project where you want to add the GXPing component, or create a new one
  3. Open Toolbox




  4. Note: If the Gurux tab is not shown in the Toolbox,
    you can implement it manually

  5. Under Gurux tab, select the component, and drag and drop it on the form
  6. Allow the use of any version of the media component:
    • Select the component.
    • In Solution Explorer view, in the References folder, select Gurux.GuruxPing.
    • In Properties view, set Specific Version to False.



Using components with .NET
You can either drag the component to the form, or create it dynamically.

Creating component dynamically
To create component in runtime, select Add Reference in the Project menu. The dialog shows available components, select the one(s) to use (in this case GXPing component). Click OK to finish. The following example shows how to create a GXPing component.
[C# example]
using Gurux.GuruxPing;
//Create new component
GXPing GXPing1 = new GXPingClass();
		
Setting Media Properties
You can either select appropriate settings in the Properties dialog or edit the settings programmatically.
[C# example]
  //Initialize Ping settings.
  GXPing1.HostName = "www.gurux.fi";
  GXPing1.Timeout = 250;
  GXPing1.Count = 5;
  GXPing1.Ping()
Sending and receiving packets
All data is sent asynchronously. Response is received as a Received event message.
Receiving event messages
To receive event sent by the GXPing component, select the component by right-clicking it on the form and select Properties. Create handlers for OnError and OnReceived methods.
To create a handler, select an event and enter name for the handler. In the following example, received data is converted to string and stored.
[C# example]
private void gxPing1_OnPing(object sender, Gurux.GuruxPing.GX_PING_STATE State, int SentCount, int LostCount, int LastTime, int AverageTime, int MinimumTime, int MaximumTime)
{
              //Update statistics.
              SendCount.Text += SentCount.ToString();
}

private void gxPing1_OnError(object sender, string ErrorInfo)
{
    MessageBox.Show(sender.ToString());
}
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, GXPing component returns error. The following example shows how to handle errors in .NET.
[C# example]
try
{
	object data = Convert.ChangeType("Hello", System.TypeCode.Object);
	string receiver = null;
	GXPing1.Send(ref data, 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++