Browse Source

ModBus建立TcpServer发送指令

章政 8 years ago
parent
commit
cdf43076de
1 changed files with 105 additions and 0 deletions
  1. 105 0
      UAS-MES/PublicMethod/ModeBusTCPServer.cs

+ 105 - 0
UAS-MES/PublicMethod/ModeBusTCPServer.cs

@@ -0,0 +1,105 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Net.Sockets;
+using System.Text;
+using System.Threading;
+using System.Windows.Forms;
+
+namespace UAS_MES.PublicMethod
+{
+    class ModeBusTCPServer
+    {
+        //M501指令OK,M502指令OK
+        byte[] M501_ON = new byte[] { 0x00, 0x05, 0x11, 0xF5, 0xFF, 0x00, 0x98, 0xE5 };
+        byte[] M501_OFF = new byte[] { 0x00, 0x05, 0x11, 0xF5, 0x00, 0x00, 0xD9, 0x15 };
+        byte[] M502_ON = new byte[] { 0x00, 0x05, 0x11, 0xF6, 0xFF, 0x00, 0x68, 0xE5 };
+        byte[] M502_OFF = new byte[] { 0x00, 0x05, 0x11, 0xF6, 0x00, 0x00, 0x29, 0x15 };
+
+        Thread threadWatch = null; //负责监听客户端的线程
+        Socket socketWatch = null; //负责监听客户端的套接字
+        List<Socket> socConnections = new List<Socket>();  //创建一个负责和客户端通信的套接字 
+        List<Thread> dictThread = new List<Thread>();
+
+        public ModeBusTCPServer(string IP, string Port)
+        {
+            try
+            {
+                //定义一个套接字用于监听客户端发来的信息  包含3个参数(IP4寻址协议,流式连接,TCP协议)
+                socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+                //服务端发送信息 需要1个IP地址和端口号
+                IPAddress ipaddress = IPAddress.Parse(IP);
+                //将IP地址和端口号绑定到网络节点endpoint上 
+                IPEndPoint endpoint = new IPEndPoint(ipaddress, int.Parse(Port));
+                //监听绑定的网络节点
+                socketWatch.Bind(endpoint);
+                //将套接字的监听队列长度限制为20
+                socketWatch.Listen(20);
+                //创建一个监听线程 
+                threadWatch = new Thread(WatchConnecting);
+                //将窗体线程设置为与后台同步
+                threadWatch.IsBackground = true;
+                //启动线程
+                threadWatch.Start();
+            }
+            catch (Exception e)
+            {
+                MessageBox.Show(e.Message);
+            }
+        }
+
+        private void WatchConnecting()
+        {
+            while (true)  //持续不断监听客户端发来的请求
+            {
+                Socket socConnection = socketWatch.Accept();
+                //创建一个通信线程 
+                ParameterizedThreadStart pts = new ParameterizedThreadStart(ServerRecMsg);
+                Thread thr = new Thread(pts);
+                thr.IsBackground = true;
+                socConnections.Add(socConnection);
+                //启动线程
+                thr.Start(socConnection);
+                dictThread.Add(thr);
+            }
+        }
+
+        private void ServerRecMsg(object obj)
+        {
+            Socket soc = obj as Socket;
+            byte[] data = new byte[1024];
+            soc.Receive(data);
+            int length = data[5];
+            byte[] datashow = new byte[length + 6];
+            for (int i = 0; i <= length + 5; i++)
+                datashow[i] = data[i];
+            string stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串
+            Console.WriteLine(stringdata);
+        }
+
+        public void SendOrder(string Code)
+        {
+            foreach (Socket socConnection in socConnections)
+            {
+                switch (Code)
+                {
+                    case "M501_ON":
+                        socConnection.Send(M501_ON);
+                        break;
+                    case "M501_OFF":
+                        socConnection.Send(M501_OFF);
+                        break;
+                    case "M502_ON":
+                        socConnection.Send(M502_ON);
+                        break;
+                    case "M502_OFF":
+                        socConnection.Send(M502_OFF);
+                        break;
+                    default:
+                        break;
+                }
+            }
+        }
+    }
+}