ZhongShengLedControl.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using FluentModbus;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Markup;
  11. namespace AwInitilizer
  12. {
  13. internal class ZhongShengLedControl : IDisposable
  14. {
  15. private readonly MainWindow mainWindow;
  16. private const int DevAddress = 1;
  17. private const int GreenAddress = 0;
  18. private const int YelloAddress = 1;
  19. private const int EmergencyAddress = 2;
  20. private ModbusRtuClient modbusRtuClient;
  21. private Task poolingMonitorTask;
  22. private bool isInEmergency = false;
  23. private CancellationTokenSource cancellationSource;
  24. private bool IsInitCompleted => modbusRtuClient != null;
  25. private SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);
  26. private bool isDisposing = false;
  27. private bool isPoolingMonitorTaskComplete = false;
  28. public ZhongShengLedControl(ModbusRtuClient comportClient, MainWindow mainWindow)
  29. {
  30. if (comportClient is null)
  31. {
  32. return;
  33. }
  34. cancellationSource = new CancellationTokenSource();
  35. this.mainWindow = mainWindow;
  36. //mainWindow.OnUpdateStarting += MainWindow_OnUpdateStarting;
  37. //mainWindow.OnUpdateCompleted += MainWindow_OnUpdateCompleted;
  38. InitComport(comportClient);
  39. InitMoniter();
  40. }
  41. public ZhongShengLedControl(string comport, MainWindow mainWindow)
  42. {
  43. if (string.IsNullOrEmpty(comport))
  44. {
  45. return;
  46. }
  47. cancellationSource = new CancellationTokenSource();
  48. this.mainWindow = mainWindow;
  49. //mainWindow.OnUpdateStarting += MainWindow_OnUpdateStarting;
  50. //mainWindow.OnUpdateCompleted += MainWindow_OnUpdateCompleted;
  51. InitComport(comport);
  52. InitMoniter();
  53. }
  54. private async void MainWindow_OnUpdateCompleted(object sender, EventArgs e)
  55. {
  56. if (!IsInitCompleted || isInEmergency)
  57. {
  58. return;
  59. }
  60. await SetGreenOn(true);
  61. await SetYelloOn(false);
  62. }
  63. private async void MainWindow_OnUpdateStarting(object sender, EventArgs e)
  64. {
  65. if (!IsInitCompleted || isInEmergency)
  66. {
  67. return;
  68. }
  69. await SetGreenOn(false);
  70. await SetYelloOn(true);
  71. }
  72. public void Dispose()
  73. {
  74. Application.Current.Dispatcher.Invoke(() => {
  75. isDisposing = true;
  76. //cancellationSource.Cancel();
  77. Task.Delay(1000).Wait();
  78. SetGreenOn(true).Wait();
  79. SetYelloOn(false).Wait();
  80. //mainWindow.OnUpdateStarting -= MainWindow_OnUpdateStarting;
  81. //mainWindow.OnUpdateCompleted -= MainWindow_OnUpdateCompleted;
  82. modbusRtuClient.Dispose();
  83. });
  84. }
  85. public async Task DisposeAsync()
  86. {
  87. isDisposing = true;
  88. //cancellationSource.Cancel();
  89. //var wai = poolingMonitorTask.Result;
  90. while(!isPoolingMonitorTaskComplete)
  91. {
  92. await Task.Delay(1000);
  93. }
  94. var test = poolingMonitorTask.Status;
  95. await SetGreenOn(true);
  96. await SetYelloOn(false);
  97. //mainWindow.OnUpdateStarting -= MainWindow_OnUpdateStarting;
  98. //mainWindow.OnUpdateCompleted -= MainWindow_OnUpdateCompleted;
  99. modbusRtuClient.Dispose();
  100. }
  101. private void InitComport(ModbusRtuClient comportClient)
  102. {
  103. modbusRtuClient = comportClient;
  104. }
  105. private void InitComport(string comport)
  106. {
  107. try
  108. {
  109. modbusRtuClient = new ModbusRtuClient()
  110. {
  111. BaudRate = 38400,
  112. StopBits = System.IO.Ports.StopBits.One,
  113. Parity = System.IO.Ports.Parity.None //arity奇偶
  114. };
  115. modbusRtuClient.Connect(comport);
  116. }
  117. catch (Exception ex)
  118. {
  119. MessageBox.Show("ZhongShengLed port open failed");
  120. throw;
  121. }
  122. }
  123. private void InitMoniter()
  124. {
  125. if (poolingMonitorTask != null)
  126. {
  127. return;
  128. }
  129. //poolingMonitorTask = MonitorTask(cancellationSource.Token);
  130. poolingMonitorTask = Task.Factory.StartNew(() => MonitorTask().Wait(), TaskCreationOptions.AttachedToParent);
  131. }
  132. private async Task MonitorTask(CancellationToken token = default)
  133. {
  134. try
  135. {
  136. while (!token.IsCancellationRequested && !isDisposing)
  137. {
  138. var emergencyDataRead = await Read(EmergencyAddress, token);
  139. if (isDisposing) return ;
  140. isInEmergency = emergencyDataRead > 0;
  141. if (isInEmergency)
  142. {
  143. //await SetGreenOn(false);
  144. await SetYelloOn(false, token);
  145. if (isDisposing) return ;
  146. }
  147. else
  148. {
  149. await SetGreenOn(false, token);
  150. if (isDisposing) return ;
  151. await SetYelloOn(true, token);
  152. if (isDisposing) return ;
  153. }
  154. await Task.Delay(1000);
  155. }
  156. }
  157. catch
  158. {
  159. }
  160. finally
  161. {
  162. isPoolingMonitorTaskComplete = true;
  163. }
  164. return ;
  165. }
  166. public async Task SetGreenOn(bool isOn, CancellationToken token = default)
  167. {
  168. try
  169. {
  170. //var greenValue = await Read(GreenAddress);
  171. //var isGreenOn = greenValue == 0;
  172. //if (isGreenOn != isOn)
  173. //{
  174. // await Write(GreenAddress, 1);
  175. //}
  176. await Write(GreenAddress, isOn ? (short)0 : (short)256, token);
  177. }
  178. catch (Exception ex)
  179. {
  180. }
  181. }
  182. public async Task SetYelloOn(bool isOn, CancellationToken token = default)
  183. {
  184. try
  185. {
  186. //var yelloValue = await Read(YelloAddress);
  187. //var isYellowOn = yelloValue > 0;
  188. //if (isYellowOn != isOn)
  189. //{
  190. // await Write(YelloAddress, 1);
  191. //}
  192. await Write(YelloAddress, isOn ? (short)256 : (short)0, token);
  193. }
  194. catch (Exception ex)
  195. {
  196. }
  197. }
  198. private async Task Write(int address, short value, CancellationToken token = default)
  199. {
  200. //await semaphore.WaitAsync();
  201. try
  202. {
  203. await modbusRtuClient.WriteSingleRegisterAsync(DevAddress, address, value, token);
  204. }
  205. catch (Exception e)
  206. {
  207. }
  208. finally
  209. {
  210. //semaphore.Release();
  211. }
  212. return;
  213. }
  214. private async Task<short?> Read(int address, CancellationToken token = default)
  215. {
  216. //await semaphore.WaitAsync();
  217. try
  218. {
  219. //var result = await modbusRtuClient.ReadInputRegistersAsync<short>(DevAddress, address, 1);
  220. var result = await modbusRtuClient.ReadHoldingRegistersAsync<short>(DevAddress, address, 1, token);
  221. var resultArray = result.ToArray();
  222. if (resultArray.Length == 0)
  223. {
  224. return null;
  225. }
  226. return resultArray[0];
  227. }
  228. catch (Exception ex)
  229. {
  230. return null;
  231. }
  232. finally
  233. {
  234. //semaphore.Release();
  235. }
  236. }
  237. }
  238. }