Browse Source

添加TCPClient

章政 7 years ago
parent
commit
0340c6d840

+ 18 - 14
PLCDataReader/Main.cs

@@ -430,6 +430,7 @@ namespace UAS_PLCDataReader
             }
         }
 
+        Dictionary<string, ModBusTCPClient> client = new Dictionary<string, ModBusTCPClient>();
         /// <summary>
         /// 轮询执行的业务
         /// </summary>
@@ -442,6 +443,7 @@ namespace UAS_PLCDataReader
             DataHelper dh = pl.Dh;
             string Decode = pl.DeviceCode;
             string Dccode = pl.CommandCode;
+            string DpcID = pl.Id.ToString();
             DataTable dt = (DataTable)dh.ExecuteSql("select dnc_ip,dnc_port from DEVICENETCONFIG where dnc_decode='" + Decode + "'", "select");
             DataTable dt1 = (DataTable)dh.ExecuteSql("select dc_sendcoding,dc_value,dc_receivecoding from devicecommand where dc_code='" + Dccode + "'", "select");
             if (dt.Rows.Count > 0)
@@ -458,8 +460,17 @@ namespace UAS_PLCDataReader
                     Command = dt1.Rows[0]["dc_value"].ToString();
                     SendCommandByteSize = Encoding.Default.GetBytes(Command.ToCharArray()).Length;
                 }
+                if (client.ContainsKey(DpcID))
+                {
+                    client[DpcID].Send(Command);
+                }
+                else
+                {
+                    ModBusTCPClient modclient = new ModBusTCPClient(dt.Rows[0]["dnc_ip"].ToString(), int.Parse(dt.Rows[0]["dnc_port"].ToString()));
+                    client.Add(DpcID, modclient);
+                }
                 //发送指令
-                mbt.Send(IP, SendCoding, ReceiveCoding, Command);
+                //mbt.Send(IP, SendCoding, ReceiveCoding, Command);
                 //如果不包含该项数据则在键值对中添加
                 if (!ReturnData.ContainsKey(Decode))
                 {
@@ -482,12 +493,12 @@ namespace UAS_PLCDataReader
                     //检测键值对是否发生变化,发生变化时赋予新值
                 }
                 //成功返回了信息
-                if (mbt.Returnvalue.ContainsKey(IP))
+                if (client[DpcID].Returnvalue.ContainsKey(IP))
                 {
-                    int ReceiveCommandByteSize = Encoding.Default.GetBytes(mbt.Returnvalue[IP].ToCharArray()).Length;
+                    int ReceiveCommandByteSize = Encoding.Default.GetBytes(client[DpcID].Returnvalue[IP].ToCharArray()).Length;
                     //存放返回的所有数据
                     Dictionary<string, string> ItemData = new Dictionary<string, string>();
-                    int[] Arr = BaseUtil.GetDecimalData(BaseUtil.ASCIIToString(mbt.Returnvalue[IP]), 8);
+                    int[] Arr = BaseUtil.GetDecimalData(BaseUtil.ASCIIToString(client[DpcID].Returnvalue[IP]), 8);
                     for (int i = 0; i < Arr.Length; i++)
                     {
                         ItemData.Add("Item" + i, Arr[i].ToString());
@@ -521,7 +532,7 @@ namespace UAS_PLCDataReader
                             LogicHandler.DoDeviceDataDiffLog(pl.DeviceCode, pl.DeviceName, pl.CommandCode, User.UserName);
                         }
                     }
-                    mbt.Returnvalue.Remove(IP);
+                    client[DpcID].Returnvalue.Remove(IP);
                     //SQL.Clear();
                     ////更新轮询状态
                     //sql.Clear();
@@ -564,6 +575,7 @@ namespace UAS_PLCDataReader
             }
             PollSettingPaintRowIndex.Clear();
             GridPollingSetting.Focus();
+            client.Clear();
             Ptime.Clear();
         }
 
