yinyujing пре 4 година
родитељ
комит
d7351cc90b
1 измењених фајлова са 51 додато и 46 уклоњено
  1. 51 46
      NB_IOT_TCP_HP_SOCKET/MainForm_TCP.cs

+ 51 - 46
NB_IOT_TCP_HP_SOCKET/MainForm_TCP.cs

@@ -562,59 +562,68 @@ namespace NB_IOT_TCP_HP_SOCKET
                     DataTable dt = ProtocolAnalysisTools.GetCommunicationDevice();
                     for (int i = 0; i < dt.Rows.Count; i++)
                     {
-                        string transId = dt.Rows[i]["传输协议参数"].ToString().Split(',')[0];
-                        // 获取设备的最新链接ID
-                        if (!deveiceConnId.ContainsKey(transId))
-                        {
-                            continue;
-                        }
-                        IntPtr conId = deveiceConnId[transId];
-
-                        int _addressId = Convert.ToInt32(dt.Rows[i]["设备地址"]);
-
-                        ModBusDeviceType modBusDeviceType = new ModBusDeviceType(dt.Rows[i]["设备协议参数"].ToString());
-                        int _analogStartAddress = Convert.ToInt32(modBusDeviceType.AnalogStartAddress);
-                        int _analogRegisterCount = Convert.ToInt32(modBusDeviceType.AnalogRegisterCount);
-                        int _switchStartAddress = Convert.ToInt32(modBusDeviceType.SwitchStartAddress);
-                        int _switchRegisterCount = Convert.ToInt32(modBusDeviceType.SwitchRegisterCount);
-
-                        string transID = dt.Rows[i]["ID"].ToString(); // 通讯设备ID
-                        byte[] buffer;
-                        Modbus[] modbusArr = new Modbus[2];
-                        bool sendFlag = false;
-                        Modbus modbus;
-                        if (((ushort)_analogRegisterCount) != 0)
+
+                        try
                         {
-                            modbus = new Modbus((ushort)_addressId, Modbus.FunctionCode.F04, (ushort)_analogStartAddress, (ushort)_analogRegisterCount);
-                            buffer = new byte[0];
-                            if (Modbus.Pack(modbus, modBusDeviceType, out buffer).Result)
+                            string transId = dt.Rows[i]["传输协议参数"].ToString().Split(',')[0];
+                            // 获取设备的最新链接ID
+                            if (!deveiceConnId.ContainsKey(transId))
                             {
-                                if (server.Send(conId, buffer, buffer.Length))
+                                continue;
+                            }
+                            IntPtr conId = deveiceConnId[transId];
+
+                            int _addressId = Convert.ToInt32(dt.Rows[i]["设备地址"]);
+
+                            ModBusDeviceType modBusDeviceType = new ModBusDeviceType(dt.Rows[i]["设备协议参数"].ToString());
+                            int _analogStartAddress = Convert.ToInt32(modBusDeviceType.AnalogStartAddress);
+                            int _analogRegisterCount = Convert.ToInt32(modBusDeviceType.AnalogRegisterCount);
+                            int _switchStartAddress = Convert.ToInt32(modBusDeviceType.SwitchStartAddress);
+                            int _switchRegisterCount = Convert.ToInt32(modBusDeviceType.SwitchRegisterCount);
+
+                            string transID = dt.Rows[i]["ID"].ToString(); // 通讯设备ID
+                            byte[] buffer;
+                            Modbus[] modbusArr = new Modbus[2];
+                            bool sendFlag = false;
+                            Modbus modbus;
+                            if (((ushort)_analogRegisterCount) != 0)
+                            {
+                                modbus = new Modbus((ushort)_addressId, Modbus.FunctionCode.F04, (ushort)_analogStartAddress, (ushort)_analogRegisterCount);
+                                buffer = new byte[0];
+                                if (Modbus.Pack(modbus, modBusDeviceType, out buffer).Result)
                                 {
-                                    AddMsg($" > [{conId},OnSend] -> {transId} ({buffer.Length} bytes) ModBus读模拟量寄存器 \n {BitConverter.ToString(buffer, 0, buffer.Length).Replace("-", " ")}", lbReceiveData);
-                                    sendFlag = true;
-                                    modbusArr[0] = modbus;
+                                    if (server.Send(conId, buffer, buffer.Length))
+                                    {
+                                        AddMsg($" > [{conId},OnSend] -> {transId} ({buffer.Length} bytes) ModBus读模拟量寄存器 \n {BitConverter.ToString(buffer, 0, buffer.Length).Replace("-", " ")}", lbReceiveData);
+                                        sendFlag = true;
+                                        modbusArr[0] = modbus;
+                                    }
                                 }
                             }
-                        }
-                        if (_switchRegisterCount != 0)
-                        {
-                            modbus = new Modbus((ushort)_addressId, Modbus.FunctionCode.F02, (ushort)_switchStartAddress, (ushort)_switchRegisterCount);
-                            buffer = new byte[0];
-                            if (Modbus.Pack(modbus, modBusDeviceType, out buffer).Result)
+                            if (_switchRegisterCount != 0)
                             {
-                                if (server.Send(conId, buffer, buffer.Length))
+                                modbus = new Modbus((ushort)_addressId, Modbus.FunctionCode.F02, (ushort)_switchStartAddress, (ushort)_switchRegisterCount);
+                                buffer = new byte[0];
+                                if (Modbus.Pack(modbus, modBusDeviceType, out buffer).Result)
                                 {
-                                    AddMsg($" > [{conId},OnSend] -> {transId} ({buffer.Length} bytes) ModBus读开关量输入状态 \n {BitConverter.ToString(buffer, 0, buffer.Length).Replace("-", " ")}", lbReceiveData);
-                                    sendFlag = true;
-                                    modbusArr[1] = modbus;
+                                    if (server.Send(conId, buffer, buffer.Length))
+                                    {
+                                        AddMsg($" > [{conId},OnSend] -> {transId} ({buffer.Length} bytes) ModBus读开关量输入状态 \n {BitConverter.ToString(buffer, 0, buffer.Length).Replace("-", " ")}", lbReceiveData);
+                                        sendFlag = true;
+                                        modbusArr[1] = modbus;
+                                    }
                                 }
                             }
+                            if (sendFlag)
+                            {
+
+                                sendComm[transID] = modbusArr;
+                            }
                         }
-                        if (sendFlag)
+                        catch (Exception ex)
                         {
-
-                            sendComm[transID] = modbusArr;
+                            AddMsg(ex.Message, lbErrorData);
+                            continue;
                         }
                     }
                     isTimeRuning = false;
@@ -633,9 +642,6 @@ namespace NB_IOT_TCP_HP_SOCKET
         /// </summary>
         private void DealWithSendThread()
         {
-            //定义变量
-            byte[] arr = null;
-            bool flag = true;
             while (true)
             {
                 try
@@ -647,7 +653,6 @@ namespace NB_IOT_TCP_HP_SOCKET
                     else
                     {
                     Label_1:
-                        arr = null;
                         // 处理数据
                         ClientInfo clientInfo = (ClientInfo)_SendConidQueue.Dequeue();
                         PSProtocol psp = clientInfo.pSProtocol;