/*********************************************************************
*
* Afag Telnet C# Integration Sample Header
*
*********************************************************************
* FileName: TCPCommunicationClass.cs
* Dependencies: aflex_CSharp_Integration_Sample
* Company: ima-tec GmbH
*
* Software License Agreement
*
*
* THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
* WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
* LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* AFAG BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
* PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
* BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
* THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
* SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
* (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
*
*
********************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
//using TouchSocket.Core;
//using TouchSocket.Sockets;
namespace aflex_CSharp_Integration_Sample
{
class TCPCommunicationClass
{
public TcpClient objTCPClient = null;
private byte[] byteReceiveBuffer = new byte[256];
private bool boolTXEnable = false;
public delegate void ReadResultDelegate(string Result);
//Events
public event ReadResultDelegate ResultReceived; //event of valid recieve package
public event EventHandler ConnectionAbort;
public TCPCommunicationClass()
{
}
public bool Connect(IPAddress IPAddress, int Port) { //connect to the Telnet server
bool Successful = false;
if (this.objTCPClient != null) {
this.objTCPClient.Close(); //if there is already a connection, close it
}
this.objTCPClient = new TcpClient();
try {
this.objTCPClient.Connect(IPAddress, Port);
this.objTCPClient.Client.BeginReceive(byteReceiveBuffer, 0, 256, SocketFlags.None, new AsyncCallback(asyncResultRead), null);
Successful = true;
this.boolTXEnable = true;
} catch {
}
return Successful;
}
public void Disconnect() { //disconnect from server
this.objTCPClient.Close();
}
private void asyncResultRead(IAsyncResult Result) { // here we read out an asynchronously received package
try {
if (Result.IsCompleted) {
this.objTCPClient.Client.EndReceive(Result);
string returnString = "";
int y = 0;
for (int i = 0; i < byteReceiveBuffer.Length - 2; i++) {
if (i < 250 && Convert.ToChar(byteReceiveBuffer[i]) == 'H' && Convert.ToChar(byteReceiveBuffer[i + 1]) == 'B' && Convert.ToChar(byteReceiveBuffer[i + 2]) == 'T') {
returnString = String.Empty;
for (int x = i; x < i + 5; x++) {
if (byteReceiveBuffer[x] != 0) {
returnString += (Convert.ToChar(byteReceiveBuffer[x]).ToString());
y = x;
byteReceiveBuffer[x] = 0;
}
}
if (this.ResultReceived != null) {
this.ResultReceived(returnString);
}
}
if (byteReceiveBuffer[i] == 13 && byteReceiveBuffer[i + 1] == 10) {
returnString = String.Empty;
for (int x = y; x < i; x++) {
if (byteReceiveBuffer[x] != 0) {
returnString += (Convert.ToChar(byteReceiveBuffer[x]).ToString());
y = x;
byteReceiveBuffer[x] = 0;
}
}
if (this.ResultReceived != null) {
this.ResultReceived(returnString);
}
}
}
//reset buffer
byteReceiveBuffer = new byte[256];
this.objTCPClient.Client.BeginReceive(byteReceiveBuffer, 0, 256, SocketFlags.None, new AsyncCallback(asyncResultRead), null);
}
} catch {
this.objTCPClient.Close();
}
}
///
/// Send a command to the Host
///
/// the command to transmit
/// transmit sucessfull = true
public bool SendCommand(string Packet) {
bool Successfull = false;
if (this.boolTXEnable && this.Connected) {
byte[] byteTXBuffer;
byteTXBuffer = Encoding.ASCII.GetBytes(Packet + "\r\n");
try {
this.objTCPClient.Client.Send(byteTXBuffer);
} catch {
if (this.ConnectionAbort != null) {
this.ConnectionAbort(this, new EventArgs());
}
}
Successfull = true;
}
if (this.Connected == false) {
if (this.ConnectionAbort != null) {
this.ConnectionAbort(this, new EventArgs());
}
}
return Successfull;
}
#region SET / GET
///
/// shows the connection state
///
public bool Connected {
get {
bool Connected = false;
if (this.objTCPClient != null && this.objTCPClient.Client != null) {
Connected = this.objTCPClient.Connected;
}
return Connected;
}
}
#endregion
}
}