sc 1 month ago
parent
commit
0c24c679ce
  1. 34
      ViewModel/MainWindowViewModel.cs

34
ViewModel/MainWindowViewModel.cs

@ -9,7 +9,9 @@ using Microsoft.Win32;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using NModbus; using NModbus;
using NModbus.Serial; using NModbus.Serial;
using OpenHardwareMonitor.Hardware;
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
@ -35,7 +37,6 @@ using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Threading; using System.Windows.Threading;
using TouchSocket.Sockets; using TouchSocket.Sockets;
using OpenHardwareMonitor.Hardware;
namespace DyeingComputer.ViewModel namespace DyeingComputer.ViewModel
{ {
@ -3391,21 +3392,35 @@ namespace DyeingComputer.ViewModel
if (PORTOK) if (PORTOK)
{ {
await Task.Run(() => await Task.Run(() =>
{
while (true)
{ {
try try
{ {
if (protocols_ == "MODBUS_RTU") 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); //读取线圈 DO = master.ReadCoils(slaveId, 0, DO_L); //读取线圈
RW = master.ReadHoldingRegisters(slaveId, 0, RW_L); //读取寄存器 RW = master.ReadHoldingRegisters(slaveId, 0, RW_L); //读取寄存器
master.WriteMultipleCoils(slaveId, 0, DQ); //写入线圈 DQ = master.ReadCoils(slaveId, 0, DQ_L);
//master.WriteMultipleCoils(slaveId, 0, DQ); //写入线圈
master.WriteMultipleRegisters(slaveId, 0, DW); //写入寄存器 master.WriteMultipleRegisters(slaveId, 0, DW); //写入寄存器
}//MODBUSRTU }//MODBUSRTU
else 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); //读取线圈 DO = master.ReadCoils(slaveId, 02255, DO_L); //读取线圈
RW = master.ReadHoldingRegisters(slaveId, 06000, RW_L); //读取寄存器 RW = master.ReadHoldingRegisters(slaveId, 06000, RW_L); //读取寄存器
master.WriteMultipleCoils(slaveId, 02000, DQ); //写入线圈 DQ = master.ReadCoils(slaveId, 02000, DQ_L);
// master.WriteMultipleCoils(slaveId, 02000, DQ); //写入线圈
master.WriteMultipleRegisters(slaveId, 07000, DW); //写入寄存器 master.WriteMultipleRegisters(slaveId, 07000, DW); //写入寄存器
}//SCMODBUSRTU }//SCMODBUSRTU
LINK_OK = true; LINK_OK = true;
@ -3420,7 +3435,8 @@ namespace DyeingComputer.ViewModel
} }
finally finally
{ {
Port_link(); // Port_link();
}
} }
}); });
} }
@ -3702,6 +3718,10 @@ namespace DyeingComputer.ViewModel
, e.Row.Field<string>("ID") , e.Row.Field<string>("ID")
, e.Row.Field<string>("PLC")); , e.Row.Field<string>("PLC"));
} }
if (MD < 4000 && MD > 3000)
{
_logQueue.Enqueue(new QueueS {ID = MD - 3001 ,D_DAT= e.Row.Field<bool>("DIO") });
}
} }
catch (Exception) { } catch (Exception) { }
} }
@ -3981,6 +4001,11 @@ namespace DyeingComputer.ViewModel
public string PLC { get; set; } public string PLC { get; set; }
public string type { 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 class TechnologicalProcess
{ {
public int RUN { get; set; } public int RUN { get; set; }
@ -4009,6 +4034,7 @@ namespace DyeingComputer.ViewModel
public string DYELOT { get; set; } public string DYELOT { get; set; }
public string Remark { get; set; } public string Remark { get; set; }
} }
public static Queue<QueueS> _logQueue = new Queue<QueueS>();
public ObservableCollection<T> ToObservableCollection<T>(DataTable dt) where T : class, new() public ObservableCollection<T> ToObservableCollection<T>(DataTable dt) where T : class, new()
{ {
Type t = typeof(T); Type t = typeof(T);

Loading…
Cancel
Save