ModeBusTCPServer.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Threading;
  5. using System.Windows.Forms;
  6. namespace UAS_MES_NEW.PublicMethod
  7. {
  8. class ModeBusTCPServer
  9. {
  10. //M501指令OK,M502指令OK
  11. //1,2位表示指令类型,读写或者开关,3,4位表示寄存器地址,5,6表示对寄存器的操作,7,8位表示补码,需要计算
  12. byte[] M501_ON = new byte[] { 0x00, 0x05, 0x11, 0xF5, 0xFF, 0x00, 0x98, 0xE5 };
  13. byte[] M501_OFF = new byte[] { 0x00, 0x05, 0x11, 0xF5, 0x00, 0x00, 0xD9, 0x15 };
  14. byte[] M502_ON = new byte[] { 0x00, 0x05, 0x11, 0xF6, 0xFF, 0x00, 0x68, 0xE5 };
  15. byte[] M502_OFF = new byte[] { 0x00, 0x05, 0x11, 0xF6, 0x00, 0x00, 0x29, 0x15 };
  16. byte[] M503_READ = new byte[] { 0x00, 0x01, 0x11, 0xF7, 0x00, 0x01, 0x48, 0xD5 };
  17. byte[] M503_OFF = new byte[] { 0x00, 0x05, 0x11, 0xF7, 0x00, 0x00, 0x78, 0xD5 };
  18. private bool isOpen = false;
  19. Thread threadWatch = null; //负责监听客户端的线程
  20. Socket socketWatch = null; //负责监听客户端的套接字
  21. Socket socConnection;
  22. //等待机械臂完成动作
  23. bool WaitFinish = true;
  24. public bool IsOpen
  25. {
  26. get
  27. {
  28. return isOpen;
  29. }
  30. set
  31. {
  32. isOpen = value;
  33. }
  34. }
  35. public string IP
  36. {
  37. get
  38. {
  39. return iP;
  40. }
  41. set
  42. {
  43. iP = value;
  44. }
  45. }
  46. public string Port
  47. {
  48. get
  49. {
  50. return port;
  51. }
  52. set
  53. {
  54. port = value;
  55. }
  56. }
  57. private string iP;
  58. private string port;
  59. public ModeBusTCPServer()
  60. {
  61. }
  62. public bool Open()
  63. {
  64. try
  65. {
  66. //定义一个套接字用于监听客户端发来的信息 包含3个参数(IP4寻址协议,流式连接,TCP协议)
  67. socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  68. //服务端发送信息 需要1个IP地址和端口号
  69. IPAddress ipaddress = IPAddress.Parse(iP);
  70. //将IP地址和端口号绑定到网络节点endpoint上
  71. IPEndPoint endpoint = new IPEndPoint(ipaddress, int.Parse(port));
  72. //监听绑定的网络节点
  73. socketWatch.Bind(endpoint);
  74. //将套接字的监听队列长度限制为20
  75. socketWatch.Listen(20);
  76. //创建一个监听线程
  77. threadWatch = new Thread(WatchConnecting);
  78. //将窗体线程设置为与后台同步
  79. threadWatch.IsBackground = true;
  80. //启动线程
  81. threadWatch.Start();
  82. isOpen = true;
  83. return true;
  84. }
  85. catch (Exception e)
  86. {
  87. MessageBox.Show(e.Message);
  88. return false;
  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. int LoopTime = 0;
  130. while (WaitFinish && LoopTime <= 40)
  131. {
  132. Thread.Sleep(100);
  133. LoopTime += LoopTime + 1;
  134. socConnection.Send(M503_READ);
  135. }
  136. break;
  137. case "M501_OFF":
  138. socConnection.Send(M501_OFF);
  139. break;
  140. case "M502_ON":
  141. socConnection.Send(M502_ON);
  142. WaitFinish = true;
  143. int LoopTime1 = 0;
  144. while (WaitFinish && LoopTime1 <= 40)
  145. {
  146. Thread.Sleep(100);
  147. LoopTime1 += LoopTime1 + 1;
  148. socConnection.Send(M503_READ);
  149. }
  150. break;
  151. case "M502_OFF":
  152. socConnection.Send(M502_OFF);
  153. break;
  154. case "M503_READ":
  155. socConnection.Send(M503_READ);
  156. break;
  157. case "M503_OFF":
  158. socConnection.Send(M503_OFF);
  159. break;
  160. default:
  161. break;
  162. }
  163. }
  164. }
  165. }