You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
344 lines
12 KiB
344 lines
12 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommonServiceLocator;
|
|
using System.Windows.Threading;
|
|
using static DyeingComputer.UserClass.SqliteHelper;
|
|
using System.Data;
|
|
using System.Collections.ObjectModel;
|
|
using DyeingComputer.View;
|
|
using System.Windows;
|
|
using DyeingComputer.UserClass;
|
|
using System.Reflection;
|
|
using System.Windows.Documents;
|
|
using System.Runtime.CompilerServices;
|
|
using NModbus;
|
|
using NModbus.Serial;
|
|
using System.IO.Ports;
|
|
using OpenTK.Graphics.ES11;
|
|
using DyeingComputer.Properties;
|
|
|
|
|
|
namespace DyeingComputer.ViewModel
|
|
{
|
|
/// <summary>
|
|
/// 变量传递至ui
|
|
/// </summary>
|
|
public class ViewModelBase : INotifyPropertyChanged
|
|
{
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
protected virtual void OnPropertyChanged(string propertyName)
|
|
{
|
|
if (this.PropertyChanged != null)
|
|
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
|
|
public void RaisePropertyChanged(string propertyName)
|
|
{
|
|
if (propertyName != null)
|
|
{
|
|
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public class MainWindowViewModel : ViewModelBase
|
|
{
|
|
public MainWindowViewModel()
|
|
{
|
|
WorkNumder = "----------";
|
|
CountDown(); //启动循环任务
|
|
SQL_data(); //读数据库
|
|
UPort(); //启动串口
|
|
|
|
|
|
}
|
|
|
|
public string work_Temp; //显示温度
|
|
public string Work_Temp //通知UI控件参数改变
|
|
{
|
|
get { return work_Temp; }
|
|
set { work_Temp = value; OnPropertyChanged("Work_Temp"); }
|
|
}
|
|
|
|
public string work_Numder; //显示工单号
|
|
public string Work_Numder //通知UI控件参数改变
|
|
{
|
|
get { return work_Numder; }
|
|
set { work_Numder = value; OnPropertyChanged("Work_Numder"); }
|
|
}
|
|
|
|
public string status_Str; //显示状态
|
|
public string Status_Str //通知UI控件参数改变
|
|
{
|
|
get { return status_Str; }
|
|
set { status_Str = value; OnPropertyChanged("Status_Str"); }
|
|
}
|
|
|
|
public string sys_Time; //显示系统时间
|
|
public string Sys_Time //通知UI控件参数改变
|
|
{
|
|
get { return sys_Time; }
|
|
set { sys_Time = value; OnPropertyChanged("Sys_Time"); }
|
|
}
|
|
|
|
public void CountDown()
|
|
{
|
|
DispatcherTimer timer1s = new DispatcherTimer//初始化循环,每1秒调用一次Tick
|
|
{
|
|
Interval = TimeSpan.FromSeconds(1)//秒
|
|
};
|
|
timer1s.Tick += Tick_Event_1S;
|
|
timer1s.Start();
|
|
|
|
DispatcherTimer timer5s = new DispatcherTimer//初始化循环,每1秒调用一次Tick
|
|
{
|
|
Interval = TimeSpan.FromSeconds(5)//秒
|
|
};
|
|
timer5s.Tick += Tick_Event_5S;
|
|
timer5s.Start();
|
|
|
|
//设置定时器
|
|
DispatcherTimer disTimer = new DispatcherTimer
|
|
{
|
|
Interval = TimeSpan.FromMilliseconds(100) //毫秒
|
|
};
|
|
disTimer.Tick += new EventHandler(DisTimer_100MS);//每一秒执行的方法
|
|
disTimer.Start();//计时开始
|
|
}
|
|
|
|
public static object WorkNumder;
|
|
void Tick_Event_1S(object sender, EventArgs e)//Tick_Event周期执行事件
|
|
{
|
|
Sys_Time = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
|
|
Work_Temp = "130" + " ℃";
|
|
Work_Numder = WorkNumder.ToString();
|
|
Status_Str = "qweqeqwqqfwfqef";
|
|
|
|
if (LINK_OK) Modbus_link();
|
|
IO_data();
|
|
}
|
|
void Tick_Event_5S(object sender, EventArgs e)//Tick_Event周期执行事件
|
|
{
|
|
if (!LINK_OK) Modbus_link();
|
|
|
|
}
|
|
void DisTimer_100MS(object sender, EventArgs e)//Tick_Event周期执行事件
|
|
{
|
|
|
|
}
|
|
|
|
public static SerialPort port = new SerialPort();//创建串口
|
|
static ModbusFactory factory = new ModbusFactory();
|
|
|
|
// Create Modbus Master
|
|
static IModbusMaster master = factory.CreateRtuMaster(port);
|
|
|
|
void UPort()
|
|
{
|
|
try
|
|
{
|
|
port.PortName = "COM5";
|
|
port.BaudRate = 9600;//配置
|
|
port.DataBits = 8;
|
|
port.Parity = Parity.None;
|
|
port.StopBits = StopBits.One;
|
|
port.ReadTimeout = 100;
|
|
port.WriteTimeout = 100;
|
|
port.Open();//打开串口
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogGing.LogGingDATA("800_SerialPort_Not");
|
|
LogGing.LogGingDATA(e.Message);
|
|
}
|
|
}
|
|
|
|
bool LINK_OK = false;
|
|
public static async void Modbus_link()
|
|
{
|
|
await Task.Run(() =>
|
|
{
|
|
byte slaveId = 1;
|
|
ushort startAddress = 100;
|
|
ushort[] registers = new ushort[] { 1, 2, 3 };
|
|
|
|
master.Transport.ReadTimeout = 500;
|
|
master.Transport.WriteTimeout = 500;
|
|
|
|
//master.Transport.Retries = 100;
|
|
// write three registers
|
|
try
|
|
{
|
|
master.WriteMultipleRegisters(slaveId, startAddress, registers);
|
|
}
|
|
catch (Exception ex) { }
|
|
});
|
|
}
|
|
|
|
private SQLiteHelper SQLiteHelpers = null; //定义数据库
|
|
private readonly string DBAddress = Environment.CurrentDirectory + "\\DataBase\\800COMPUTER.db"; //数据库路径
|
|
DataTable dt_d = new DataTable("DIO");
|
|
DataTable dt_a = new DataTable("AIO");
|
|
DataTable dt_m = new DataTable("M");
|
|
|
|
void SQL_data()//获得io表
|
|
{
|
|
SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径
|
|
SQLiteHelpers.Open(); //打开数据库
|
|
dt_d = SQLiteHelpers.ExecuteDataSet("select * from IOName where type='DI' OR type='DO'", null).Tables[0]; //读取表写入缓存
|
|
dt_a = SQLiteHelpers.ExecuteDataSet("select * from IOName where type='AI' OR type='AO'", null).Tables[0];
|
|
dt_m = SQLiteHelpers.ExecuteDataSet("select * from IOName where type='M'", null).Tables[0];
|
|
SQLiteHelpers.Close();
|
|
}
|
|
|
|
|
|
|
|
async void IO_data()//刷新
|
|
{
|
|
await Task.Run(() =>
|
|
{
|
|
SYSData_A = ToObservableCollection<DATA_A>(dt_a);
|
|
SYSData_D = ToObservableCollection<DATA_D>(dt_d);
|
|
SYSData_M = ToObservableCollection<DATA_M>(dt_m);
|
|
});
|
|
}
|
|
|
|
|
|
void updata_dtm(string key,int Value)//更新M寄存器
|
|
{
|
|
DataRow[] arrRows = dt_m.Select("ID="+key);
|
|
int index = dt_m.Rows.IndexOf(arrRows[0]);
|
|
DataRow drEmployee = dt_m.Rows[index];
|
|
drEmployee.BeginEdit();
|
|
drEmployee["Value"] = Value;
|
|
drEmployee.EndEdit();
|
|
}
|
|
void updata_dtd(string key, bool Value)//更新D寄存器
|
|
{
|
|
DataRow[] arrRows = dt_d.Select("ID=" + key);
|
|
int index = dt_d.Rows.IndexOf(arrRows[0]);
|
|
DataRow drEmployee = dt_d.Rows[index];
|
|
drEmployee.BeginEdit();
|
|
drEmployee["DIO"] = Value;
|
|
drEmployee.EndEdit();
|
|
}
|
|
void updata_dta(string key, int Value)//更新A寄存器
|
|
{
|
|
DataRow[] arrRows = dt_a.Select("ID=" + key);
|
|
int index = dt_a.Rows.IndexOf(arrRows[0]);
|
|
DataRow drEmployee = dt_a.Rows[index];
|
|
drEmployee.BeginEdit();
|
|
drEmployee["AIO"] = Value;
|
|
drEmployee.EndEdit();
|
|
}
|
|
|
|
ObservableCollection<DATA_A> sysData_A = new ObservableCollection<DATA_A>();
|
|
ObservableCollection<DATA_D> sysData_D = new ObservableCollection<DATA_D>();
|
|
ObservableCollection<DATA_M> sysData_M = new ObservableCollection<DATA_M>();
|
|
public ObservableCollection<DATA_A> SYSData_A
|
|
{
|
|
|
|
get { return sysData_A; }
|
|
set
|
|
{
|
|
sysData_A = value;
|
|
RaisePropertyChanged("SYSData_A");
|
|
}
|
|
}
|
|
public ObservableCollection<DATA_D> SYSData_D
|
|
{
|
|
|
|
get { return sysData_D; }
|
|
set
|
|
{
|
|
sysData_D = value;
|
|
RaisePropertyChanged("SYSData_D");
|
|
}
|
|
}
|
|
public ObservableCollection<DATA_M> SYSData_M
|
|
{
|
|
|
|
get { return sysData_M; }
|
|
set
|
|
{
|
|
sysData_M = value;
|
|
RaisePropertyChanged("SYSData_M");
|
|
}
|
|
}
|
|
public class DATA_A
|
|
{
|
|
public string IOName { get; set; }
|
|
public int AIO { get; set; }
|
|
public string ID { get; set; }
|
|
public string type { get; set; }
|
|
}
|
|
public class DATA_D
|
|
{
|
|
public string IOName { get; set; }
|
|
public bool DIO { get; set; }
|
|
public string ID { get; set; }
|
|
public string type { get; set; }
|
|
}
|
|
public class DATA_M
|
|
{
|
|
public string IOName { get; set; }
|
|
public int Value { get; set; }
|
|
public String ID { get; set; }
|
|
}
|
|
public ObservableCollection<T> ToObservableCollection<T>(DataTable dt) where T : class, new()
|
|
{
|
|
Type t = typeof(T);
|
|
PropertyInfo[] propertys = t.GetProperties();
|
|
ObservableCollection<T> lst = new ObservableCollection<T>();
|
|
string typeName = string.Empty;
|
|
foreach (DataRow dr in dt.Rows)
|
|
{
|
|
T entity = new T();
|
|
foreach (PropertyInfo pi in propertys)
|
|
{
|
|
typeName = pi.Name;
|
|
if (dt.Columns.Contains(typeName))
|
|
{
|
|
if (!pi.CanWrite) continue;
|
|
object value = dr[typeName];
|
|
if (value == DBNull.Value) continue;
|
|
if (pi.PropertyType == typeof(string))
|
|
{
|
|
pi.SetValue(entity, value.ToString(), null);
|
|
}
|
|
else if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(int?))
|
|
{
|
|
pi.SetValue(entity, int.Parse(value.ToString()), null);
|
|
}
|
|
else if (pi.PropertyType == typeof(DateTime?) || pi.PropertyType == typeof(DateTime))
|
|
{
|
|
pi.SetValue(entity, DateTime.Parse(value.ToString()), null);
|
|
}
|
|
else if (pi.PropertyType == typeof(float))
|
|
{
|
|
pi.SetValue(entity, float.Parse(value.ToString()), null);
|
|
}
|
|
else if (pi.PropertyType == typeof(double))
|
|
{
|
|
pi.SetValue(entity, double.Parse(value.ToString()), null);
|
|
}
|
|
else
|
|
{
|
|
pi.SetValue(entity, value, null);
|
|
}
|
|
}
|
|
}
|
|
lst.Add(entity);
|
|
}
|
|
return lst;
|
|
}
|
|
|
|
}
|
|
}
|