sc 1 year ago
parent
commit
0d79bc3d46
  1. 43
      App.xaml.cs
  2. 1
      View/InOutView.xaml.cs
  3. 97
      ViewModel/MainWindowViewModel.cs

43
App.xaml.cs

@ -1,10 +1,12 @@
using DyeingComputer.UserClass; using DyeingComputer.UserClass;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; using System.Configuration;
using System.Data; using System.Data;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
@ -26,8 +28,7 @@ namespace DyeingComputer
} }
System.Threading.Mutex mutex; System.Threading.Mutex mutex;
void App_Startup(object sender, StartupEventArgs e) void App_Startup(object sender, StartupEventArgs e)
{ {
LogGing.LogGingDATA("800_Start");
bool ret; bool ret;
mutex = new System.Threading.Mutex(true, "ElectronicNeedleTherapySystem", out ret);//程序只允许启动一次 mutex = new System.Threading.Mutex(true, "ElectronicNeedleTherapySystem", out ret);//程序只允许启动一次
if (!ret) if (!ret)
@ -36,6 +37,11 @@ namespace DyeingComputer
MessageBox.Show("The program has been launched"); MessageBox.Show("The program has been launched");
Environment.Exit(0); Environment.Exit(0);
} }
LogGing.LogGingDATA("800_Start");
SystemSleepTool.PreventSleep();
//return Container.Resolve<MainWindow>();
} }
protected override void OnStartup(StartupEventArgs e) //跟踪报错信息 protected override void OnStartup(StartupEventArgs e) //跟踪报错信息
@ -108,12 +114,39 @@ namespace DyeingComputer
} }
/// <summary> /// <summary>
/// 启动初始页面 /// 启动初始页面
/// </summary> /// </summary>
private void Application_Startup(object sender, StartupEventArgs e) private void Application_Startup(object sender, StartupEventArgs e)
{ {
Application.Current.StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);//和MainWindwo同一目录 Application.Current.StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);//和MainWindwo同一目录
} }
public static class SystemSleepTool
{
//定义API函数
[DllImport("kernel32.dll")]
static extern uint SetThreadExecutionState(ExecutionFlag flags);
[Flags]
enum ExecutionFlag : uint
{
ES_SYSTEM_REQUIRED = 0x00000001,
ES_DISPLAY_REQUIRED = 0x00000002,
ES_CONTINUOUS = 0x80000000,
}
/// <summary>
///阻止系统休眠
/// </summary>
public static void PreventSleep()
{
SetThreadExecutionState(ExecutionFlag.ES_SYSTEM_REQUIRED | ExecutionFlag.ES_DISPLAY_REQUIRED | ExecutionFlag.ES_CONTINUOUS);
}
/// <summary>
///恢复系统休眠
/// </summary>
public static void RestoreSleep()
{
SetThreadExecutionState(ExecutionFlag.ES_CONTINUOUS);
}
}
} }
} }

1
View/InOutView.xaml.cs

@ -10,6 +10,7 @@ using System.Windows.Data;
using System.Windows.Documents; using System.Windows.Documents;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Navigation; using System.Windows.Navigation;
using System.Windows.Shapes; using System.Windows.Shapes;

97
ViewModel/MainWindowViewModel.cs

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

Loading…
Cancel
Save