ModeBusTCPServer.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Windows.Forms;
  9. namespace UAS_MES.PublicMethod
  10. {
  11. class ModeBusTCPServer
  12. {
  13. //M501指令OK,M502指令OK
  14. byte[] M501_ON = new byte[] { 0x00, 0x05, 0x11, 0xF5, 0xFF, 0x00, 0x98, 0xE5 };
  15. byte[] M501_OFF = new byte[] { 0x00, 0x05, 0x11, 0xF5, 0x00, 0x00, 0xD9, 0x15 };
  16. byte[] M502_ON = new byte[] { 0x00, 0x05, 0x11, 0xF6, 0xFF, 0x00, 0x68, 0xE5 };
  17. byte[] M502_OFF = new byte[] { 0x00, 0x05, 0x11, 0xF6, 0x00, 0x00, 0x29, 0x15 };
  18. byte[] M503_READ = new byte[] { 0x00, 0x01, 0x11, 0xF7, 0x00, 0x01, 0x48, 0xD5 };
  19. byte[] M503_OFF = new byte[] { 0x00, 0x05, 0x11, 0xF7, 0x00, 0x00, 0x78, 0xD5 };
  20. private bool isOpen = false;
  21. Thread threadWatch = null; //负责监听客户端的线程
  22. Socket socketWatch = null; //负责监听客户端的套接字
  23. Socket socConnection;
  24. //等待机械臂完成动作
  25. bool WaitFinish = true;
  26. public bool IsOpen
  27. {
  28. get
  29. {
  30. return isOpen;
  31. }
  32. set
  33. {
  34. isOpen = value;
  35. }
  36. }
  37. public string IP
  38. {
  39. get
  40. {
  41. return iP;
  42. }
  43. set
  44. {
  45. iP = value;
  46. }
  47. }
  48. public string Port
  49. {
  50. get
  51. {
  52. return port;
  53. }
  54. set
  55. {
  56. port = value;
  57. }
  58. }
  59. private string iP;
  60. private string port;
  61. public ModeBusTCPServer()
  62. {
  63. }
  64. public bool Open()
  65. {
  66. try
  67. {
  68. //定义一个套接字用于监听客户端发来的信息 包含3个参数(IP4寻址协议,流式连接,TCP协议)
  69. socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  70. //服务端发送信息 需要1个IP地址和端口号
  71. IPAddress ipaddress = IPAddress.Parse(iP);
  72. //将IP地址和端口号绑定到网络节点endpoint上
  73. IPEndPoint endpoint = new IPEndPoint(ipaddress, int.Parse(port));
  74. //监听绑定的网络节点
  75. socketWatch.Bind(endpoint);
  76. //将套接字的监听队列长度限制为20
  77. socketWatch.Listen(20);
  78. //创建一个监听线程
  79. threadWatch = new Thread(WatchConnecting);
  80. //将窗体线程设置为与后台同步
  81. threadWatch.IsBackground = true;
  82. //启动线程
  83. threadWatch.Start();
  84. isOpen = true;
  85. return true;
  86. }
  87. catch (Exception e)
  88. {
  89. MessageBox.Show(e.Message);
  90. return false;
  91. }
  92. }
  93. private void WatchConnecting()
  94. {
  95. socConnection = socketWatch.Accept();
  96. while (true) //持续不断监听客户端发来的请求
  97. {
  98. byte[] data = new byte[1024];
  99. socConnection.Receive(data);
  100. int length = data[5];
  101. byte[] datashow = new byte[length + 6];
  102. for (int i = 0; i <= length + 5; i++)
  103. {
  104. datashow[i] = data[i];
  105. }
  106. string stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串
  107. stringdata = stringdata.Replace("-", "");
  108. //503口打开
  109. if (stringdata.Substring(7, 1) == "1")
  110. {
  111. socConnection.Send(M501_OFF);
  112. socConnection.Send(M502_OFF);
  113. socConnection.Send(M503_OFF);
  114. WaitFinish = false;
  115. }
  116. }
  117. }
  118. public void Close()
  119. {
  120. threadWatch.Abort();
  121. socketWatch.Close();
  122. isOpen = false;
  123. }
  124. public void SendOrder(string Code)
  125. {
  126. switch (Code)
  127. {
  128. case "M501_ON":
  129. socConnection.Send(M501_ON);
  130. WaitFinish = true;
  131. int LoopTime = 0;
  132. while (WaitFinish && LoopTime <= 40)
  133. {
  134. Thread.Sleep(100);
  135. LoopTime += LoopTime + 1;
  136. socConnection.Send(M503_READ);
  137. }
  138. break;
  139. case "M501_OFF":
  140. socConnection.Send(M501_OFF);
  141. break;
  142. case "M502_ON":
  143. socConnection.Send(M502_ON);
  144. WaitFinish = true;
  145. int LoopTime1 = 0;
  146. while (WaitFinish && LoopTime1 <= 40)
  147. {
  148. Thread.Sleep(100);
  149. LoopTime1 += LoopTime1 + 1;
  150. socConnection.Send(M503_READ);
  151. }
  152. break;
  153. case "M502_OFF":
  154. socConnection.Send(M502_OFF);
  155. break;
  156. case "M503_READ":
  157. socConnection.Send(M503_READ);
  158. break;
  159. case "M503_OFF":
  160. socConnection.Send(M503_OFF);
  161. break;
  162. default:
  163. break;
  164. }
  165. }
  166. }
  167. }