SerialPortProtocol.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using TeamAAS_VP.Enums;
  7. using TeamAAS_VP.Models;
  8. using TouchSocket.Core;
  9. using TouchSocket.SerialPorts;
  10. using TouchSocket.Sockets;
  11. namespace TeamAAS_VP.Core.Lights
  12. {
  13. public class SerialPortProtocol : ICommunicationProtocol
  14. {
  15. private SerialPortClient _client;
  16. private IWaitingClient<ISerialPortClient, IReceiverResult> _waitClient;
  17. private readonly SerialPortConfig _config;
  18. public bool IsConnected => _client?.Online == true;
  19. public Terminator Terminator { get; set; } = Terminator.None;
  20. public Encoding Encoding { get; set; } = Encoding.ASCII;
  21. public event Action<object, bool> ConnectionChanged;
  22. public event Action<object, string> DataReceived;
  23. public event Action<object, string> DataSent;
  24. public SerialPortProtocol(SerialPortConfig config)
  25. {
  26. _config = config;
  27. }
  28. public async Task<bool> ConnectAsync()
  29. {
  30. try
  31. {
  32. _client = new SerialPortClient();
  33. var config = new TouchSocketConfig()
  34. .SetSerialPortOption(new SerialPortOption()
  35. {
  36. PortName = _config.PortName,
  37. BaudRate = _config.BaudRate,
  38. DataBits = _config.DataBits,
  39. Parity = _config.Parity,
  40. StopBits = _config.StopBits
  41. })
  42. .SetSerialDataHandlingAdapter(() => new PeriodPackageAdapter()
  43. {
  44. CacheTimeout = TimeSpan.FromMilliseconds(100)
  45. });
  46. _client.Setup(config);
  47. var result = await _client.TryConnectAsync();
  48. if (result.IsSuccess)
  49. {
  50. _waitClient = _client.CreateWaitingClient(new WaitingOptions());
  51. ConnectionChanged?.Invoke(this, true);
  52. return true;
  53. }
  54. ConnectionChanged?.Invoke(this, false);
  55. return false;
  56. }
  57. catch
  58. {
  59. ConnectionChanged?.Invoke(this, false);
  60. return false;
  61. }
  62. }
  63. public Task DisconnectAsync()
  64. {
  65. ConnectionChanged?.Invoke(this, false);
  66. _client?.Close();
  67. return Task.CompletedTask;
  68. }
  69. public async Task<byte[]> SendAndReceiveAsync(byte[] data, int timeout = 5000)
  70. {
  71. if (_waitClient == null)
  72. throw new InvalidOperationException("Not connected");
  73. DataSent?.Invoke(this, Encoding.GetString(data));
  74. var response = await _waitClient.SendThenReturnAsync(data, timeout);
  75. DataReceived?.Invoke(this, Encoding.GetString(response));
  76. return response;
  77. }
  78. public Task SendAsync(byte[] data)
  79. {
  80. if (_client == null)
  81. throw new InvalidOperationException("Not connected");
  82. DataSent?.Invoke(this, Encoding.GetString(data));
  83. _client.Send(data);
  84. return Task.CompletedTask;
  85. }
  86. public async Task<string> SendAndReceiveAsync(string data, int timeout = 5000)
  87. {
  88. if (_waitClient == null)
  89. throw new InvalidOperationException("Not connected");
  90. DataSent?.Invoke(this, data);
  91. var response = await SendAndReceiveAsync(Encoding.GetBytes(data), timeout);
  92. DataReceived?.Invoke(this, Encoding.GetString(response));
  93. return Encoding.GetString(response);
  94. }
  95. public string SendAndReceive(string data, int timeout = 5000)
  96. {
  97. if (_waitClient == null)
  98. throw new InvalidOperationException("Not connected");
  99. DataSent?.Invoke(this, data);
  100. var response = _waitClient.SendThenReturn(Encoding.GetBytes(data), timeout);
  101. DataReceived?.Invoke(this, Encoding.GetString(response));
  102. return Encoding.GetString(response);
  103. }
  104. public byte[] SendAndReceive(byte[] data, int timeout = 5000)
  105. {
  106. if (_waitClient == null)
  107. throw new InvalidOperationException("Not connected");
  108. DataSent?.Invoke(this, Encoding.GetString(data));
  109. var response = _waitClient.SendThenReturn(data, timeout);
  110. DataReceived?.Invoke(this, Encoding.GetString(response));
  111. return response;
  112. }
  113. public void Send(string data)
  114. {
  115. if (_client == null)
  116. throw new InvalidOperationException("Not connected");
  117. DataSent?.Invoke(this, data);
  118. _client.Send(Encoding.GetBytes(data));
  119. }
  120. public async Task SendAsync(string data)
  121. {
  122. if (_client == null)
  123. throw new InvalidOperationException("Not connected");
  124. DataSent?.Invoke(this, data);
  125. await _client.SendAsync(Encoding.GetBytes(data));
  126. }
  127. public void Send(byte[] data)
  128. {
  129. if (_client == null)
  130. throw new InvalidOperationException("Not connected");
  131. DataSent?.Invoke(this, Encoding.GetString(data));
  132. _client.Send(data);
  133. }
  134. public void Dispose()
  135. {
  136. _client?.Dispose();
  137. _waitClient = null;
  138. }
  139. }
  140. }