This commit is contained in:
sc committed 2024-09-14 01:09:49 +08:00
commit 21d5cbc00a
4 files changed
+44 -30

No files matched your search

+32 -24
View File
@@ -94,7 +94,7 @@ namespace DyeingComputer.ViewModel
{
DispatcherTimer timer1s = new DispatcherTimer//初始化循环,每1秒调用一次Tick
{
Interval = TimeSpan.FromSeconds(1)//秒
Interval = TimeSpan.FromSeconds(0.5)//秒
};
timer1s.Tick += Tick_Event_1S;
timer1s.Start();
@@ -165,8 +165,12 @@ namespace DyeingComputer.ViewModel
private bool LINK_OK = false;
private int LINK_RUN = 0;
private int LINK_ERR = 0;
bool[] DQ = new bool[12];
ushort DQ_L = 12;
bool[] DQ = new bool[128];
ushort DQ_L = 128;
bool[] DO = new bool[128];
ushort DO_L = 128;
readonly byte slaveId = 1;
private async void Modbus_link()
{
await Task.Run(() =>
@@ -178,21 +182,22 @@ namespace DyeingComputer.ViewModel
// write three registers
LINK_RUN++;
// try
try
{
byte slaveId = 1;
ushort startAddress = 46001;
ushort[] registers = new ushort[] { 1, 2, 3 };
// ushort startAddress = 46001;
// ushort[] registers = new ushort[] { 1, 2, 3 };
// master.WriteMultipleRegisters(slaveId, startAddress, registers);
// bool[] x = master.ReadInputs(slaveId, 1001, 100);
DQ = master.ReadCoils(slaveId,0, DQ_L);
DO = master.ReadCoils(slaveId, 01000, DO_L);
DQ = master.ReadCoils(slaveId, 00000, DQ_L);
LINK_OK=true;
}
// catch (Exception ex)
catch (Exception ex)
{
LINK_OK=false;
LINK_ERR++;
// LogGing.LogGingDATA(ex.Message);
Name_err = "PLC"+Properties.Resources.Connectionlost;
@@ -213,7 +218,7 @@ namespace DyeingComputer.ViewModel
{
SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径
SQLiteHelpers.Open(); //打开数据库
dt_d = SQLiteHelpers.ExecuteDataSet("select * from IOName where type='DI' OR type='DO'", null).Tables[0]; //读取表写入缓存
dt_d = SQLiteHelpers.ExecuteDataSet("select * from IOName where ID>'2000' AND ID<'4000'", 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();
@@ -223,14 +228,12 @@ namespace DyeingComputer.ViewModel
{
await Task.Run(() =>
{
for (ushort i = 0; i < DQ_L; i++)
{
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]); }
});
//updata_dtd(i.ToString(), DQ[i]);
}
updata_dtd("1", false);
await Task.Run(() =>
{
SYSData_A = ToObservableCollection<DATA_A>(dt_a);
SYSData_D = ToObservableCollection<DATA_D>(dt_d);
SYSData_M = ToObservableCollection<DATA_M>(dt_m);
@@ -266,12 +269,17 @@ namespace DyeingComputer.ViewModel
}
void updata_dtd(string key, bool Value)//更新D寄存器
{
DataRow[] arrRows = dt_d.Select("ID=" + key);
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();
if (index != -1)
{
DataRow drEmployee = dt_d.Rows[index];
drEmployee.BeginEdit();
drEmployee["DIO"] = Value;
drEmployee.EndEdit();
drEmployee.AcceptChanges();
drEmployee.ClearErrors();
}
}
void updata_dta(string key, int Value)//更新A寄存器
{