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 Terminal 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
GXTerminal1.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 GXTerminal1.IsOpen Then
OnMediaStateChange(GuruxTerminal.GX_MEDIA_STATE_CHANGE.GX_MEDIA_STATE_CHANGE_OPEN)
Else
OnMediaStateChange(GuruxTerminal.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
GXTerminal1.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
GXTerminal1.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 GXTerminal 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 GXTerminal1_OnError(ByVal eventSender As System.Object, ByVal eventArgs As AxGuruxTerminal.IGXTerminalEvents_OnErrorEvent) Handles GXTerminal1.OnError
Try
Dim args() As Object = {eventArgs.errorInfo}
Me.BeginInvoke(New GuruxTerminal.IGXTerminalEvents_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 GuruxTerminal.GX_MEDIA_STATE_CHANGE)
Try
Dim bOpen As Boolean
bOpen = State = GuruxTerminal.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 GXTerminal 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 GXTerminal1_OnMediaStateChange(ByVal eventSender As System.Object, ByVal eventArgs As AxGuruxTerminal.IGXTerminalEvents_OnMediaStateChangeEvent) Handles GXTerminal1.OnMediaStateChange
Try
Dim args() As Object = {eventArgs.state}
Me.BeginInvoke(New GuruxTerminal.IGXTerminalEvents_OnMediaStateChangeEventHandler(AddressOf OnMediaStateChange), args)
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
''' <summary>
''' New data from GXTerminal is received.
''' </summary>
''' <param name="data">Received data.</param>
''' <param name="senderInfo">Sender of the data. In Terminal 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 GXTerminal, 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 GXTerminal 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 GXTerminal1_OnReceived(ByVal eventSender As System.Object, ByVal eventArgs As AxGuruxTerminal.IGXTerminalEvents_OnReceivedEvent) Handles GXTerminal1.OnReceived
Try
Dim args() As Object = {eventArgs.data, eventArgs.senderInfo}
Me.BeginInvoke(New GuruxTerminal.IGXTerminalEvents_OnReceivedEventHandler(AddressOf OnReceived), args)
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
''' <summary>
''' Opens Terminal 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
GXTerminal1.Open()
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
''' <summary>
''' Shows GXTerminal 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
GXTerminal1.Properties(Me.Handle.ToInt32)
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
''' <summary>
''' Sends data to the Terminal.
''' </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.
GXTerminal1.SendSync(SendText.Text, GuruxTerminal.GX_VARTYPE.GX_VT_HEX_STR, EOPText.Text, MinSizeTB.Text, WaitTimeTB.Text, True, GuruxTerminal.GX_VARTYPE.GX_VT_HEX_STR, Data)
ReceivedText.Text = Data
Else
'Send data as ASCII string.
GXTerminal1.SendSync(SendText.Text, GuruxTerminal.GX_VARTYPE.GX_VT_NONE, EOPText.Text, MinSizeTB.Text, WaitTimeTB.Text, True, GuruxTerminal.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.
GXTerminal1.Send(SendText.Text, GuruxTerminal.GX_VARTYPE.GX_VT_HEX_STR)
Else
'Sends data as ASCII string
GXTerminal1.Send(SendText.Text, GuruxTerminal.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.
GXTerminal1.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 GXTerminalSample
{
internal partial class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Closes Terminal connection.
/// </summary>
/// <param name="eventSender"></param>
/// <param name="eventArgs"></param>
private void CloseBtn_Click(System.Object eventSender, System.EventArgs eventArgs)
{
try
{
GXTerminal1.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 (GXTerminal1.IsOpen)
{
OnMediaStateChange(GuruxTerminal.GX_MEDIA_STATE_CHANGE.GX_MEDIA_STATE_CHANGE_OPEN);
}
else
{
OnMediaStateChange(GuruxTerminal.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
{
GXTerminal1.Close();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
/// <summary>
/// To improve the performance of communication, the GXTerminal 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 GXTerminal 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 GXTerminal1_OnError(System.Object eventSender, AxGuruxTerminal.IGXTerminalEvents_OnErrorEvent eventArgs)
{
try
{
this.BeginInvoke(new GuruxTerminal.IGXTerminalEvents_OnErrorEventHandler(OnError), new object[] { eventArgs.errorInfo });
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
/// <summary>
/// To improve the performance of communication, the GXTerminal 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(GuruxTerminal.GX_MEDIA_STATE_CHANGE State)
{
try
{
bool bOpen;
bOpen = State == GuruxTerminal.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 GXTerminal 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 GXTerminal1_OnMediaStateChange(System.Object eventSender, AxGuruxTerminal.IGXTerminalEvents_OnMediaStateChangeEvent eventArgs)
{
try
{
this.BeginInvoke(new GuruxTerminal.IGXTerminalEvents_OnMediaStateChangeEventHandler(OnMediaStateChange), new object[] { eventArgs.state });
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
/// <summary>
/// To improve the performance of communication, the GXTerminal 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 GXTerminal, 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 GXTerminal1_OnReceived(System.Object eventSender, AxGuruxTerminal.IGXTerminalEvents_OnReceivedEvent eventArgs)
{
this.BeginInvoke(new GuruxTerminal.IGXTerminalEvents_OnReceivedEventHandler(OnReceived), new object[] { eventArgs.data, eventArgs.senderInfo });
}
/// <summary>
/// Opens Terminal connection.
/// </summary>
/// <param name="eventSender"></param>
/// <param name="eventArgs"></param>
private void OpenBtn_Click(System.Object eventSender, System.EventArgs eventArgs)
{
try
{
GXTerminal1.Open();
}
catch(Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
/// <summary>
/// Shows GXTerminal media properties.
/// </summary>
/// <param name="eventSender"></param>
/// <param name="eventArgs"></param>
private void PropertiesBtn_Click(System.Object eventSender, System.EventArgs eventArgs)
{
try
{
GXTerminal1.Properties(this.Handle.ToInt32());
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
/// <summary>
/// Sends data to the Terminal.
/// </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.
GXTerminal1.SendSync(SendText.Text, GuruxTerminal.GX_VARTYPE.GX_VT_HEX_STR, EOPText.Text, Convert.ToInt32(MinSizeTB.Text), Convert.ToInt32(WaitTimeTB.Text), true, GuruxTerminal.GX_VARTYPE.GX_VT_HEX_STR, out Data);
ReceivedText.Text = Convert.ToString(Data);
}
else
{
// Sends data as ASCII string.
GXTerminal1.SendSync(SendText.Text, GuruxTerminal.GX_VARTYPE.GX_VT_NONE, EOPText.Text, Convert.ToInt32(MinSizeTB.Text), Convert.ToInt32(WaitTimeTB.Text), true, GuruxTerminal.GX_VARTYPE.GX_VT_STR, out Data);
ReceivedText.Text = Convert.ToString(Data);
}
}
else // Sends data asynchronously.
{
if (HexCB.Checked)
{
// Sends data as byte array.
GXTerminal1.Send(SendText.Text, GuruxTerminal.GX_VARTYPE.GX_VT_HEX_STR);
}
else
{
// Sends data as ASCII string.
GXTerminal1.Send(SendText.Text, GuruxTerminal.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.
GXTerminal1.SendSyncComplete = true;
MinSizeTB.Enabled = WaitTimeTB.Enabled = EOPText.Enabled = SyncBtn.Checked;
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
}
}