Commit 549e2ec5 authored by 潘栩锋's avatar 潘栩锋 🚴

优化 LCUS1 改为用定时器1s 写入一次状态。 就是LCUS1 模块有问题,多次写入应该能提高执行性。

parent 1a412bc1
......@@ -19,8 +19,7 @@ namespace FLY.Thick.Base.UI
this.paramDictionary = paramDictionary;
//绑定 this.PortName 与 this.paramDictionary.LCUS1_PortName
this.paramDictionary.SetBinding(this, nameof(PortName), ParamDistItemKeys.LCUS1_PortName, "COM1");
Off();
this.paramDictionary.SetBinding(this, nameof(Enable), ParamDistItemKeys.LCUS1_Enable, false);
}
}
}
......@@ -5,6 +5,7 @@ using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
namespace FLY.Thick.Base.UI
{
......@@ -14,48 +15,61 @@ namespace FLY.Thick.Base.UI
public string ErrMsg { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
DispatcherTimer timer;
public void On()
public bool IsOn { get; set; }
private bool enable;
public bool Enable
{
Task.Factory.StartNew(() =>
{
try
{
SerialPort serial = new SerialPort();
serial.PortName = PortName;
serial.BaudRate = 9600;
serial.Open();
byte[] buf = new byte[] { 0xa0, 0x01, 0x01, 0xa2 };
serial.Write(buf, 0, buf.Length);
serial.Close();
ErrMsg = $"{DateTime.Now} ON";
get {
return enable;
}
set {
if (enable != value) {
enable = value;
timer.IsEnabled = enable;
}
catch (Exception e)
{
ErrMsg = e.Message;
}
});
}
}
public void Off()
public LCUS1() {
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += Timer_Tick;
}
private void Timer_Tick(object sender, EventArgs e)
{
Task.Factory.StartNew(() =>
//每1秒都写状态。 避免通讯不上
try
{
try
{
SerialPort serial = new SerialPort();
serial.PortName = PortName;
serial.BaudRate = 9600;
serial.Open();
byte[] buf = new byte[] { 0xa0, 0x01, 0x00, 0xa1 };
serial.Write(buf, 0, buf.Length);
serial.Close();
ErrMsg = $"{DateTime.Now} OFF";
}
catch (Exception e)
{
ErrMsg = e.Message;
}
});
SerialPort serial = new SerialPort();
serial.PortName = PortName;
serial.BaudRate = 9600;
serial.Open();
byte[] buf;
if (IsOn)
buf = new byte[] { 0xa0, 0x01, 0x01, 0xa2 };
else
buf = new byte[] { 0xa0, 0x01, 0x00, 0xa1 };
serial.Write(buf, 0, buf.Length);
serial.Close();
string msg = IsOn ? "On" : "Off";
ErrMsg = $"{DateTime.Now} {msg}";
}
catch (Exception _e)
{
ErrMsg = _e.Message;
}
}
public void On()
{
IsOn = true;
}
public void Off()
{
IsOn = false;
}
}
}
......@@ -37,7 +37,7 @@ namespace FLY.Thick.Base.UI.OnInit
{
if (e.PropertyName == nameof(warningSystemManager.IsRinging))
{
if (this.paramDictionary.GetValue(ParamDistItemKeys.LCUS1_Enable, false))
if (lcus1.Enable)
{
if (warningSystemManager.IsRinging)
lcus1.On();
......
......@@ -37,7 +37,7 @@ namespace FLY.Thick.Base.UI.OnInit
{
if (e.PropertyName == nameof(warningService.IsRinging))
{
if (this.paramDictionary.GetValue(ParamDistItemKeys.LCUS1_Enable, false))
if (lcus1.Enable)
{
if (warningService.IsRinging)
lcus1.On();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment