diff --git a/ViewModel/MainWindowViewModel.cs b/ViewModel/MainWindowViewModel.cs index cc7278f..07f3054 100644 --- a/ViewModel/MainWindowViewModel.cs +++ b/ViewModel/MainWindowViewModel.cs @@ -9,7 +9,9 @@ using Microsoft.Win32; using Newtonsoft.Json.Linq; using NModbus; using NModbus.Serial; +using OpenHardwareMonitor.Hardware; using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; @@ -35,7 +37,6 @@ using System.Windows.Input; using System.Windows.Media; using System.Windows.Threading; using TouchSocket.Sockets; -using OpenHardwareMonitor.Hardware; namespace DyeingComputer.ViewModel { @@ -3392,35 +3393,50 @@ namespace DyeingComputer.ViewModel { await Task.Run(() => { - try + while (true) { - if (protocols_ == "MODBUS_RTU") + try { - DO = master.ReadCoils(slaveId, 0, DO_L); //读取线圈 - RW = master.ReadHoldingRegisters(slaveId, 0, RW_L); //读取寄存器 - master.WriteMultipleCoils(slaveId, 0, DQ); //写入线圈 - master.WriteMultipleRegisters(slaveId, 0, DW); //写入寄存器 - }//MODBUSRTU - else + if (protocols_ == "MODBUS_RTU") + { + while (_logQueue.Count>0) + { + QueueS d=_logQueue.Dequeue(); + master.WriteSingleCoil(slaveId,Convert.ToUInt16(d.ID),d.D_DAT); + } + DO = master.ReadCoils(slaveId, 0, DO_L); //读取线圈 + RW = master.ReadHoldingRegisters(slaveId, 0, RW_L); //读取寄存器 + DQ = master.ReadCoils(slaveId, 0, DQ_L); + //master.WriteMultipleCoils(slaveId, 0, DQ); //写入线圈 + master.WriteMultipleRegisters(slaveId, 0, DW); //写入寄存器 + }//MODBUSRTU + else + { + while (_logQueue.Count > 0) + { + QueueS d = _logQueue.Dequeue(); + master.WriteSingleCoil(slaveId, Convert.ToUInt16(d.ID+02000), d.D_DAT); + } + DO = master.ReadCoils(slaveId, 02255, DO_L); //读取线圈 + RW = master.ReadHoldingRegisters(slaveId, 06000, RW_L); //读取寄存器 + DQ = master.ReadCoils(slaveId, 02000, DQ_L); + // master.WriteMultipleCoils(slaveId, 02000, DQ); //写入线圈 + master.WriteMultipleRegisters(slaveId, 07000, DW); //写入寄存器 + }//SCMODBUSRTU + LINK_OK = true; + ERRinf.ERRinf_d(errTabler, "ERR001"); + LINK_RUN++; + } + catch (Exception ex) { - DO = master.ReadCoils(slaveId, 02255, DO_L); //读取线圈 - RW = master.ReadHoldingRegisters(slaveId, 06000, RW_L); //读取寄存器 - master.WriteMultipleCoils(slaveId, 02000, DQ); //写入线圈 - master.WriteMultipleRegisters(slaveId, 07000, DW); //写入寄存器 - }//SCMODBUSRTU - LINK_OK = true; - ERRinf.ERRinf_d(errTabler, "ERR001"); - LINK_RUN++; - } - catch (Exception ex) - { - LINK_OK = false; - LINK_ERR++; - ERRinf.ERRinf_w(errTabler, "PLC" + Properties.Resources.Connectionlost + "(" + ex.Message + ")", "ERR001"); - } - finally - { - Port_link(); + LINK_OK = false; + LINK_ERR++; + ERRinf.ERRinf_w(errTabler, "PLC" + Properties.Resources.Connectionlost + "(" + ex.Message + ")", "ERR001"); + } + finally + { + // Port_link(); + } } }); } @@ -3702,6 +3718,10 @@ namespace DyeingComputer.ViewModel , e.Row.Field("ID") , e.Row.Field("PLC")); } + if (MD < 4000 && MD > 3000) + { + _logQueue.Enqueue(new QueueS {ID = MD - 3001 ,D_DAT= e.Row.Field("DIO") }); + } } catch (Exception) { } } @@ -3981,6 +4001,11 @@ namespace DyeingComputer.ViewModel public string PLC { get; set; } public string type { get; set; } } + public class QueueS + { + public int ID { get; set; } + public bool D_DAT { get; set; } + } public class TechnologicalProcess { public int RUN { get; set; } @@ -4009,6 +4034,7 @@ namespace DyeingComputer.ViewModel public string DYELOT { get; set; } public string Remark { get; set; } } + public static Queue _logQueue = new Queue(); public ObservableCollection ToObservableCollection(DataTable dt) where T : class, new() { Type t = typeof(T);