ModeBusTCPServer.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. string ip = "";
  25. string port = "";
  26. public bool IsOpen
  27. {
  28. get
  29. {
  30. return isOpen;
  31. }
  32. set
  33. {
  34. isOpen = value;
  35. }
  36. }
  37. public ModeBusTCPServer(string IP, string Port)
  38. {
  39. ip = IP;
  40. port = Port;
  41. }
  42. public void Open()
  43. {
  44. try
  45. {
  46. //定义一个套接字用于监听客户端发来的信息 包含3个参数(IP4寻址协议,流式连接,TCP协议)
  47. socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  48. //服务端发送信息 需要1个IP地址和端口号
  49. IPAddress ipaddress = IPAddress.Parse(ip);
  50. //将IP地址和端口号绑定到网络节点endpoint上
  51. IPEndPoint endpoint = new IPEndPoint(ipaddress, int.Parse(port));
  52. //监听绑定的网络节点
  53. socketWatch.Bind(endpoint);
  54. //将套接字的监听队列长度限制为20
  55. socketWatch.Listen(20);
  56. //创建一个监听线程
  57. threadWatch = new Thread(WatchConnecting);
  58. //将窗体线程设置为与后台同步
  59. threadWatch.IsBackground = true;
  60. //启动线程
  61. threadWatch.Start();
  62. isOpen = true;
  63. }
  64. catch (Exception e)
  65. {
  66. MessageBox.Show(e.Message);
  67. }
  68. }
  69. private void WatchConnecting()
  70. {
  71. socConnection = socketWatch.Accept();
  72. while (true) //持续不断监听客户端发来的请求
  73. {
  74. byte[] data = new byte[1024];
  75. socConnection.Receive(data);
  76. int length = data[5];
  77. byte[] datashow = new byte[length + 6];
  78. for (int i = 0; i <= length + 5; i++)
  79. {
  80. datashow[i] = data[i];
  81. }
  82. string stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串
  83. stringdata = stringdata.Replace("-", "");
  84. }
  85. }
  86. public void Close()
  87. {
  88. threadWatch.Abort();
  89. socketWatch.Close();
  90. isOpen = false;
  91. }
  92. public void SendOrder(string Code)
  93. {
  94. switch (Code)
  95. {
  96. case "M501_ON":
  97. socConnection.Send(M501_ON);
  98. break;
  99. case "M501_OFF":
  100. socConnection.Send(M501_OFF);
  101. break;
  102. case "M502_ON":
  103. socConnection.Send(M502_ON);
  104. break;
  105. case "M502_OFF":
  106. socConnection.Send(M502_OFF);
  107. break;
  108. case "M503_READ":
  109. socConnection.Send(M503_READ);
  110. break;
  111. case "M503_OFF":
  112. socConnection.Send(M503_OFF);
  113. break;
  114. default:
  115. break;
  116. }
  117. }
  118. }
  119. }