SerialPortDevice.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Ips.Cfq.SetFreqBySerialPort.Cli
  8. {
  9. public class SerialPortDevice : IDisposable
  10. {
  11. SerialPort _sPort = null;
  12. /// <summary>
  13. /// 串口号
  14. /// </summary>
  15. String _endpoint;
  16. /// <summary>
  17. /// 串口输出
  18. /// </summary>
  19. public String _outString;
  20. public SerialPortDevice(String endpoint)
  21. {
  22. _endpoint = endpoint;
  23. }
  24. /// <summary>
  25. /// 打开串口
  26. /// </summary>
  27. /// <returns></returns>
  28. public bool Open()
  29. {
  30. if (_sPort != null && _sPort.IsOpen)
  31. return true;
  32. bool result = false;
  33. if (string.IsNullOrEmpty(this._endpoint))
  34. {
  35. result = false;
  36. }
  37. else
  38. {
  39. try
  40. {
  41. if (this._sPort == null)
  42. {
  43. this._sPort = new SerialPort();
  44. }
  45. else
  46. {
  47. if (this._sPort.IsOpen)
  48. {
  49. result = true;
  50. return result;
  51. }
  52. }
  53. this._sPort.DataReceived += _sPort_DataReceived;
  54. this._sPort.PortName = _endpoint;
  55. this._sPort.BaudRate = 9600;
  56. this._sPort.DataBits = 8;
  57. this._sPort.StopBits = StopBits.One;
  58. this._sPort.Open();
  59. result = true;
  60. }
  61. catch
  62. {
  63. result = false;
  64. }
  65. }
  66. return result;
  67. }
  68. /// <summary>
  69. /// 串口数据接收
  70. /// </summary>
  71. /// <param name="sender"></param>
  72. /// <param name="e"></param>
  73. private void _sPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
  74. {
  75. string res = string.Empty;
  76. try
  77. {
  78. res = this._sPort.ReadLine();
  79. }
  80. catch
  81. {
  82. }
  83. if (!string.IsNullOrEmpty(res))
  84. {
  85. this._outString = res.Replace("\0", "");
  86. }
  87. }
  88. /// <summary>
  89. /// 发送命令
  90. /// </summary>
  91. /// <param name="command"></param>
  92. public bool SendCommand(String command, ref String errmsg)
  93. {
  94. Console.WriteLine($"in:{command}");
  95. bool result = false;
  96. if (this._sPort == null)
  97. {
  98. errmsg = "串口未初始化";
  99. result = false;
  100. }
  101. else
  102. {
  103. if (!this._sPort.IsOpen)
  104. {
  105. errmsg = "串口未打开";
  106. result = false;
  107. }
  108. else
  109. {
  110. try
  111. {
  112. _outString = String.Empty;
  113. _sPort.Write(command);
  114. result = true;
  115. }
  116. catch (Exception ex)
  117. {
  118. errmsg = $"串口通讯异常:{ex.Message}";
  119. result = false;
  120. }
  121. }
  122. }
  123. return result;
  124. }
  125. /// <summary>
  126. /// 获取串口输出
  127. /// </summary>
  128. /// <returns></returns>
  129. public String GetOutString()
  130. {
  131. int num = 0;
  132. string result;
  133. while (string.IsNullOrEmpty(_outString))
  134. {
  135. num++;
  136. System.Threading.Thread.Sleep(100);
  137. if (num >= 50)
  138. {
  139. result = "";
  140. return result;
  141. }
  142. }
  143. result = _outString;
  144. Console.WriteLine($"out:{result}");
  145. return result;
  146. }
  147. public void Dispose()
  148. {
  149. if (_sPort != null)
  150. {
  151. _sPort.Close();
  152. _sPort.Dispose();
  153. _sPort = null;
  154. }
  155. }
  156. }
  157. }