| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- /*********************************************************************
- *
- * 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();
- }
- }
- /// <summary>
- /// Send a command to the Host
- /// </summary>
- /// <param name="Command">the command to transmit</param>
- /// <returns>transmit sucessfull = true</returns>
- 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
- /// <summary>
- /// shows the connection state
- /// </summary>
- public bool Connected {
- get {
- bool Connected = false;
- if (this.objTCPClient != null && this.objTCPClient.Client != null) {
- Connected = this.objTCPClient.Connected;
- }
- return Connected;
- }
- }
- #endregion
- }
- }
|