ModeBusTCPServer.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 void 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. }
  86. catch (Exception e)
  87. {
  88. MessageBox.Show(e.Message);
  89. }
  90. }
  91. private void WatchConnecting()
  92. {
  93. socConnection = socketWatch.Accept();
  94. while (true) //持续不断监听客户端发来的请求
  95. {
  96. byte[] data = new byte[1024];
  97. socConnection.Receive(data);
  98. int length = data[5];
  99. byte[] datashow = new byte[length + 6];
  100. for (int i = 0; i <= length + 5; i++)
  101. {
  102. datashow[i] = data[i];
  103. }
  104. string stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串
  105. stringdata = stringdata.Replace("-", "");
  106. //503口打开
  107. if (stringdata.Substring(7, 1) == "1")
  108. {
  109. socConnection.Send(M501_OFF);
  110. socConnection.Send(M502_OFF);
  111. socConnection.Send(M503_OFF);
  112. WaitFinish = false;
  113. }
  114. }
  115. }
  116. public void Close()
  117. {
  118. threadWatch.Abort();
  119. socketWatch.Close();
  120. isOpen = false;
  121. }
  122. public void SendOrder(string Code)
  123. {
  124. switch (Code)
  125. {
  126. case "M501_ON":
  127. socConnection.Send(M501_ON);
  128. WaitFinish = true;
  129. while (WaitFinish)
  130. {
  131. Thread.Sleep(80);
  132. socConnection.Send(M503_READ);
  133. }
  134. break;
  135. case "M501_OFF":
  136. socConnection.Send(M501_OFF);
  137. break;
  138. case "M502_ON":
  139. socConnection.Send(M502_ON);
  140. WaitFinish = true;
  141. while (WaitFinish)
  142. {
  143. Thread.Sleep(80);
  144. socConnection.Send(M503_READ);
  145. }
  146. break;
  147. case "M502_OFF":
  148. socConnection.Send(M502_OFF);
  149. break;
  150. case "M503_READ":
  151. socConnection.Send(M503_READ);
  152. break;
  153. case "M503_OFF":
  154. socConnection.Send(M503_OFF);
  155. break;
  156. default:
  157. break;
  158. }
  159. }
  160. }
  161. }