Gurux provides you with a sample code to show, how to use the component.
For further information, contact our product support.
[VB.Net example]
Option Explicit On
Friend Class Form1
Inherits System.Windows.Forms.Form
''' <summary>
''' Closes SNMP connection.
''' </summary>
''' <param name="eventSender"></param>
''' <param name="eventArgs"></param>
Private Sub CloseBtn_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CloseBtn.Click
Try
GXSNMP1.Close()
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
''' <summary>
''' Disables buttons on load.
''' </summary>
''' <param name="eventSender"></param>
''' <param name="eventArgs"></param>
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Try
If GXSNMP1.IsOpen Then
OnMediaStateChange(GuruxSNMP.GX_MEDIA_STATE_CHANGE.GX_MEDIA_STATE_CHANGE_OPEN)
Else
OnMediaStateChange(GuruxSNMP.GX_MEDIA_STATE_CHANGE.GX_MEDIA_STATE_CHANGE_CLOSE)
End If
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
''' <summary>
''' Closes media, when the window is closed.
''' </summary>
''' <param name="eventSender"></param>
''' <param name="eventArgs"></param>
Private Sub Form1_FormClosing(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Try
GXSNMP1.Close()
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
''' <summary>
''' Closes media.
''' </summary>
''' <param name="eventSender"></param>
''' <param name="eventArgs"></param>
Private Sub Form1_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Try
GXSNMP1.Close()
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
''' <summary>
''' Shows occurred errors.
''' </summary>
''' <param name="ErrorInfo"></param>
Private Sub OnError(ByVal ErrorInfo As String)
Try
MessageBox.Show(ErrorInfo)
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
''' <summary>
''' To improve the performance of communication, the GXSNMP component can send
''' an error notification, directly through its own thread. Windows, however,
''' does not allow all UI components to be used from another thread. As the
''' updated notifications cannot be shown automatically, the application
''' itself has to show them. This is done by using BeginInvoke function,
''' as shown below.
''' </summary>
''' <param name="eventSender"></param>
''' <param name="eventArgs"></param>
Private Sub GXSNMP1_OnError(ByVal eventSender As System.Object, ByVal eventArgs As AxGuruxSNMP.IGXSNMPEvents_OnErrorEvent) Handles GXSNMP1.OnError
Try
Dim args() As Object = {eventArgs.errorInfo}
Me.BeginInvoke(New GuruxSNMP.IGXSNMPEvents_OnErrorEventHandler(AddressOf OnError), args)
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
''' <summary>
''' Media state has changed.
''' </summary>
''' <param name="State"></param>
Private Sub OnMediaStateChange(ByVal State As GuruxSNMP.GX_MEDIA_STATE_CHANGE)
Try
Dim bOpen As Boolean
bOpen = State = GuruxSNMP.GX_MEDIA_STATE_CHANGE.GX_MEDIA_STATE_CHANGE_OPEN
OpenBtn.Enabled = Not bOpen
SendText.Enabled = bOpen
SendBtn.Enabled = bOpen
CloseBtn.Enabled = bOpen
ReceivedText.Enabled = bOpen
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
''' <summary>
''' To improve the performance of communication, the GXSNMP component can send a notification
''' of a changed media state, directly through its own thread. Windows, however, does not allow
''' all UI components to be used from another thread. As the updated notifications cannot be
''' shown automatically, the application itself has to show them. This is done by using
''' BeginInvoke function, as shown below.
''' </summary>
''' <param name="eventSender"></param>
''' <param name="eventArgs"></param>
Private Sub GXSNMP1_OnMediaStateChange(ByVal eventSender As System.Object, ByVal eventArgs As AxGuruxSNMP.IGXSNMPEvents_OnMediaStateChangeEvent) Handles GXSNMP1.OnMediaStateChange
Try
Dim args() As Object = {eventArgs.state}
Me.BeginInvoke(New GuruxSNMP.IGXSNMPEvents_OnMediaStateChangeEventHandler(AddressOf OnMediaStateChange), args)
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
''' <summary>
''' New data from GXSNMP is received.
''' </summary>
''' <param name="data">Received data.</param>
''' <param name="senderInfo">Sender of the data. In SNMP this is empty.</param>
Private Sub OnReceived(ByRef data As Object, ByVal senderInfo As String)
Try
Dim Value As Byte()
Value = data
'Byte array received from GXSNMP, and must be changed to chars.
If (HexCB.Checked) Then
ReceivedText.Text += BitConverter.ToString(Value)
Else
'Gets received data as string.
ReceivedText.Text += System.Text.Encoding.ASCII.GetString(Value)
End If
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
''' <summary>
''' To improve the performance of communication, the GXSNMP component can send a notification
''' of data received, directly through its own thread. Windows, however, does not allow all UI
''' components to be used from another thread. As the updated notifications cannot be shown
''' automatically, the application itself has to show them. This is done by using
''' BeginInvoke function, as shown below.
''' </summary>
''' <param name="eventSender"></param>
''' <param name="eventArgs"></param>
Private Sub GXSNMP1_OnReceived(ByVal eventSender As System.Object, ByVal eventArgs As AxGuruxSNMP.IGXSNMPEvents_OnReceivedEvent) Handles GXSNMP1.OnReceived
Try
Dim args() As Object = {eventArgs.data, eventArgs.senderInfo}
Me.BeginInvoke(New GuruxSNMP.IGXSNMPEvents_OnReceivedEventHandler(AddressOf OnReceived), args)
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
''' <summary>
''' Opens SNMP connection.
''' </summary>
''' <param name="eventSender"></param>
''' <param name="eventArgs"></param>
Private Sub OpenBtn_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles OpenBtn.Click
Try
GXSNMP1.Open()
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
''' <summary>
''' Shows GXSNMP media properties.
''' </summary>
''' <param name="eventSender"></param>
''' <param name="eventArgs"></param>
Private Sub PropertiesBtn_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles PropertiesBtn.Click
Try
GXSNMP1.Properties(Me.Handle.ToInt32)
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
''' <summary>
''' Sends data to the SNMP.
''' </summary>
''' <param name="eventSender"></param>
''' <param name="eventArgs"></param>
Private Sub SendBtn_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles SendBtn.Click
ReceivedText.Text = String.Empty
Try
Dim Data As String
Data = Nothing
If SyncBtn.CheckState = 1 Then 'Sends data synchronously.
If HexCB.CheckState = 1 Then
'Sends data as byte array.
GXSNMP1.SendSync(SendText.Text, GuruxSNMP.GX_VARTYPE.GX_VT_HEX_STR, EOPText.Text, MinSizeTB.Text, WaitTimeTB.Text, True, GuruxSNMP.GX_VARTYPE.GX_VT_HEX_STR, Data)
ReceivedText.Text = Data
Else
'Send data as ASCII string.
GXSNMP1.SendSync(SendText.Text, GuruxSNMP.GX_VARTYPE.GX_VT_NONE, EOPText.Text, MinSizeTB.Text, WaitTimeTB.Text, True, GuruxSNMP.GX_VARTYPE.GX_VT_STR, Data)
ReceivedText.Text = Data
End If
Else 'Sends data asynchronously.
If HexCB.CheckState = 1 Then
'Sends data as byte array.
GXSNMP1.Send(SendText.Text, GuruxSNMP.GX_VARTYPE.GX_VT_HEX_STR)
Else
'Sends data as ASCII string
GXSNMP1.Send(SendText.Text, GuruxSNMP.GX_VARTYPE.GX_VT_STR)
End If
End If
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
''' <summary>
''' End of Packet is used only when data is send synchronously.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub SyncBtn_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SyncBtn.CheckedChanged
Try
'If True, no more data is expected to be received synchronously. If False, asynchronous data is not received.
GXSNMP1.SendSyncComplete = True
EOPText.Enabled = SyncBtn.Checked
WaitTimeTB.Enabled = SyncBtn.Checked
MinSizeTB.Enabled = SyncBtn.Checked
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
End Class
[C# example]
using System;
using System.Windows.Forms;
namespace GXSNMPSample
{
internal partial class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Closes SNMP connection.
/// </summary>
/// <param name="eventSender"></param>
/// <param name="eventArgs"></param>
private void CloseBtn_Click(System.Object eventSender, System.EventArgs eventArgs)
{
try
{
GXSNMP1.Close();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
/// <summary>
/// Disables buttons.
/// </summary>
/// <param name="eventSender"></param>
/// <param name="eventArgs"></param>
private void Form1_Load(System.Object eventSender, System.EventArgs eventArgs)
{
try
{
if (GXSNMP1.IsOpen)
{
OnMediaStateChange(GuruxSNMP.GX_MEDIA_STATE_CHANGE.GX_MEDIA_STATE_CHANGE_OPEN);
}
else
{
OnMediaStateChange(GuruxSNMP.GX_MEDIA_STATE_CHANGE.GX_MEDIA_STATE_CHANGE_CLOSE);
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
/// <summary>
/// Closes media, when the window is closed.
/// </summary>
/// <param name="eventSender"></param>
/// <param name="eventArgs"></param>
private void Form1_FormClosed(System.Object eventSender, System.Windows.Forms.FormClosedEventArgs eventArgs)
{
try
{
GXSNMP1.Close();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
/// <summary>
/// To improve the performance of communication, the GXSNMP component can
/// send an error notification directly through its own thread. Windows, however,
/// does not allow all UI components to be used from another thread.
/// As the updated notifications cannot be shown automatically, the application
/// itself has to show them. This is done by using BeginInvoke function, as shown below.
/// </summary>
/// <param name="ErrorInfo"></param>
private void OnError(string ErrorInfo)
{
try
{
MessageBox.Show(ErrorInfo);
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
/// <summary>
/// To improve the performance of communication, the GXSNMP component can
/// send an error notification directly through its own thread.
/// Windows, however, does not allow all UI components to be used from another thread.
/// As the updated notifications cannot be shown automatically, the application itself
/// has to show them. This is done by using BeginInvoke function, as shown below.
/// </summary>
/// <param name="eventSender"></param>
/// <param name="eventArgs"></param>
private void GXSNMP1_OnError(System.Object eventSender, AxGuruxSNMP.IGXSNMPEvents_OnErrorEvent eventArgs)
{
try
{
this.BeginInvoke(new GuruxSNMP.IGXSNMPEvents_OnErrorEventHandler(OnError), new object[] { eventArgs.errorInfo });
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
/// <summary>
/// To improve the performance of communication, the GXSNMP component can
/// send a notification of a changed media state, directly through its own thread.
/// Windows, however, does not allow all UI components to be used from another thread.
/// As the updated notifications cannot be shown automatically, the application itself
/// has to show them. This is done by using BeginInvoke function, as shown below.
/// </summary>
/// <param name="State"></param>
private void OnMediaStateChange(GuruxSNMP.GX_MEDIA_STATE_CHANGE State)
{
try
{
bool bOpen;
bOpen = State == GuruxSNMP.GX_MEDIA_STATE_CHANGE.GX_MEDIA_STATE_CHANGE_OPEN;
OpenBtn.Enabled = !bOpen;
SendText.Enabled = bOpen;
SendBtn.Enabled = bOpen;
CloseBtn.Enabled = bOpen;
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
/// <summary>
/// To improve the performance of communication, the GXSNMP component can
/// send a notification of a changed media state, directly through its own thread.
/// Windows, however, does not allow all UI components to be used from another thread.
/// As the updated notifications cannot be shown automatically, the application itself
/// has to show them. This is done by using BeginInvoke function, as shown below.
/// </summary>
/// <param name="eventSender"></param>
/// <param name="eventArgs"></param>
private void GXSNMP1_OnMediaStateChange(System.Object eventSender, AxGuruxSNMP.IGXSNMPEvents_OnMediaStateChangeEvent eventArgs)
{
try
{
this.BeginInvoke(new GuruxSNMP.IGXSNMPEvents_OnMediaStateChangeEventHandler(OnMediaStateChange), new object[] { eventArgs.state });
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
/// <summary>
/// To improve the performance of communication, the GXSNMP component can
/// send a notification of data received, directly through its own thread.
/// Windows, however, does not allow all UI components to be used from another thread.
/// As the updated notifications cannot be shown automatically, the application itself
/// has to show them. This is done by using BeginInvoke function, as shown below.
/// </summary>
/// <param name="data"></param>
/// <param name="senderInfo"></param>
private void OnReceived(ref object data, string senderInfo)
{
try
{
// Byte array received from GXSNMP, and must be changed to chars.
if (HexCB.Checked)
{
ReceivedText.Text += BitConverter.ToString((byte[]) data);
}
else
{
// Gets received data as string.
ReceivedText.Text += System.Text.Encoding.ASCII.GetString((byte[]) data);
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
/// <summary>
/// Asks UI to show received data.
/// </summary>
/// <param name="eventSender"></param>
/// <param name="eventArgs"></param>
private void GXSNMP1_OnReceived(System.Object eventSender, AxGuruxSNMP.IGXSNMPEvents_OnReceivedEvent eventArgs)
{
this.BeginInvoke(new GuruxSNMP.IGXSNMPEvents_OnReceivedEventHandler(OnReceived), new object[] { eventArgs.data, eventArgs.senderInfo });
}
/// <summary>
/// Opens SNMP connection.
/// </summary>
/// <param name="eventSender"></param>
/// <param name="eventArgs"></param>
private void OpenBtn_Click(System.Object eventSender, System.EventArgs eventArgs)
{
try
{
GXSNMP1.Open();
}
catch(Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
/// <summary>
/// Shows GXSNMP media properties.
/// </summary>
/// <param name="eventSender"></param>
/// <param name="eventArgs"></param>
private void PropertiesBtn_Click(System.Object eventSender, System.EventArgs eventArgs)
{
try
{
GXSNMP1.Properties(this.Handle.ToInt32());
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
/// <summary>
/// Sends data to the SNMP.
/// </summary>
/// <param name="eventSender"></param>
/// <param name="eventArgs"></param>
private void SendBtn_Click(System.Object eventSender, System.EventArgs eventArgs)
{
try
{
ReceivedText.Text = string.Empty;
object Data;
{
if (HexCB.Checked)
{
// Sends data as byte array.
GXSNMP1.SendSync(SendText.Text, GuruxSNMP.GX_VARTYPE.GX_VT_HEX_STR, EOPText.Text, Convert.ToInt32(MinSizeTB.Text), Convert.ToInt32(WaitTimeTB.Text), true, GuruxSNMP.GX_VARTYPE.GX_VT_HEX_STR, out Data);
ReceivedText.Text = Convert.ToString(Data);
}
else
{
// Sends data as ASCII string.
GXSNMP1.SendSync(SendText.Text, GuruxSNMP.GX_VARTYPE.GX_VT_NONE, EOPText.Text, Convert.ToInt32(MinSizeTB.Text), Convert.ToInt32(WaitTimeTB.Text), true, GuruxSNMP.GX_VARTYPE.GX_VT_STR, out Data);
ReceivedText.Text = Convert.ToString(Data);
}
}
else // Sends data asynchronously.
{
if (HexCB.Checked)
{
// Sends data as byte array.
GXSNMP1.Send(SendText.Text, GuruxSNMP.GX_VARTYPE.GX_VT_HEX_STR);
}
else
{
// Sends data as ASCII string.
GXSNMP1.Send(SendText.Text, GuruxSNMP.GX_VARTYPE.GX_VT_STR);
}
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
/// <summary>
/// End of Packet used only, when data is sent synchronously.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SyncBtn_CheckedChanged(object sender, EventArgs e)
{
try
{
// If True, no more data is expected to be received synchronously.
// If False, asynchronous data is not received.
GXSNMP1.SendSyncComplete = true;
MinSizeTB.Enabled = WaitTimeTB.Enabled = EOPText.Enabled = SyncBtn.Checked;
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
}
}