IOSerial.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. namespace Universal.IO;
  2. public class IOSerial : BaseFilter, IDisposable
  3. {
  4. private static bool SerialPortExist(string portName)
  5. {
  6. foreach (string singlePortName in SerialPort.GetPortNames())
  7. {
  8. if (singlePortName.ToLower().Equals(portName.ToLower()))
  9. return true;
  10. }
  11. return false;
  12. }
  13. volatile bool _init = false;
  14. public string? Name { get; internal set; }
  15. private SerialConnection? _SerialPortConnection;
  16. public bool Initialize(string name)
  17. {
  18. if (_init)
  19. return false;
  20. _init = true;
  21. this.Name = name;
  22. return true;
  23. }
  24. public bool Open(string strPortName, int baudRate, Parity parity, int dataBits, StopBits stopBits, out SerialConnection? connection)
  25. {
  26. connection = null;
  27. if (!_init)
  28. return false;
  29. if (_SerialPortConnection is not null && _SerialPortConnection.SerialPort.IsOpen)
  30. return false;
  31. if (!SerialPortExist(strPortName))
  32. return false;
  33. if (baudRate <= 0)
  34. return false;
  35. if (dataBits < 5 || dataBits > 8)
  36. return false;
  37. SerialPort serialPort;
  38. SerialConnection? serialPortConnection = null;
  39. try
  40. {
  41. serialPort = new(strPortName, baudRate, parity, dataBits, stopBits);
  42. serialPort.Open();
  43. serialPortConnection = new(strPortName, serialPort);
  44. PrevConnector?.Connected(serialPortConnection!);
  45. }
  46. catch { return false; }
  47. serialPort.DataReceived += DataReceivedEvent;
  48. this._SerialPortConnection = serialPortConnection;
  49. connection = this._SerialPortConnection;
  50. return true;
  51. }
  52. void DataReceivedEvent(object sender, SerialDataReceivedEventArgs e)
  53. {
  54. DateTime receiveTime = DateTime.Now;
  55. if (this._SerialPortConnection is null)
  56. return;
  57. byte[] cache = new byte[this._SerialPortConnection.SerialPort.BytesToRead];
  58. try
  59. {
  60. this._SerialPortConnection.SerialPort.Read(cache, 0, cache.Length);
  61. }
  62. catch
  63. {
  64. //Port Closed
  65. }
  66. PrevReceiver?.Receive(new(cache, _SerialPortConnection, receiveTime));
  67. }
  68. public override bool Send(Data data)
  69. {
  70. if (!_init)
  71. return false;
  72. if (this._SerialPortConnection is null)
  73. return false;
  74. if (this._SerialPortConnection is null)
  75. return false;
  76. if (!this._SerialPortConnection.SerialPort.IsOpen)
  77. return false;
  78. if (!this._SerialPortConnection.SerialPort.DsrHolding)
  79. return false;
  80. if (data.RawData == null || data.RawData.Length == 0)
  81. return false;
  82. if (data.Connection is null)
  83. return false;
  84. if (data.Connection != this._SerialPortConnection)
  85. return false;
  86. try
  87. {
  88. this._SerialPortConnection.SerialPort.BaseStream.Write([.. data.RawData], 0, data.RawData.Length);
  89. this._SerialPortConnection.SerialPort.BaseStream.Flush();
  90. }
  91. catch
  92. {
  93. return false;
  94. }
  95. return true;
  96. }
  97. public bool Close()
  98. {
  99. if (!_init)
  100. return false;
  101. if (this._SerialPortConnection is not null)
  102. {
  103. if (this._SerialPortConnection.SerialPort.IsOpen)
  104. this._SerialPortConnection.SerialPort.Close();
  105. PrevConnector?.Disconnected(this._SerialPortConnection);
  106. }
  107. return true;
  108. }
  109. private bool disposedValue;
  110. protected virtual void Dispose(bool disposing)
  111. {
  112. if (disposedValue)
  113. return;
  114. if (disposing)
  115. this._SerialPortConnection = null;
  116. Close();
  117. disposedValue = true;
  118. }
  119. public void Dispose()
  120. {
  121. Dispose(disposing: true);
  122. GC.SuppressFinalize(this);
  123. }
  124. }