ModeBusTCPServer.cs 4.5 KB

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