@@ -574,22 +586,14 @@ namespace UAS_PLCDataReader
                 GridViewPollSetting.SetRowCellValue(e.RowHandle, e.Column, e.Value);
                 if (GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ENABLE").ToString() == "0")
                 {
-                    //DialogResult cancel = XtraMessageBox.Show("确认禁用该轮询?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
-                    //if (cancel.ToString() == "Yes")
-                    //{
                     int id = int.Parse(GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ID").ToString());
                     if (Ptime.ContainsKey(id))
                     {
                         Ptime[id].Stop();
                         Ptime.Remove(id);
+                        client.Remove(id.ToString());
                         PollSettingPaintRowIndex.Remove(e.RowHandle);
                     }
-                    //}
-                    //else
-                    //{
-                    //    Cancel = false;
-                    //    GridViewPollSetting.SetRowCellValue(e.RowHandle, e.Column, -1);
-                    //}
                 }
                 else
                 {

+ 123 - 0
PLCDataReader/PublicMethod/ModBusTCPClient.cs

@@ -0,0 +1,123 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Net.Sockets;
+using System.Text;
+
+namespace UAS_PLCDataReader.PublicMethod
+{
+    class ModBusTCPClient
+    {
+        private Socket socket;
+
+        private IPEndPoint serverFullAddr;//完整终端地址
+
+        private bool isOpen = false;
+
+        private bool receiveData;
+
+        private Dictionary<string, string> returnvalue = new Dictionary<string, string>();
+
+        public bool IsOpen
+        {
+            get
+            {
+                return isOpen;
+            }
+
+            set
+            {
+                isOpen = value;
+            }
+        }
+
+        public string IP
+        {
+            get
+            {
+                return iP;
+            }
+
+            set
+            {
+                iP = value;
+            }
+        }
+
+        public string Port
+        {
+            get
+            {
+                return port;
+            }
+
+            set
+            {
+                port = value;
+            }
+        }
+
+        public bool ReceiveData
+        {
+            get
+            {
+                return receiveData;
+            }
+
+            set
+            {
+                receiveData = value;
+            }
+        }
+
+        public Dictionary<string, string> Returnvalue
+        {
+            get
+            {
+                return returnvalue;
+            }
+
+            set
+            {
+                returnvalue = value;
+            }
+        }
+
+        private string iP;
+
+        private string port;
+
+        public ModBusTCPClient(string IP, int Port)
+        {
+            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+            serverFullAddr = new IPEndPoint(IPAddress.Parse(IP), Port);//设置IP,端口
+            try
+            {
+                socket.Connect(serverFullAddr);
+            }
+            catch (Exception)
+            {
+
+            }
+        }
+
+        public void Send(string Command)
+        {
+            Command = Command.Replace(" ", "");
+            byte[] arr = new byte[Command.Length / 2];
+            for (int i = 0; i < Command.Length / 2; i++)
+            {
+                arr[i] = (byte)Convert.ToInt32(Command.Substring(i * 2, 2), 16);
+            }
+            socket.Send(arr);
+            byte[] receive = new byte[1024 * 1024];
+            int length = socket.Receive(receive);
+            Console.WriteLine(BaseUtil.ByteToHexadecimalString(receive, length));
+            if (!returnvalue.ContainsKey(socket.RemoteEndPoint.ToString()))
+            {
+                returnvalue.Add(socket.RemoteEndPoint.ToString(), BaseUtil.ByteToHexadecimalString(receive, length));
+            }
+        }
+    }
+}

+ 1 - 0
PLCDataReader/UAS_PLCDataReader.csproj

@@ -275,6 +275,7 @@
     <Compile Include="PublicMethod\BaseUtil.cs" />
     <Compile Include="PublicMethod\LogicHandler.cs" />
     <Compile Include="PublicMethod\LogManager.cs" />
+    <Compile Include="PublicMethod\ModBusTCPClient.cs" />
     <Compile Include="PublicMethod\ModeBusTCPServer.cs" />
     <Compile Include="PublicMethod\PollingTask.cs" />
     <Compile Include="PublicMethod\PollingTimer.cs">