TCPCommunicationClass.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*********************************************************************
  2. *
  3. * Afag Telnet C# Integration Sample Header
  4. *
  5. *********************************************************************
  6. * FileName: TCPCommunicationClass.cs
  7. * Dependencies: aflex_CSharp_Integration_Sample
  8. * Company: ima-tec GmbH
  9. *
  10. * Software License Agreement
  11. *
  12. *
  13. * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
  14. * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
  15. * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
  16. * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  17. * AFAG BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
  18. * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
  19. * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
  20. * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
  21. * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
  22. * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
  23. * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
  24. *
  25. *
  26. ********************************************************************/
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Linq;
  30. using System.Net;
  31. using System.Net.Sockets;
  32. using System.Text;
  33. using System.Threading.Tasks;
  34. //using TouchSocket.Core;
  35. //using TouchSocket.Sockets;
  36. namespace aflex_CSharp_Integration_Sample
  37. {
  38. class TCPCommunicationClass
  39. {
  40. public TcpClient objTCPClient = null;
  41. private byte[] byteReceiveBuffer = new byte[256];
  42. private bool boolTXEnable = false;
  43. public delegate void ReadResultDelegate(string Result);
  44. //Events
  45. public event ReadResultDelegate ResultReceived; //event of valid recieve package
  46. public event EventHandler ConnectionAbort;
  47. public TCPCommunicationClass()
  48. {
  49. }
  50. public bool Connect(IPAddress IPAddress, int Port) { //connect to the Telnet server
  51. bool Successful = false;
  52. if (this.objTCPClient != null) {
  53. this.objTCPClient.Close(); //if there is already a connection, close it
  54. }
  55. this.objTCPClient = new TcpClient();
  56. try {
  57. this.objTCPClient.Connect(IPAddress, Port);
  58. this.objTCPClient.Client.BeginReceive(byteReceiveBuffer, 0, 256, SocketFlags.None, new AsyncCallback(asyncResultRead), null);
  59. Successful = true;
  60. this.boolTXEnable = true;
  61. } catch {
  62. }
  63. return Successful;
  64. }
  65. public void Disconnect() { //disconnect from server
  66. this.objTCPClient.Close();
  67. }
  68. private void asyncResultRead(IAsyncResult Result) { // here we read out an asynchronously received package
  69. try {
  70. if (Result.IsCompleted) {
  71. this.objTCPClient.Client.EndReceive(Result);
  72. string returnString = "";
  73. int y = 0;
  74. for (int i = 0; i < byteReceiveBuffer.Length - 2; i++) {
  75. if (i < 250 && Convert.ToChar(byteReceiveBuffer[i]) == 'H' && Convert.ToChar(byteReceiveBuffer[i + 1]) == 'B' && Convert.ToChar(byteReceiveBuffer[i + 2]) == 'T') {
  76. returnString = String.Empty;
  77. for (int x = i; x < i + 5; x++) {
  78. if (byteReceiveBuffer[x] != 0) {
  79. returnString += (Convert.ToChar(byteReceiveBuffer[x]).ToString());
  80. y = x;
  81. byteReceiveBuffer[x] = 0;
  82. }
  83. }
  84. if (this.ResultReceived != null) {
  85. this.ResultReceived(returnString);
  86. }
  87. }
  88. if (byteReceiveBuffer[i] == 13 && byteReceiveBuffer[i + 1] == 10) {
  89. returnString = String.Empty;
  90. for (int x = y; x < i; x++) {
  91. if (byteReceiveBuffer[x] != 0) {
  92. returnString += (Convert.ToChar(byteReceiveBuffer[x]).ToString());
  93. y = x;
  94. byteReceiveBuffer[x] = 0;
  95. }
  96. }
  97. if (this.ResultReceived != null) {
  98. this.ResultReceived(returnString);
  99. }
  100. }
  101. }
  102. //reset buffer
  103. byteReceiveBuffer = new byte[256];
  104. this.objTCPClient.Client.BeginReceive(byteReceiveBuffer, 0, 256, SocketFlags.None, new AsyncCallback(asyncResultRead), null);
  105. }
  106. } catch {
  107. this.objTCPClient.Close();
  108. }
  109. }
  110. /// <summary>
  111. /// Send a command to the Host
  112. /// </summary>
  113. /// <param name="Command">the command to transmit</param>
  114. /// <returns>transmit sucessfull = true</returns>
  115. public bool SendCommand(string Packet) {
  116. bool Successfull = false;
  117. if (this.boolTXEnable && this.Connected) {
  118. byte[] byteTXBuffer;
  119. byteTXBuffer = Encoding.ASCII.GetBytes(Packet + "\r\n");
  120. try {
  121. this.objTCPClient.Client.Send(byteTXBuffer);
  122. } catch {
  123. if (this.ConnectionAbort != null) {
  124. this.ConnectionAbort(this, new EventArgs());
  125. }
  126. }
  127. Successfull = true;
  128. }
  129. if (this.Connected == false) {
  130. if (this.ConnectionAbort != null) {
  131. this.ConnectionAbort(this, new EventArgs());
  132. }
  133. }
  134. return Successfull;
  135. }
  136. #region SET / GET
  137. /// <summary>
  138. /// shows the connection state
  139. /// </summary>
  140. public bool Connected {
  141. get {
  142. bool Connected = false;
  143. if (this.objTCPClient != null && this.objTCPClient.Client != null) {
  144. Connected = this.objTCPClient.Connected;
  145. }
  146. return Connected;
  147. }
  148. }
  149. #endregion
  150. }
  151. }