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; using Microsoft.Win32; using static DyeingComputer.Windows.ViewStep; using System.Windows.Media; using System.Runtime.Remoting.Channels; namespace DyeingComputer.ViewModel { /// /// 变量传递至ui /// public class ViewModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } public void RaisePropertyChanged(string propertyName) { if (PropertyChanged != null) { if (propertyName != null) { PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } } } public class MainWindowViewModel : ViewModelBase { private SQLiteHelper SQLiteHelpers = null; //定义数据库 private readonly string DBAddress = Environment.CurrentDirectory + "\\DataBase\\800COMPUTER.db"; //数据库路径 private readonly string ChartAdress = Environment.CurrentDirectory + "\\DataBase\\Chart.db"; //数据库路径 private IniFile.IniFiles Configini = new IniFile.IniFiles(Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "DyeingComputer.ini"); private PID pid = new PID(); public static DataTable RUN_DATATABLE = new DataTable();//缓存工作表 public static int RUN_STEPID;//当前步骤 public MainWindowViewModel() { Sys_workSTEPID = -1; WorkNumder = "----------"; SYS_SET(); //crbl SQL_data(); //读数据库 UPort(); //启动串口 CountDown(); //启动循环任务 } private static int MT05;//主缸排水延时 private static int MT06;//主缸排水延时 private int MT12; private static double MT16;//流量计数系数 private int MT18;//主缸管数 private static double MT33;//主缸温度偏差 private int MT38;//高水位 private int MT39;//中水位 private int MT40;//低水位 private int MT41;//安全水位 private static int MU01;//呼叫操作员 private int S16;//机型 private int SM01;//副缸 private int SM02;//副缸 private int SM03;//副缸 private double MS01;//1缸温度偏差 private double MS02;//2缸温度偏差 private double MS03;//3缸温度偏差 void SYS_SET() { SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径 SQLiteHelpers.Open(); //打开数据库 dt_con = SQLiteHelpers.ExecuteDataSet("select * from Parameters order by ParameterID desc", null).Tables[0]; //读取表写入缓存 dt_sys = SQLiteHelpers.ExecuteDataSet("select * from System order by ParameterID desc", null).Tables[0]; //读取表写入缓存 SQLiteHelpers.Close(); pid.Kp = Convert.ToDouble(Selet_con("MT21")); //读取pid pid.Ki = Convert.ToDouble(Selet_con("MT22")); pid.Kd = Convert.ToDouble(Selet_con("MT23")); pid.N = Convert.ToDouble(Selet_con("MT24")); pid.OutputUpperLimit = Convert.ToDouble(Selet_con("MT25")); pid.OutputLowerLimit = Convert.ToDouble(Selet_con("MT26")); MT05 = Convert.ToInt16(Selet_con("MT05"));//主缸排水延时 MT06 = Convert.ToInt16(Selet_con("MT06"));//动力排水延时 MT12 = Convert.ToInt16(Selet_con("MT12"));// MT16 = Convert.ToDouble(Selet_con("MT16"));//流量计数系数 MT18 = Convert.ToInt16(Selet_con("MT18"));// MT33 = Convert.ToDouble(Selet_con("MT33"));//温度偏差参数 MT38 = Convert.ToInt16(Selet_con("MT38"));// MT39 = Convert.ToInt16(Selet_con("MT39"));// MT40 = Convert.ToInt16(Selet_con("MT40"));// MT41 = Convert.ToInt16(Selet_con("MT41"));// MU01 = Convert.ToInt16(Selet_con("MU01"));//呼叫操作员 MS01 = Convert.ToDouble(Selet_con("MS01")); MS02 = Convert.ToDouble(Selet_con("MS02")); MS03 = Convert.ToDouble(Selet_con("MS03")); S16 = Convert.ToInt16(Selet_sys("S16"));//工作信息 SM01 = Convert.ToInt16(Selet_sys("SM01"));//FG SM02 = Convert.ToInt16(Selet_sys("SM02"));//FG SM03 = Convert.ToInt16(Selet_sys("SM03"));//FG } DataTable dt_con = new DataTable(); DataTable dt_sys = new DataTable(); public string Selet_con(string key)//配置缓存 { try { DataRow[] arrRows = dt_con.Select("ParameterID ='" + key + "'"); string index = arrRows[0]["Value"].ToString(); return index; } catch (Exception ex) { LogGing.LogGingDATA(ex.ToString()); return "0"; } } public string Selet_sys(string key)//配置缓存 { try { DataRow[] arrRows = dt_sys.Select("ParameterID ='" + key + "'"); string index = arrRows[0]["Value"].ToString(); return index; } catch (Exception ex) { LogGing.LogGingDATA(ex.ToString()); return "0"; } } 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 int sys_workSTEPID; // public int Sys_workSTEPID //通知UI控件参数改变 { get { return sys_workSTEPID; } set { sys_workSTEPID = value; OnPropertyChanged("Sys_workSTEPID"); } } public string sys_log; // public string Sys_log //通知UI控件参数改变 { get { return sys_log; } set { sys_log = value; OnPropertyChanged("Sys_log"); } } public static int ViewID = 0;//页面id public static object Name_err = ""; public static object WorkNumder; public static object Process_step; public static Int32 DIDETime; //空闲计时器 bool ERR_JOG;//故障状态 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(500) //毫秒 }; disTimer.Tick += new EventHandler(DisTimer_500MS);//每一秒执行的方法 disTimer.Start();//计时开始 }//时间周期初始化 void Tick_Event_1S(object sender, EventArgs e)//Tick_Event周期执行事件1S { Sys_Time = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"); Work_Temp = Selet_dtm("1010") + " ℃"; Work_Numder = WorkNumder.ToString(); if (!SETP_runtime) DIDETime++; STEP_RUN(); LOG_view(); IO_view(); if (!string.IsNullOrEmpty(Name_err.ToString())) { Status_Str = Name_err.ToString(); if (Alert_red) { Alert_red = false; Alert_bell = false; }//关报警 } else { Alert_red = !Alert_red; Alert_bell = !Alert_bell; } } void Tick_Event_5S(object sender, EventArgs e)//Tick_Event周期执行事件5S { if (!LINK_OK) Modbus_link(); Chart();//写入记录 if (string.IsNullOrEmpty(Name_err.ToString()) && ((Selet_dtm("1010") < 10) || (Selet_dtm("1010") > 160))) { Name_err = Resources.Temperature + Resources.Sensor + Resources.Malfunction; } //温度故障提示 } void DisTimer_500MS(object sender, EventArgs e)//Tick_Event周期执行事件500MS { if (LINK_OK) Modbus_link(); if (LINK_OK) IO_data(); if (!string.IsNullOrEmpty(Name_err.ToString()) && (!ERR_JOG)) //有故障信息,无故障保持 { ERR_JOG = true; LogGing.LogGingDATA(Name_err.ToString());//输出故障信息 } if (string.IsNullOrEmpty(Name_err.ToString()) && (ERR_JOG)) //无故障信息,有故障保持 { ERR_JOG = false; LogGing.LogGingDATA(Name_err.ToString() + "=>" + Resources.FailureRecovery);//解除故障信息 } } void Chart() { Dictionary Chart_new = new Dictionary();//缓存函数 Chart_new.Add("DYELOT", work_Numder); Chart_new.Add("Time", sys_Time); Chart_new.Add("MST", string.Format(" {0:###.#}", TEMP_co)); Chart_new.Add("MTT", Selet_dtm("1010")); Chart_new.Add("MTL", Selet_dtm("1015")); Chart_new.Add("MTH", Selet_dtm("1009")); Chart_new.Add("STTA", Selet_dtm("1012")); Chart_new.Add("STLA", Selet_dtm("1017")); Chart_new.Add("STTB", Selet_dtm("1013")); Chart_new.Add("STLB", Selet_dtm("1018")); Chart_new.Add("STTC", Selet_dtm("1014")); Chart_new.Add("STLC", Selet_dtm("1019")); SQLiteHelpers = new SQLiteHelper(ChartAdress); //数据库连接路径 SQLiteHelpers.Open(); //打开数据库 SQLiteHelpers.InsertData("Chart", Chart_new);// 执行插入 SQLiteHelpers.Close(); }//历史记录 public static int WORK_RUN = 0; //运行状态0停止1暂停2运行 private static string STEP_ID = "0"; private static double STEP_P1 = 0; private static double STEP_P2 = 0; private static double STEP_P3 = 0; private static double STEP_P4 = 0; private static double STEP_P5 = 0; private static bool SETP_runtime = false; //步骤开始 private static bool STEP_finish = false; //步骤结束 private static int STEP_TIME = 0; //步骤计时S private static int THL_mode = 0; //温控模式 0保温 1加热 2降 private static double TEMP_co = 0; //计算温度 private static double TEMP_tar = 0.5; //目标温度 private bool Alert_yellow = false; //警报黄灯 private bool Alert_red = false; //警报红灯 private bool Alert_bell = false; //警报铃声 public static void STEP_START(string ID, double P1, double P2, double P3, double P4, double P5) //启动模块 { STEP_ID = ID; switch (ID) { case "001": STEP_P1 = Convert.ToDouble(P1); STEP_P2 = Convert.ToDouble(P2); STEP_P3 = Convert.ToDouble(P3); TEMP_tar = STEP_P1; double t_t = Convert.ToDouble(Selet_dtm("1010")); if (STEP_P1 > t_t) { THL_mode = 1; } else if (STEP_P1 < t_t) { THL_mode = 2; } TEMP_co = t_t; Updata_dtm("1003", STEP_P2 / 600); STEP_TIME = Convert.ToInt16(STEP_P3) * 60; WORK_RUN = 2; SETP_runtime = true; STEP_finish = false; break;//功能模块001温度控制开始 case "007": STEP_P1 = Convert.ToDouble(P1); STEP_P2 = Convert.ToDouble(P2); WORK_RUN = 2; SETP_runtime = true; STEP_finish = false; break; case "008": STEP_P1 = Convert.ToDouble(P1); STEP_P2 = Convert.ToDouble(P2); WORK_RUN = 2; SETP_runtime = true; STEP_finish = false; break; case "013": break; case "015": break; case "017": break; case "020": STEP_P1 = Convert.ToDouble(P1); STEP_P2 = Convert.ToDouble(P2); STEP_TIME = Convert.ToInt16(MT05); //排水倒计时 WORK_RUN = 2; SETP_runtime = true; STEP_finish = false; break; case "022": STEP_P1 = Convert.ToDouble(P1); STEP_P2 = Convert.ToDouble(P2); STEP_TIME = Convert.ToInt16(MT06); //动力排水倒计时 WORK_RUN = 2; SETP_runtime = true; STEP_finish = false; break; case "031": STEP_TIME = Convert.ToInt16(P1) * 60; WORK_RUN = 2; SETP_runtime = true; STEP_finish = false; break; case "035": STEP_TIME = Convert.ToInt32(MU01); WORK_RUN = 2; SETP_runtime = true; STEP_finish = false; break; case "036": STEP_TIME = Convert.ToInt32(MU01); WORK_RUN = 2; SETP_runtime = true; STEP_finish = false; break; case "039": STEP_TIME = Convert.ToInt32(MU01); WORK_RUN = 2; SETP_runtime = true; STEP_finish = false; break; case "040": STEP_TIME = Convert.ToInt32(MU01); WORK_RUN = 2; SETP_runtime = true; STEP_finish = false; break; case "041": WORK_RUN = 2; SETP_runtime = false; STEP_finish = false; break; case "049": STEP_TIME = Convert.ToInt32(MU01); WORK_RUN = 2; SETP_runtime = true; STEP_finish = false; break; case "050": break;//主缸加压 case "051": break;//主缸减压 case "054": STEP_P1 = Convert.ToDouble(P1); STEP_P2 = Convert.ToDouble(P2); STEP_P3 = Convert.ToDouble(P3); WORK_RUN = 2; SETP_runtime = true; STEP_finish = false; break;//主泵/风机速度 case "055": STEP_TIME = 10; WORK_RUN = 2; SETP_runtime = true; STEP_finish = false; break;//主泵启动 case "056": WORK_RUN = 2; SETP_runtime = true; STEP_finish = false; break;//主泵停止 case "064": break;//药缸备药 case "065": break;//药缸加药 case "066": break;//呼叫输送 case "067": break;//呼叫输送 case "090": break;//快速加酸 case "091": break;//PH温度控制 case "093": break;//PH检测 case "094": break;//PH控制 default: break; } } TimeSpan ts = new TimeSpan(00,00,01);//1秒间隔 private int temp_time10s = 10;//开关周期时间 void STEP_RUN() { if (WORK_RUN == 2) { if (SETP_runtime && (!STEP_finish))// { switch (STEP_ID) { case "001": int TIME_H; int TIME_M; int TIME_S; double TO = 0; double T = Selet_dtm("1010"); //实际温度 if (THL_mode == 0) //保温 { if (STEP_TIME == 0) { STEP_finish = true; } else { STEP_TIME--; } TO = pid.PID_iterate(STEP_P1, T, ts); if (TO < 0) TO = 0; TIME_H = STEP_TIME / 3600; TIME_M = (STEP_TIME - TIME_H * 3600) / 60; TIME_S = STEP_TIME - TIME_H * 3600 - TIME_M * 60; if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.Heat_insulating + ":" + Resources.Time + string.Format(" {0:D2}", TIME_H) + ":" + string.Format(" {0:D2}", TIME_M) + ":" + string.Format(" {0:D2}", TIME_S); } else if (THL_mode == 1)//升温 { if (STEP_P1 > TEMP_co) TEMP_co = TEMP_co + (STEP_P2 / 600); TO = pid.PID_iterate(TEMP_co, T, ts); if (TO < 0) TO = 0; if (STEP_P1 < T) { THL_mode = 0; TIME_M = 0; } else { TIME_M = Convert.ToInt16((STEP_P1 - T) / (STEP_P2 / 10)); } if (string.IsNullOrEmpty(Name_err.ToString())) { Status_Str = Resources.Heats + ":" + Resources.Target + Resources.Temperature + string.Format(" {0:###.#}", STEP_P1) + "°C"; if ((TEMP_co - T) > 5) { Status_Str = Status_Str + "[" + Resources.Heats + Resources.LnsufficientPower + "]"; } else { Status_Str = Status_Str + Resources.Expected + string.Format(" {0:D2}", TIME_M) + Resources.Min; } } } else if (THL_mode == 2)//降温 { if (STEP_P1 < TEMP_co) TEMP_co = TEMP_co - (STEP_P2 / 600); TO = pid.PID_iterate(TEMP_co, T, ts); if (TO > 0) TO = 0; TO = Math.Abs(TO); if (STEP_P1 > T) { THL_mode = 0; TIME_M = 0; } else { TIME_M = Convert.ToInt16((T - STEP_P1) / (STEP_P2 / 10)); } if (string.IsNullOrEmpty(Name_err.ToString())) { Status_Str = Resources.Cooling + ":" + Resources.Target + Resources.Temperature + string.Format(" {0:###.#}", STEP_P1) + "°C"; if ((T - TEMP_co) > 5) { Status_Str = Status_Str + "[" + Resources.Cooling + Resources.LnsufficientPower + "]"; } else { Status_Str = Status_Str + Resources.Expected + string.Format(" {0:D2}", TIME_M) + Resources.Min; } } } Updata_dtm("1004", TO); Updata_dtm("1005", TEMP_co); if (MT12 == 1)//输出类型0开关1模拟 { if (THL_mode == 2) { Updata_dta("5002", Convert.ToInt16(TO)); } else { Updata_dta("5001", Convert.ToInt16(TO)); } } else { if (temp_time10s <= 0) { temp_time10s = 10; } else { temp_time10s--; } if (THL_mode == 2) { if ((temp_time10s <= Convert.ToInt16(TO)/100)) { Updata_dtd("3010", true); } else { Updata_dtd("3010", false); }//降温 } else { if ((temp_time10s <= Convert.ToInt16(TO)/100)) { Updata_dtd("3009", true); } else { Updata_dtd("3009", false); }//升温 } } break;//功能模块001温度控制 case "007": if (STEP_P1.ToString() == "1") { Updata_dtd("3012", true); Updata_dtd("3013", false); Updata_dtd("3014", false); Updata_dtd("3015", false); if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.AddWater + ":" + Resources.AddWater + "1"; } else if (STEP_P1.ToString() == "2") { Updata_dtd("3012", false); Updata_dtd("3013", true); Updata_dtd("3014", false); Updata_dtd("3015", false); if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.AddWater + ":" + Resources.AddWater + "2"; } else if (STEP_P1.ToString() == "3") { Updata_dtd("3012", false); Updata_dtd("3013", false); Updata_dtd("3014", true); Updata_dtd("3015", false); if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.AddWater + ":" + Resources.AddWater + "3"; } else if (STEP_P1.ToString() == "4") { Updata_dtd("3012", false); Updata_dtd("3013", false); Updata_dtd("3014", false); Updata_dtd("3015", true); if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.AddWater + ":" + Resources.AddWater + "4"; } if (Selet_dtm("1015") >= STEP_P2) //到达水位 { Updata_dtd("3012", false); Updata_dtd("3013", false); Updata_dtd("3014", false); Updata_dtd("3015", false); if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.AddWater + ":" + Resources.Finish; STEP_finish = true; } break;//液位入水 case "008": if (STEP_P1.ToString() == "1") { Updata_dtd("3012", true); Updata_dtd("3013", false); Updata_dtd("3014", false); Updata_dtd("3015", false); if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.AddWater + ":" + Resources.AddWater + "1"; } else if (STEP_P1.ToString() == "2") { Updata_dtd("3012", false); Updata_dtd("3013", true); Updata_dtd("3014", false); Updata_dtd("3015", false); if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.AddWater + ":" + Resources.AddWater + "2"; } else if (STEP_P1.ToString() == "3") { Updata_dtd("3012", false); Updata_dtd("3013", false); Updata_dtd("3014", true); Updata_dtd("3015", false); if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.AddWater + ":" + Resources.AddWater + "3"; } else if (STEP_P1.ToString() == "4") { Updata_dtd("3012", false); Updata_dtd("3013", false); Updata_dtd("3014", false); Updata_dtd("3015", true); if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.AddWater + ":" + Resources.AddWater + "4"; } if (Selet_dtm("1016") >= STEP_P2) //到达水量 { Updata_dtd("3012", false); Updata_dtd("3013", false); Updata_dtd("3014", false); Updata_dtd("3015", false); if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.AddWater + ":" + Resources.Finish; STEP_finish = true; } break;//流量入水 case "013": break; case "015": break; case "017": break; case "020": if (STEP_P1.ToString() == "1") { Updata_dtd("3016", true); Updata_dtd("3017", false); Updata_dtd("3018", false); if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.Drainage + ":" + Resources.Drainage + "1"; } else if (STEP_P1.ToString() == "2") { Updata_dtd("3016", false); Updata_dtd("3017", true); Updata_dtd("3018", false); if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.Drainage + ":" + Resources.Drainage + "2"; } else if (STEP_P1.ToString() == "3") { Updata_dtd("3016", false); Updata_dtd("3017", false); Updata_dtd("3018", true); if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.Drainage + ":" + Resources.Drainage + "3"; } if (Selet_dtm("1015") <= STEP_P2) //排水状态完成 { if (STEP_TIME == 0)//排水延时 { Updata_dtd("3016", false); Updata_dtd("3017", false); Updata_dtd("3018", false); if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.Drainage + ":" + Resources.Finish; STEP_finish = true; } else { STEP_TIME--; //排水延时计时 if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.Drainage + ":" + Resources.Time + STEP_TIME; } } break;//排水 case "022": if (STEP_P1.ToString() == "1") { Updata_dtd("3016", true); Updata_dtd("3017", false); Updata_dtd("3018", false); if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.Drainage + ":" + Resources.Drainage + "1"; } else if (STEP_P1.ToString() == "2") { Updata_dtd("3016", false); Updata_dtd("3017", true); Updata_dtd("3018", false); if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.Drainage + ":" + Resources.Drainage + "2"; } else if (STEP_P1.ToString() == "3") { Updata_dtd("3016", false); Updata_dtd("3017", false); Updata_dtd("3018", true); if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.Drainage + ":" + Resources.Drainage + "3"; } Updata_dtd("3019", true);//开排水泵 if (Selet_dtm("1015") <= STEP_P2) //排水状态完成 { if (STEP_TIME == 0)//排水延时 { Updata_dtd("3016", false); Updata_dtd("3017", false); Updata_dtd("3018", false); Updata_dtd("3019", false);//开排水泵 if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.Drainage + ":" + Resources.Finish; STEP_finish = true; } else { STEP_TIME--; //排水延时计时 if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.Drainage + ":" + Resources.Time + STEP_TIME; } } break;//动力排水 case "031": if (STEP_TIME == 0) { STEP_finish = true; } else { STEP_TIME--; TIME_M = STEP_TIME / 60; TIME_S = STEP_TIME - TIME_M * 60; if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.RunningTime + ":" + Resources.Time + string.Format(" {0:D3}", TIME_M) + ":" + string.Format(" {0:D2}", TIME_S); } break;//运转时间 case "035": if (Selet_dtd("2003")) { STEP_finish = true; Alert_yellow = false; } else { Alert_yellow = true; STEP_TIME--; if (STEP_TIME == 0) { Alert_bell = false; } else { Alert_bell = true; } } if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.ClothIntake; break;//准备入布 case "036": if (Selet_dtd("2003")) { STEP_finish = true; Alert_yellow = false; } else { Alert_yellow = true; STEP_TIME--; if (STEP_TIME == 0) { Alert_bell = false; } else { Alert_bell = true; } } if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.OutTheCloth; break;//准备出布 case "039": if (Selet_dtd("2003")) { STEP_finish = true; Alert_yellow = false; } else { Alert_yellow = true; STEP_TIME--; if (STEP_TIME == 0) { Alert_bell = false; } else { Alert_bell = true; } } if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.CallWorkers; break;//呼叫人工 case "040": if (Selet_dtd("2003")) { STEP_finish = true; Alert_yellow = false; } else { Alert_yellow = true; STEP_TIME--; if (STEP_TIME == 0) { Alert_bell = false; } else { Alert_bell = true; } } if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.Sampling; break;//取样 case "041": STEP_finish = true; if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.End; break;//结束 case "049": if (Selet_dtd("2003")) { STEP_finish = true; Alert_yellow = false; } else { Alert_yellow = true; STEP_TIME--; if (STEP_TIME == 0) { Alert_bell = false; } else { Alert_bell = true; } } if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.Await; break;//等待 case "050": break;//主缸加压 case "051": break;//主缸减压 case "054": Updata_dtm("1020", STEP_P1); Updata_dtm("1021", STEP_P2); Updata_dtm("1008", STEP_P3); STEP_finish = true; break;//主泵/风机速度 case "055": if (Selet_dtm("1015") > MT41) { Updata_dtd("3011", true);//主泵启动 if (S16 == 1) Updata_dtd("3012", true);//风机启动 STEP_TIME--; if (STEP_TIME == 0) { STEP_finish = true; } if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.PumpStart + "("+ STEP_TIME + "S)"; } else { Updata_dtd("3011", false); Updata_dtd("3012", false); if (string.IsNullOrEmpty(Name_err.ToString())) Status_Str = Resources.MasterCylinder + Resources.Low_waterlevel; } break;//主泵启动 case "056": Updata_dtd("3011", false);//主泵 if (S16 == 1) Updata_dtd("3012", false);//风机 STEP_finish = true; break;//主泵停止 case "064": break;//药缸备药 case "065": break;//药缸加药 case "066": break;//呼叫输送 case "067": break;//呼叫输送 case "090": break;//快速加酸 case "091": break;//PH温度控制 case "093": break;//PH检测 case "094": break;//PH控制 default: break; } } else { if (STEP_finish) { RUN_STEPID++; DataRow[] arrRows = RUN_DATATABLE.Select("STEP ='" + RUN_STEPID + "'"); string T_ID = arrRows[0]["StepID"].ToString(); double T_P1 = Convert.ToDouble(arrRows[0]["Parameter1"].ToString()); double T_P2 = Convert.ToDouble(arrRows[0]["Parameter2"].ToString()); double T_P3 = Convert.ToDouble(arrRows[0]["Parameter3"].ToString()); double T_P4 = Convert.ToDouble(arrRows[0]["Parameter4"].ToString()); double T_P5 = Convert.ToDouble(arrRows[0]["Parameter5"].ToString()); STEP_START(T_ID, T_P1, T_P2, T_P3, T_P4, T_P5); if (ViewID == 1) { sys_workSTEPID = RUN_STEPID; } else { sys_workSTEPID = -1; } } if (string.IsNullOrEmpty(Name_err.ToString())) { Status_Str = Resources.Await; } } } else if (WORK_RUN == 1) { SETP_runtime = false; STEP_finish = false; Status_Str = Resources.Paused; Updata_dtm("1004", 0); Updata_dtd("3009", false); Updata_dtd("3010", false); } else if (WORK_RUN == 0) { if (Status_Str == Resources.Paused) { Updata_dtm("1004", 0); Updata_dtm("1005", Selet_dtm("1010")); for (int i = 1; i <128; i++) { Updata_dtd((3000+i).ToString(), false); } }//清除输出状态 if (string.IsNullOrEmpty(Name_err.ToString())) { int TIMEH = DIDETime / 3600; int TIMEM = (DIDETime - TIMEH * 3600) / 60; int TIMES = DIDETime - TIMEH * 3600 - TIMEM * 60; Status_Str = Resources.Dide + string.Format(" {0:D4}", TIMEH) + ":" + string.Format(" {0:D2}", TIMEM) + ":" + string.Format(" {0:D2}", TIMES); } } } 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 = Configini.IniReadvalue("SYS", "COM"); port.BaudRate = 115200;//配置 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); Name_err = "800_SerialPort_Not"; } } private bool LINK_OK = false; private int LINK_RUN = 0; private int LINK_ERR = 0; bool[] DQ = new bool[63]; ushort DQ_L = 63; bool[] DO = new bool[63]; ushort DO_L = 63; //int[] MW = new int[128]; // ushort MW_L = 128; ushort[] DW = new ushort[63]; ushort DW_L = 63; ushort[] RW = new ushort[63]; ushort RW_L = 63; readonly byte slaveId = 1; private async void Modbus_link() { if (Name_err.ToString() != "800_SerialPort_Not") { await Task.Run(() => { LINK_RUN++; try { master.WriteMultipleCoils(slaveId, 00000, DQ); //写入线圈 master.WriteMultipleRegisters(slaveId, 07000, DW); //写入寄存器 DO = master.ReadCoils(slaveId, 01000, DO_L); //读取线圈 RW = master.ReadHoldingRegisters(slaveId, 06000, RW_L); //读取寄存器 LINK_OK = true; Name_err = ""; } catch (Exception ex) { LINK_OK = false; LINK_ERR++; Name_err = "PLC" + Properties.Resources.Connectionlost + "(" + ex.Message + ")"; } }); } } void LOG_view()//细节页面 { Sys_log = Resources.Target + Resources.Temperature + " : " + string.Format(" {0:###.#}", TEMP_tar) + "°C";//显示目标温度 Sys_log = Sys_log + " | " + Resources.MasterCylinder + Resources.Temperature + " : " + string.Format(" {0:###.#}", Convert.ToDouble(Selet_dtm("1010"))) + "°C";//显示实际温度 Sys_log = Sys_log + "\n" + Resources.Compute + Resources.Temperature + " : " + string.Format(" {0:###.#}", Convert.ToDouble(Selet_dtm("1005"))) + "°C";//显示计算温度 Sys_log = Sys_log + " | " + Resources.ProportionalValves + " : " + string.Format("{0:###.#}", Convert.ToDouble(Selet_dtm("1004")) / 10) + "%";//显示比例 int IO1015 = Convert.ToInt16(Selet_dtm("1015")); if (IO1015 < MT39) { Sys_log = Sys_log + "\n" + Resources.MasterCylinder + Resources.WaterLevel + " : " + string.Format("{0:D4}", IO1015) + "L =>[" + Resources.Low_waterlevel + "]";//显示水位 } else if ((IO1015 > MT39) && (IO1015 < MT38)) { Sys_log = Sys_log + "\n" + Resources.MasterCylinder + Resources.WaterLevel + " : " + string.Format("{0:D4}", IO1015) + "L =>[" + Resources.Mid_waterlevel + "]";//显示水位 } else if (IO1015 > MT38) { Sys_log = Sys_log + "\n" + Resources.MasterCylinder + Resources.WaterLevel + " : " + string.Format("{0:D4}", IO1015) + "L =>[" + Resources.High_waterlevel + "]";//显示水位 } if (Selet_dtd("3011")) //主泵状态 { Sys_log = Sys_log + "\n" + Resources.PumpSpeed + " : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1020"))) + "% => [" + Resources.PumpStart + "]";//主泵状态 }//主泵状态 else { Sys_log = Sys_log + "\n" + Resources.PumpSpeed + " : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1020"))) + "% => [" + Resources.PumpStops + "]";//主泵状态 } Sys_log = Sys_log + "\n" + Resources.MasterCylinder + Resources.Pressure + " : " + string.Format("{0:###.#}", Convert.ToDouble(Selet_dtm("1042"))) + "Bar"; Sys_log = Sys_log + " | " + Resources.Nozzle + Resources.Pressure + " : " + string.Format("{0:###.#}", Convert.ToDouble(Selet_dtm("1043"))) + "Bar"; Sys_log = Sys_log + "\n-------------------------------------------------------"; if (S16 == 0) { Sys_log = Sys_log + "\n" + Resources.ClothWheel + "M/min"; Sys_log = Sys_log + "\n" + "1 : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1022"))); if (MT18 >= 2) Sys_log = Sys_log + " | " + "2 : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1023"))); if (MT18 >= 3) Sys_log = Sys_log + " | " + "3 : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1024"))); if (MT18 >= 4) Sys_log = Sys_log + "\n" + "4 : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1025"))); if (MT18 >= 5) Sys_log = Sys_log + " | " + "5 : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1026"))); if (MT18 >= 6) Sys_log = Sys_log + " | " + "6 : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1027"))); if (MT18 >= 7) Sys_log = Sys_log + "\n" + "7 : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1028"))); if (MT18 >= 8) Sys_log = Sys_log + " | " + "8 : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1029"))); if (MT18 >= 9) Sys_log = Sys_log + " | " + "9 : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1030"))); if (MT18 >= 10) Sys_log = Sys_log + "\n" + "10 : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1031"))); Sys_log = Sys_log + "\n" + Resources.Cycletime + "Sec"; Sys_log = Sys_log + "\n" + "1 : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1032"))); if (MT18 >= 2) Sys_log = Sys_log + " | " + "2 : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1033"))); if (MT18 >= 3) Sys_log = Sys_log + " | " + "3 : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1034"))); if (MT18 >= 4) Sys_log = Sys_log + "\n" + "4 : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1035"))); if (MT18 >= 5) Sys_log = Sys_log + " | " + "5 : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1036"))); if (MT18 >= 6) Sys_log = Sys_log + " | " + "6 : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1037"))); if (MT18 >= 7) Sys_log = Sys_log + "\n" + "7 : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1038"))); if (MT18 >= 8) Sys_log = Sys_log + " | " + "8 : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1039"))); if (MT18 >= 9) Sys_log = Sys_log + " | " + "9 : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1040"))); if (MT18 >= 10) Sys_log = Sys_log + "\n" + "10 : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1041"))); }//溢流/o型 else if (S16 == 1) //气流 { Sys_log = Sys_log + "\n" + Resources.DifferentialPressure + " : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1044"))) + "%"; Sys_log = Sys_log + "\n" + Resources.Blower + " : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1021"))) + "%"; Sys_log = Sys_log + "\n" + Resources.GuidingWheel + " : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1046"))) + "M/min"; Sys_log = Sys_log + "\n" + Resources.Nozzle + Resources.Flowmeter + " : " + string.Format("{0:D3}", Convert.ToInt16(Selet_dtm("1045"))) + "L/min"; } else if (S16 == 2) //桶纱 { } Sys_log = Sys_log + "\n-------------------------------------------------------"; if (SM01 == 1) { Sys_log = Sys_log + "\n" + Resources.Medicine + Resources.Tank + "1 => [" + Resources.WaterLevel + ":" + string.Format("{0:D4}", Convert.ToInt16(Selet_dtm("1017"))) + "L][" + Resources.Temperature + ":" + string.Format(" {0:###.#}", Convert.ToInt16(Selet_dtm("1012"))) + "°C]"; Sys_log = Sys_log + "\n" + Resources.Dyelot + " : " + " => "; Sys_log = Sys_log + "\n" + Resources.WorkingStatus + " : "; } if (SM02 == 1) { Sys_log = Sys_log + "\n" + Resources.Medicine + Resources.Tank + "2 => [" + Resources.WaterLevel + ":" + string.Format("{0:D4}", Convert.ToInt16(Selet_dtm("1018"))) + "L][" + Resources.Temperature + ":" + string.Format(" {0:###.#}", Convert.ToInt16(Selet_dtm("1013"))) + "°C]"; Sys_log = Sys_log + "\n" + Resources.Dyelot + " : " + " => "; Sys_log = Sys_log + "\n" + Resources.WorkingStatus + " : "; } if (SM03 == 1) { Sys_log = Sys_log + "\n" + Resources.Medicine + Resources.Tank + "3 => [" + Resources.WaterLevel + ":" + string.Format("{0:D4}", Convert.ToInt16(Selet_dtm("1019"))) + "L][" + Resources.Temperature + ":" + string.Format(" {0:###.#}", Convert.ToInt16(Selet_dtm("1014"))) + "°C]"; Sys_log = Sys_log + "\n" + Resources.Dyelot + " : " + " => "; Sys_log = Sys_log + "\n" + Resources.WorkingStatus + " : "; } } public static DataTable dt_d = new DataTable("DIO"); public static DataTable dt_a = new DataTable("AIO"); public static 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='DO' OR type='DQ' order by ID", null).Tables[0]; //读取表写入缓存 dt_a = SQLiteHelpers.ExecuteDataSet("select * from IOName where type='AI' OR type='AO' order by ID", null).Tables[0]; dt_m = SQLiteHelpers.ExecuteDataSet("select * from IOName where type='M' order by ID", null).Tables[0]; SQLiteHelpers.Close(); //创建dt_d主建 DataColumn[] dt_dcols =new DataColumn[] { dt_d.Columns["ID"] }; dt_d.PrimaryKey = dt_dcols; //创建dt_a主建 DataColumn[] dt_acols = new DataColumn[] { dt_a.Columns["ID"] }; dt_a.PrimaryKey = dt_acols; //创建dt_m主建 DataColumn[] dt_mcols = new DataColumn[] { dt_m.Columns["ID"] }; dt_m.PrimaryKey = dt_mcols; } private async void IO_data()//刷新 { Updata_dtm("1001", LINK_RUN); Updata_dtm("1002", LINK_ERR); Updata_dtm("1010", (Convert.ToDouble(Selet_dta("4001")) + MT33)/10); Updata_dtm("1006", Selet_dta("4006")); Updata_dtm("1015", Selet_dta("4007")); Updata_dtm("1016", Selet_dta("4006") * MT16); Updata_dtm("1012", (Convert.ToDouble(Selet_dta("4003")) + MS01) / 10); Updata_dtm("1013", (Convert.ToDouble(Selet_dta("4004")) + MS02) / 10); Updata_dtm("1014", (Convert.ToDouble(Selet_dta("4005")) + MS03) / 10); await Task.Run(() => { for (ushort i = 0; i < DO_L; i++) { Updata_dtd((i + 2001).ToString(), DO[i]); } for (ushort i = 0; i < DQ_L; i++) { DQ[i] = Selet_dtd((i + 3001).ToString()); } for (ushort i = 0; i < RW_L; i++) { Updata_dta((i + 4001).ToString(), RW[i]); } for (ushort i = 0; i < DW_L; i++) { DW[i] = Convert.ToUInt16(Selet_dta((i + 5001).ToString())); } }); } private async void IO_view()//IO显示 { await Task.Run(() => { try { SYSData_M = ToObservableCollection(dt_m); } catch (Exception ex) { Name_err = "IO_SYSData_M" + "(" + ex.Message + ")"; } try { SYSData_A = ToObservableCollection(dt_a); } catch (Exception ex) { Name_err = "IO_SYSData_A" + "(" + ex.Message + ")"; } try { SYSData_D = ToObservableCollection(dt_d); } catch (Exception ex) { Name_err = "IO_SYSData_D" + "(" + ex.Message + ")"; } }); } public static double Selet_dtm(string key)//M寄存器 { try { lock (dt_m) { // DataRow[] arrRows = dt_m.Select("ID='" + key + "'"); // double index = Convert.ToDouble(arrRows[0]["Value"].ToString()); DataRow drEmployee = dt_m.Rows.Find(key); double index = drEmployee.Field("value"); return index; } } catch (Exception ex) { LogGing.LogGingDATA("SDTM:"+ex.ToString()); return -1; } } public static bool Selet_dtd(string key)//D寄存器 { try { lock (dt_d) { // DataRow[] arrRows = dt_d.Select("ID='" + key + "'"); // bool index = Convert.ToBoolean(arrRows[0]["DIO"].ToString()); DataRow drEmployee = dt_d.Rows.Find(key); bool index = drEmployee.Field("DIO"); return index; } } catch (Exception ex) { LogGing.LogGingDATA("SDTD:" + ex.ToString()); return false; } } public static int Selet_dta(string key)//A寄存器 { try { lock (dt_a) { // DataRow[] arrRows = dt_a.Select("ID='" + key + "'"); // int index = Convert.ToInt16(arrRows[0]["AIO"].ToString()); DataRow drEmployee = dt_a.Rows.Find(key); int index = drEmployee.Field("AIO"); return index; } } catch (Exception ex) { LogGing.LogGingDATA("SDTA:" + ex.ToString()); return -1; } } public static void Updata_dtm(string key,double Value)//更新M寄存器 { try { lock (dt_m) { DataRow drEmployee = dt_m.Rows.Find(key); drEmployee.BeginEdit(); drEmployee["Value"] = Value; drEmployee.EndEdit(); drEmployee.AcceptChanges(); drEmployee.ClearErrors(); } } catch (Exception ex) { LogGing.LogGingDATA("UDTM:" + ex.ToString()); } } public static void Updata_dtd(string key, bool Value)//更新D寄存器 { try { lock (dt_d)//锁 { DataRow drEmployee = dt_d.Rows.Find(key); drEmployee.BeginEdit(); drEmployee["DIO"] = Value; drEmployee.EndEdit(); drEmployee.AcceptChanges(); drEmployee.ClearErrors(); } } catch (Exception ex) { LogGing.LogGingDATA("UDTD:" + ex.ToString()); } } public static void Updata_dta(string key, int Value)//更新A寄存器 { try { lock (dt_a) { DataRow drEmployee = dt_a.Rows.Find(key); drEmployee.BeginEdit(); drEmployee["AIO"] = Value; drEmployee.EndEdit(); drEmployee.AcceptChanges(); drEmployee.ClearErrors(); } } catch (Exception ex) { LogGing.LogGingDATA("UDTA:" + ex.ToString()); } } ObservableCollection sysData_A = new ObservableCollection(); ObservableCollection sysData_D = new ObservableCollection(); ObservableCollection sysData_M = new ObservableCollection(); public ObservableCollection SYSData_A { get { return sysData_A; } set { sysData_A = value; RaisePropertyChanged("SYSData_A"); } } public ObservableCollection SYSData_D { get { return sysData_D; } set { sysData_D = value; RaisePropertyChanged("SYSData_D"); } } public ObservableCollection 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 double Value { get; set; } public string ID { get; set; } public string type { get; set; } } public ObservableCollection ToObservableCollection(DataTable dt) where T : class, new() { Type t = typeof(T); PropertyInfo[] propertys = t.GetProperties(); ObservableCollection lst = new ObservableCollection(); 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; } } }