ModeBusTCPServer.cs 5.7 KB

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