|
|
@ -51,28 +51,16 @@ namespace DyeingComputer.ViewModel |
|
|
|
{ |
|
|
|
private SQLiteHelper SQLiteHelpers = null; //定义数据库
|
|
|
|
private readonly string DBAddress = Environment.CurrentDirectory + "\\DataBase\\800COMPUTER.db"; //数据库路径
|
|
|
|
private PID PID = null; |
|
|
|
private IniFile.IniFiles Configini = new IniFile.IniFiles(Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "DyeingComputer.ini"); |
|
|
|
PID pid = new PID(); |
|
|
|
|
|
|
|
public MainWindowViewModel() |
|
|
|
{ |
|
|
|
SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径
|
|
|
|
SQLiteHelpers.Open(); //打开数据库
|
|
|
|
pid.Kp = Convert.ToDouble( SQLiteHelpers.ExecuteScalar("select value from system where ParameterID = 'ST01'", null)); //读取
|
|
|
|
pid.Ki = Convert.ToDouble( SQLiteHelpers.ExecuteScalar("select value from system where ParameterID = 'ST02'", null)); |
|
|
|
pid.Kd = Convert.ToDouble( SQLiteHelpers.ExecuteScalar("select value from system where ParameterID = 'ST03'", null)); |
|
|
|
pid.N = Convert.ToDouble(SQLiteHelpers.ExecuteScalar("select value from system where ParameterID = 'ST04'", null)); |
|
|
|
pid.OutputUpperLimit = Convert.ToDouble(SQLiteHelpers.ExecuteScalar("select value from system where ParameterID = 'ST05'", null)); |
|
|
|
pid.OutputLowerLimit = Convert.ToDouble(SQLiteHelpers.ExecuteScalar("select value from system where ParameterID = 'ST06'", null)); |
|
|
|
SQLiteHelpers.Close(); |
|
|
|
|
|
|
|
WorkNumder = "----------"; |
|
|
|
SYS_SET(); //crbl
|
|
|
|
CountDown(); //启动循环任务
|
|
|
|
SQL_data(); //读数据库
|
|
|
|
UPort(); //启动串口
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
double ST11; |
|
|
@ -80,8 +68,32 @@ namespace DyeingComputer.ViewModel |
|
|
|
{ |
|
|
|
SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径
|
|
|
|
SQLiteHelpers.Open(); //打开数据库
|
|
|
|
ST11 = Convert.ToDouble(SQLiteHelpers.ExecuteScalar("select value from system where ParameterID = 'ST11'", null)); |
|
|
|
dt_sys = SQLiteHelpers.ExecuteDataSet("select * from System", null).Tables[0]; //读取表写入缓存
|
|
|
|
SQLiteHelpers.Close(); |
|
|
|
|
|
|
|
ST11 = Convert.ToDouble(Selet_sys("ST11"));//温度偏差参数
|
|
|
|
|
|
|
|
pid.Kp = Convert.ToDouble(Selet_sys("ST01")); //读取pid
|
|
|
|
pid.Ki = Convert.ToDouble(Selet_sys("ST02")); |
|
|
|
pid.Kd = Convert.ToDouble(Selet_sys("ST03")); |
|
|
|
pid.N = Convert.ToDouble(Selet_sys("ST04")); |
|
|
|
pid.OutputUpperLimit = Convert.ToDouble(Selet_sys("ST05")); |
|
|
|
pid.OutputLowerLimit = Convert.ToDouble(Selet_sys("ST06")); |
|
|
|
} |
|
|
|
DataTable dt_sys = new DataTable(); |
|
|
|
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; //显示温度
|
|
|
@ -113,7 +125,7 @@ namespace DyeingComputer.ViewModel |
|
|
|
{ |
|
|
|
DispatcherTimer timer1s = new DispatcherTimer//初始化循环,每1秒调用一次Tick
|
|
|
|
{ |
|
|
|
Interval = TimeSpan.FromSeconds(0.5)//秒
|
|
|
|
Interval = TimeSpan.FromSeconds(1)//秒
|
|
|
|
}; |
|
|
|
timer1s.Tick += Tick_Event_1S; |
|
|
|
timer1s.Start(); |
|
|
@ -136,6 +148,7 @@ namespace DyeingComputer.ViewModel |
|
|
|
|
|
|
|
public static object Name_err; |
|
|
|
public static object WorkNumder; |
|
|
|
public Int32 DIDETime; |
|
|
|
void Tick_Event_1S(object sender, EventArgs e)//Tick_Event周期执行事件1S
|
|
|
|
{ |
|
|
|
Sys_Time = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"); |
|
|
@ -144,13 +157,16 @@ namespace DyeingComputer.ViewModel |
|
|
|
// Status_Str = "qweqeqwqqfwfqef";
|
|
|
|
|
|
|
|
if (LINK_OK) Modbus_link(); |
|
|
|
if (!SETP_runtime) STEP_RUN(); |
|
|
|
|
|
|
|
DIDETime++; |
|
|
|
IO_data(); |
|
|
|
} |
|
|
|
void Tick_Event_5S(object sender, EventArgs e)//Tick_Event周期执行事件5S
|
|
|
|
{ |
|
|
|
if (!LINK_OK) Modbus_link(); |
|
|
|
if (Name_err != null) Status_Str = Name_err.ToString(); |
|
|
|
STEP_RUN(); |
|
|
|
if(SETP_runtime) STEP_RUN(); |
|
|
|
} |
|
|
|
void DisTimer_100MS(object sender, EventArgs e)//Tick_Event周期执行事件100MS
|
|
|
|
{ |
|
|
@ -165,19 +181,61 @@ namespace DyeingComputer.ViewModel |
|
|
|
double STEP_P3 = 0; |
|
|
|
double STEP_P4 = 0; |
|
|
|
double STEP_P5 = 0; |
|
|
|
bool SETP_runtime = false; |
|
|
|
|
|
|
|
void STEP_START(object sender, AddressUpdateEventArgs e) |
|
|
|
{ |
|
|
|
|
|
|
|
public void STEP_START(object sender, AddressUpdateEventArgs e) |
|
|
|
{ |
|
|
|
if (e.StepID == "001") |
|
|
|
{ |
|
|
|
updata_dtm("1003",Convert.ToDouble( e.P2)); |
|
|
|
STEP_ID = "001"; |
|
|
|
STEP_P1 = Convert.ToDouble(e.P1); |
|
|
|
STEP_P2 = Convert.ToDouble(e.P2); |
|
|
|
STEP_P3 = Convert.ToDouble(e.P3); |
|
|
|
|
|
|
|
updata_dtm("1003",Convert.ToDouble( e.P2)/12); |
|
|
|
|
|
|
|
SETP_runtime=true; |
|
|
|
//STEP_RUN();
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void STEP_RUN() |
|
|
|
{ |
|
|
|
if (SETP_runtime) |
|
|
|
{ |
|
|
|
switch (STEP_ID) |
|
|
|
{ |
|
|
|
case "001": |
|
|
|
|
|
|
|
break; |
|
|
|
case "007": |
|
|
|
|
|
|
|
break; |
|
|
|
case "008": |
|
|
|
|
|
|
|
break; |
|
|
|
case "013": |
|
|
|
|
|
|
|
break; |
|
|
|
case "015": |
|
|
|
|
|
|
|
break; |
|
|
|
case "017": |
|
|
|
|
|
|
|
break; |
|
|
|
default: break; |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
if (Name_err == null) |
|
|
|
{ |
|
|
|
int TIMEH = DIDETime / 3600; |
|
|
|
int TIMEM = (DIDETime - TIMEH * 3600) / 60; |
|
|
|
int TIMES = DIDETime - TIMEH*3600 - TIMEM*60; |
|
|
|
Status_Str = Properties.Resources.Dide + string.Format(" {0:D4}", TIMEH) +":"+ string.Format(" {0:D2}", TIMEM) + ":" + string.Format(" {0:D2}", TIMES); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -270,17 +328,11 @@ namespace DyeingComputer.ViewModel |
|
|
|
|
|
|
|
await Task.Run(() => |
|
|
|
{ |
|
|
|
lock (dt_d) |
|
|
|
{ |
|
|
|
for (ushort i = 0; i < DO_L; i++) { updata_dtd((i + 2001).ToString(), DO[i]); } |
|
|
|
//for (ushort i = 0; i < DQ_L; i++) { updata_dtd((i + 3001).ToString(), DQ[i]); }
|
|
|
|
for (ushort i = 0; i < DQ_L; i++) { DQ[i] = Selet_dtd((i + 3001).ToString()); } |
|
|
|
} |
|
|
|
lock (dt_a) |
|
|
|
{ |
|
|
|
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())); } |
|
|
|
} |
|
|
|
for (ushort i = 0; i < DO_L; i++) { updata_dtd((i + 2001).ToString(), DO[i]); } |
|
|
|
//for (ushort i = 0; i < DQ_L; i++) { updata_dtd((i + 3001).ToString(), DQ[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())); } |
|
|
|
}); |
|
|
|
|
|
|
|
await Task.Run(() => |
|
|
@ -293,58 +345,111 @@ namespace DyeingComputer.ViewModel |
|
|
|
|
|
|
|
public int Selet_dtm(string key)//M寄存器
|
|
|
|
{ |
|
|
|
DataRow[] arrRows = dt_m.Select("ID=" + key); |
|
|
|
int index = Convert.ToInt16(arrRows[0]["Value"].ToString()); |
|
|
|
return index; |
|
|
|
} |
|
|
|
try |
|
|
|
{ |
|
|
|
DataRow[] arrRows = dt_m.Select("ID='" + key+"'"); |
|
|
|
int index = Convert.ToInt16(arrRows[0]["Value"].ToString()); |
|
|
|
return index; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
LogGing.LogGingDATA(ex.ToString()); |
|
|
|
return -1; |
|
|
|
} |
|
|
|
} |
|
|
|
public bool Selet_dtd(string key)//D寄存器
|
|
|
|
{ |
|
|
|
DataRow[] arrRows = dt_d.Select("ID=" + key); |
|
|
|
try { |
|
|
|
DataRow[] arrRows = dt_d.Select("ID='" + key + "'"); |
|
|
|
bool index = Convert.ToBoolean(arrRows[0]["DIO"].ToString()); |
|
|
|
return index; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
LogGing.LogGingDATA(ex.ToString()); |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
public int Selet_dta(string key)//A寄存器
|
|
|
|
{ |
|
|
|
DataRow[] arrRows = dt_a.Select("ID=" + key); |
|
|
|
try { |
|
|
|
DataRow[] arrRows = dt_a.Select("ID='" + key + "'"); |
|
|
|
int index = Convert.ToInt16(arrRows[0]["AIO"].ToString()); |
|
|
|
return index; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
LogGing.LogGingDATA(ex.ToString()); |
|
|
|
return -1; |
|
|
|
} |
|
|
|
} |
|
|
|
public void updata_dtm(string key,double Value)//更新M寄存器
|
|
|
|
{ |
|
|
|
DataRow[] arrRows = dt_m.Select("ID="+key); |
|
|
|
int index = dt_m.Rows.IndexOf(arrRows[0]); |
|
|
|
if (index != -1) |
|
|
|
try |
|
|
|
{ |
|
|
|
DataRow drEmployee = dt_m.Rows[index]; |
|
|
|
drEmployee.BeginEdit(); |
|
|
|
drEmployee["Value"] = Value; |
|
|
|
drEmployee.EndEdit(); |
|
|
|
lock (dt_m) |
|
|
|
{ |
|
|
|
DataRow[] arrRows = dt_m.Select("ID='" + key + "'"); |
|
|
|
int index = dt_m.Rows.IndexOf(arrRows[0]); |
|
|
|
if (index != -1) |
|
|
|
{ |
|
|
|
DataRow drEmployee = dt_m.Rows[index]; |
|
|
|
drEmployee.BeginEdit(); |
|
|
|
drEmployee["Value"] = Value; |
|
|
|
drEmployee.EndEdit(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
LogGing.LogGingDATA(ex.ToString()); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
public void updata_dtd(string key, bool Value)//更新D寄存器
|
|
|
|
{ |
|
|
|
DataRow[] arrRows = dt_d.Select("ID='" + key+"'"); |
|
|
|
int index = dt_d.Rows.IndexOf(arrRows[0]); |
|
|
|
if (index != -1) |
|
|
|
try |
|
|
|
{ |
|
|
|
lock (dt_d)//锁
|
|
|
|
{ |
|
|
|
DataRow[] arrRows = dt_d.Select("ID='" + key + "'"); |
|
|
|
int index = dt_d.Rows.IndexOf(arrRows[0]); |
|
|
|
if (index != -1) |
|
|
|
{ |
|
|
|
DataRow drEmployee = dt_d.Rows[index]; |
|
|
|
drEmployee.BeginEdit(); |
|
|
|
drEmployee["DIO"] = Value; |
|
|
|
drEmployee.EndEdit(); |
|
|
|
drEmployee.AcceptChanges(); |
|
|
|
drEmployee.ClearErrors(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
DataRow drEmployee = dt_d.Rows[index]; |
|
|
|
drEmployee.BeginEdit(); |
|
|
|
drEmployee["DIO"] = Value; |
|
|
|
drEmployee.EndEdit(); |
|
|
|
drEmployee.AcceptChanges(); |
|
|
|
drEmployee.ClearErrors(); |
|
|
|
LogGing.LogGingDATA(ex.ToString()); |
|
|
|
} |
|
|
|
} |
|
|
|
public void updata_dta(string key, int Value)//更新A寄存器
|
|
|
|
{ |
|
|
|
DataRow[] arrRows = dt_a.Select("ID=" + key); |
|
|
|
int index = dt_a.Rows.IndexOf(arrRows[0]); |
|
|
|
if (index != -1) |
|
|
|
try |
|
|
|
{ |
|
|
|
lock (dt_a) |
|
|
|
{ |
|
|
|
DataRow[] arrRows = dt_a.Select("ID='" + key + "'"); |
|
|
|
int index = dt_a.Rows.IndexOf(arrRows[0]); |
|
|
|
if (index != -1) |
|
|
|
{ |
|
|
|
DataRow drEmployee = dt_a.Rows[index]; |
|
|
|
drEmployee.BeginEdit(); |
|
|
|
drEmployee["AIO"] = Value; |
|
|
|
drEmployee.EndEdit(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
DataRow drEmployee = dt_a.Rows[index]; |
|
|
|
drEmployee.BeginEdit(); |
|
|
|
drEmployee["AIO"] = Value; |
|
|
|
drEmployee.EndEdit(); |
|
|
|
LogGing.LogGingDATA(ex.ToString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|