|
|
@ -21,6 +21,7 @@ using NModbus.Serial; |
|
|
|
using System.IO.Ports; |
|
|
|
using OpenTK.Graphics.ES11; |
|
|
|
using DyeingComputer.Properties; |
|
|
|
using Microsoft.Win32; |
|
|
|
|
|
|
|
|
|
|
|
namespace DyeingComputer.ViewModel |
|
|
@ -59,7 +60,6 @@ namespace DyeingComputer.ViewModel |
|
|
|
SQL_data(); //读数据库
|
|
|
|
UPort(); //启动串口
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public string work_Temp; //显示温度
|
|
|
@ -134,7 +134,7 @@ namespace DyeingComputer.ViewModel |
|
|
|
} |
|
|
|
void DisTimer_100MS(object sender, EventArgs e)//Tick_Event周期执行事件
|
|
|
|
{ |
|
|
|
|
|
|
|
// if (LINK_OK) Modbus_link();
|
|
|
|
} |
|
|
|
|
|
|
|
public static SerialPort port = new SerialPort();//创建串口
|
|
|
@ -169,31 +169,28 @@ namespace DyeingComputer.ViewModel |
|
|
|
ushort DQ_L = 128; |
|
|
|
bool[] DO = new bool[128]; |
|
|
|
ushort DO_L = 128; |
|
|
|
int[] MW = new int[128]; |
|
|
|
ushort MW_L = 128; |
|
|
|
ushort[] DW = new ushort[125]; |
|
|
|
ushort DW_L = 125; |
|
|
|
//int[] MW = new int[128];
|
|
|
|
// ushort MW_L = 128;
|
|
|
|
ushort[] DW = new ushort[100]; |
|
|
|
ushort DW_L = 100; |
|
|
|
ushort[] RW = new ushort[100]; |
|
|
|
ushort RW_L = 100; |
|
|
|
readonly byte slaveId = 1; |
|
|
|
|
|
|
|
private async void Modbus_link() |
|
|
|
{ |
|
|
|
await Task.Run(() => |
|
|
|
{ |
|
|
|
master.Transport.ReadTimeout = 500; |
|
|
|
master.Transport.WriteTimeout = 500; |
|
|
|
|
|
|
|
LINK_RUN++; |
|
|
|
try |
|
|
|
{ |
|
|
|
// ushort startAddress = 46001;
|
|
|
|
// ushort[] registers = new ushort[] { 1, 2, 3 };
|
|
|
|
// master.WriteMultipleRegisters(slaveId, startAddress, registers);
|
|
|
|
|
|
|
|
|
|
|
|
DO = master.ReadCoils(slaveId, 01000, DO_L); |
|
|
|
DQ = master.ReadCoils(slaveId, 00000, DQ_L); |
|
|
|
DW = master.ReadHoldingRegisters(slaveId,06000, DW_L); |
|
|
|
master.WriteMultipleCoils(slaveId, 00000, DQ); //写入线圈
|
|
|
|
master.WriteMultipleRegisters(slaveId,07000,DW); //写入寄存器
|
|
|
|
|
|
|
|
DO = master.ReadCoils(slaveId, 01000, DO_L); //读取线圈
|
|
|
|
// DQ = master.ReadCoils(slaveId, 00000, DQ_L);
|
|
|
|
RW = master.ReadHoldingRegisters(slaveId,06000, RW_L); //读取寄存器
|
|
|
|
// RW = master.ReadHoldingRegisters(slaveId, 07000, RW_L);
|
|
|
|
LINK_OK =true; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
@ -201,20 +198,16 @@ namespace DyeingComputer.ViewModel |
|
|
|
LINK_OK=false; |
|
|
|
LINK_ERR++; |
|
|
|
// LogGing.LogGingDATA(ex.Message);
|
|
|
|
Name_err = "PLC"+Properties.Resources.Connectionlost; |
|
|
|
Name_err = "PLC"+Properties.Resources.Connectionlost+"("+ ex.Message + ")"; |
|
|
|
} |
|
|
|
updata_dtm("1001", LINK_RUN); |
|
|
|
updata_dtm("1002", LINK_ERR); |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
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"); |
|
|
|
|
|
|
|
public DataTable dt_d = new DataTable("DIO"); |
|
|
|
public DataTable dt_a = new DataTable("AIO"); |
|
|
|
public DataTable dt_m = new DataTable("M"); |
|
|
|
void SQL_data()//获得io表
|
|
|
|
{ |
|
|
|
SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径
|
|
|
@ -227,13 +220,21 @@ namespace DyeingComputer.ViewModel |
|
|
|
|
|
|
|
async void IO_data()//刷新
|
|
|
|
{ |
|
|
|
updata_dtm("1001", LINK_RUN); |
|
|
|
updata_dtm("1002", LINK_ERR); |
|
|
|
|
|
|
|
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 < DW_L; i++) { updata_dta((i + 4001).ToString(), DW[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())); } |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
@ -245,34 +246,37 @@ namespace DyeingComputer.ViewModel |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private int Selet_dtm(string key)//M寄存器
|
|
|
|
public int Selet_dtm(string key)//M寄存器
|
|
|
|
{ |
|
|
|
DataRow[] arrRows = dt_m.Select("ID=" + key); |
|
|
|
int index = Convert.ToInt16(arrRows[0]["Value"].ToString()); |
|
|
|
return index; |
|
|
|
} |
|
|
|
private bool Selet_dtd(string key)//D寄存器
|
|
|
|
public bool Selet_dtd(string key)//D寄存器
|
|
|
|
{ |
|
|
|
DataRow[] arrRows = dt_d.Select("ID=" + key); |
|
|
|
bool index = Convert.ToBoolean(arrRows[0]["DIO"].ToString()); |
|
|
|
return true; |
|
|
|
return index; |
|
|
|
} |
|
|
|
private int Selet_dta(string key)//A寄存器
|
|
|
|
public int Selet_dta(string key)//A寄存器
|
|
|
|
{ |
|
|
|
DataRow[] arrRows = dt_a.Select("ID=" + key); |
|
|
|
int index = Convert.ToInt16(arrRows[0]["AIO"].ToString()); |
|
|
|
return index; |
|
|
|
} |
|
|
|
void updata_dtm(string key,int Value)//更新M寄存器
|
|
|
|
public 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(); |
|
|
|
if (index != -1) |
|
|
|
{ |
|
|
|
DataRow drEmployee = dt_m.Rows[index]; |
|
|
|
drEmployee.BeginEdit(); |
|
|
|
drEmployee["Value"] = Value; |
|
|
|
drEmployee.EndEdit(); |
|
|
|
} |
|
|
|
} |
|
|
|
void updata_dtd(string key, bool Value)//更新D寄存器
|
|
|
|
public void updata_dtd(string key, bool Value)//更新D寄存器
|
|
|
|
{ |
|
|
|
DataRow[] arrRows = dt_d.Select("ID='" + key+"'"); |
|
|
|
int index = dt_d.Rows.IndexOf(arrRows[0]); |
|
|
@ -286,14 +290,17 @@ namespace DyeingComputer.ViewModel |
|
|
|
drEmployee.ClearErrors(); |
|
|
|
} |
|
|
|
} |
|
|
|
void updata_dta(string key, int Value)//更新A寄存器
|
|
|
|
public 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(); |
|
|
|
if (index != -1) |
|
|
|
{ |
|
|
|
DataRow drEmployee = dt_a.Rows[index]; |
|
|
|
drEmployee.BeginEdit(); |
|
|
|
drEmployee["AIO"] = Value; |
|
|
|
drEmployee.EndEdit(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
ObservableCollection<DATA_A> sysData_A = new ObservableCollection<DATA_A>(); |
|
|
|