123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- using FluentModbus;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Markup;
- namespace AwInitilizer
- {
- internal class ZhongShengLedControl : IDisposable
- {
- private readonly MainWindow mainWindow;
- private const int DevAddress = 1;
- private const int GreenAddress = 0;
- private const int YelloAddress = 1;
- private const int EmergencyAddress = 2;
- private ModbusRtuClient modbusRtuClient;
- private Task poolingMonitorTask;
- private bool isInEmergency = false;
- private CancellationTokenSource cancellationSource;
- private bool IsInitCompleted => modbusRtuClient != null;
- private SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);
- private bool isDisposing = false;
- private bool isPoolingMonitorTaskComplete = false;
- public ZhongShengLedControl(ModbusRtuClient comportClient, MainWindow mainWindow)
- {
- if (comportClient is null)
- {
- return;
- }
- cancellationSource = new CancellationTokenSource();
- this.mainWindow = mainWindow;
- //mainWindow.OnUpdateStarting += MainWindow_OnUpdateStarting;
- //mainWindow.OnUpdateCompleted += MainWindow_OnUpdateCompleted;
- InitComport(comportClient);
- InitMoniter();
- }
- public ZhongShengLedControl(string comport, MainWindow mainWindow)
- {
- if (string.IsNullOrEmpty(comport))
- {
- return;
- }
- cancellationSource = new CancellationTokenSource();
- this.mainWindow = mainWindow;
- //mainWindow.OnUpdateStarting += MainWindow_OnUpdateStarting;
- //mainWindow.OnUpdateCompleted += MainWindow_OnUpdateCompleted;
- InitComport(comport);
- InitMoniter();
- }
- private async void MainWindow_OnUpdateCompleted(object sender, EventArgs e)
- {
- if (!IsInitCompleted || isInEmergency)
- {
- return;
- }
- await SetGreenOn(true);
- await SetYelloOn(false);
- }
- private async void MainWindow_OnUpdateStarting(object sender, EventArgs e)
- {
- if (!IsInitCompleted || isInEmergency)
- {
- return;
- }
- await SetGreenOn(false);
- await SetYelloOn(true);
- }
- public void Dispose()
- {
- Application.Current.Dispatcher.Invoke(() => {
- isDisposing = true;
- //cancellationSource.Cancel();
- Task.Delay(1000).Wait();
- SetGreenOn(true).Wait();
- SetYelloOn(false).Wait();
- //mainWindow.OnUpdateStarting -= MainWindow_OnUpdateStarting;
- //mainWindow.OnUpdateCompleted -= MainWindow_OnUpdateCompleted;
- modbusRtuClient.Dispose();
- });
- }
- public async Task DisposeAsync()
- {
- isDisposing = true;
- //cancellationSource.Cancel();
- //var wai = poolingMonitorTask.Result;
- while(!isPoolingMonitorTaskComplete)
- {
- await Task.Delay(1000);
- }
- var test = poolingMonitorTask.Status;
- await SetGreenOn(true);
- await SetYelloOn(false);
- //mainWindow.OnUpdateStarting -= MainWindow_OnUpdateStarting;
- //mainWindow.OnUpdateCompleted -= MainWindow_OnUpdateCompleted;
- modbusRtuClient.Dispose();
- }
- private void InitComport(ModbusRtuClient comportClient)
- {
- modbusRtuClient = comportClient;
- }
- private void InitComport(string comport)
- {
- try
- {
- modbusRtuClient = new ModbusRtuClient()
- {
- BaudRate = 38400,
- StopBits = System.IO.Ports.StopBits.One,
- Parity = System.IO.Ports.Parity.None //arity奇偶
- };
- modbusRtuClient.Connect(comport);
- }
- catch (Exception ex)
- {
- MessageBox.Show("ZhongShengLed port open failed");
- throw;
- }
- }
- private void InitMoniter()
- {
- if (poolingMonitorTask != null)
- {
- return;
- }
- //poolingMonitorTask = MonitorTask(cancellationSource.Token);
- poolingMonitorTask = Task.Factory.StartNew(() => MonitorTask().Wait(), TaskCreationOptions.AttachedToParent);
- }
- private async Task MonitorTask(CancellationToken token = default)
- {
- try
- {
- while (!token.IsCancellationRequested && !isDisposing)
- {
- var emergencyDataRead = await Read(EmergencyAddress, token);
- if (isDisposing) return ;
- isInEmergency = emergencyDataRead > 0;
- if (isInEmergency)
- {
- //await SetGreenOn(false);
- await SetYelloOn(false, token);
- if (isDisposing) return ;
- }
- else
- {
- await SetGreenOn(false, token);
- if (isDisposing) return ;
- await SetYelloOn(true, token);
- if (isDisposing) return ;
- }
- await Task.Delay(1000);
- }
- }
- catch
- {
- }
- finally
- {
- isPoolingMonitorTaskComplete = true;
- }
- return ;
- }
- public async Task SetGreenOn(bool isOn, CancellationToken token = default)
- {
- try
- {
- //var greenValue = await Read(GreenAddress);
- //var isGreenOn = greenValue == 0;
- //if (isGreenOn != isOn)
- //{
- // await Write(GreenAddress, 1);
- //}
- await Write(GreenAddress, isOn ? (short)0 : (short)256, token);
- }
- catch (Exception ex)
- {
- }
- }
- public async Task SetYelloOn(bool isOn, CancellationToken token = default)
- {
- try
- {
- //var yelloValue = await Read(YelloAddress);
- //var isYellowOn = yelloValue > 0;
- //if (isYellowOn != isOn)
- //{
- // await Write(YelloAddress, 1);
- //}
- await Write(YelloAddress, isOn ? (short)256 : (short)0, token);
- }
- catch (Exception ex)
- {
- }
- }
- private async Task Write(int address, short value, CancellationToken token = default)
- {
- //await semaphore.WaitAsync();
- try
- {
- await modbusRtuClient.WriteSingleRegisterAsync(DevAddress, address, value, token);
- }
- catch (Exception e)
- {
- }
- finally
- {
- //semaphore.Release();
- }
- return;
- }
- private async Task<short?> Read(int address, CancellationToken token = default)
- {
- //await semaphore.WaitAsync();
- try
- {
- //var result = await modbusRtuClient.ReadInputRegistersAsync<short>(DevAddress, address, 1);
- var result = await modbusRtuClient.ReadHoldingRegistersAsync<short>(DevAddress, address, 1, token);
- var resultArray = result.ToArray();
- if (resultArray.Length == 0)
- {
- return null;
- }
- return resultArray[0];
- }
- catch (Exception ex)
- {
- return null;
- }
- finally
- {
- //semaphore.Release();
- }
- }
- }
- }
|