AsyncSerial.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO.Ports;
  6. using Aitex.Core.RT.Log;
  7. namespace Aitex.Sorter.RT.Device
  8. {
  9. // public class AsyncSerial : ICommunication, IDisposable
  10. // {
  11. // public delegate void ErrorHandler(ErrorEventArgs args);
  12. // public event ErrorHandler OnErrorHappened;
  13. // public delegate void MessageHandler(string message);
  14. // public event MessageHandler OnDataChanged;
  15. // private static Object _locker = new Object();
  16. // private SerialPort _port;
  17. // private string _buff = "";
  18. // public AsyncSerial(string name, int baudRate, int dataBits, Parity parity = Parity.None, StopBits stopBits = StopBits.One, string newline ="\r")
  19. // {
  20. // _port = new SerialPort();
  21. // _port.PortName = name;
  22. // _port.BaudRate = baudRate;
  23. // _port.DataBits = dataBits;
  24. // _port.Parity = parity;
  25. // _port.StopBits = stopBits;
  26. // _port.RtsEnable = false;
  27. // _port.DtrEnable = false;
  28. // _port.ReadTimeout = 1000;
  29. // _port.WriteTimeout = 1000;
  30. // _port.NewLine = newline;
  31. // _port.Handshake = Handshake.None;
  32. // _port.DataReceived += new SerialDataReceivedEventHandler(DataReceived);
  33. // _port.ErrorReceived += new SerialErrorReceivedEventHandler(ErrorReceived);
  34. // }
  35. // public void Dispose()
  36. // {
  37. // Close();
  38. // }
  39. // public bool Open()
  40. // {
  41. // if (_port.IsOpen) Close();
  42. // try
  43. // {
  44. // _port.Open();
  45. // _port.DiscardInBuffer();
  46. // _port.DiscardOutBuffer();
  47. // _buff = "";
  48. // }
  49. // catch (Exception e)
  50. // {
  51. // string reason = _port.PortName + " port open failed,please check configuration。" + e.Message;
  52. // OnErrorHappened(new ErrorEventArgs(reason));
  53. // return false;
  54. // }
  55. // return true;
  56. // }
  57. // public bool IsOpen()
  58. // {
  59. // return _port.IsOpen;
  60. // }
  61. // public bool Close()
  62. // {
  63. // if (_port.IsOpen)
  64. // {
  65. // try
  66. // {
  67. // _port.Close();
  68. // }
  69. // catch (Exception e)
  70. // {
  71. // string reason = _port.PortName + " port close failed。" + e.Message;
  72. // OnErrorHappened(new ErrorEventArgs(reason));
  73. // return false;
  74. // }
  75. // }
  76. // return true;
  77. // }
  78. // public bool Write(string msg)
  79. // {
  80. // try
  81. // {
  82. // lock (_locker)
  83. // {
  84. // if (_port.IsOpen)
  85. // {
  86. // _port.Write(msg);
  87. // LOG.Info(string.Format("Communication {0} Send {1} successed.", _port.PortName, msg));
  88. // }
  89. // }
  90. // return true;
  91. // }
  92. // catch (Exception e)
  93. // {
  94. // string reason = string.Format("Communication {0} Send {1} failed. {2}.", _port.PortName, msg, e.Message);
  95. // LOG.Info(reason);
  96. // OnErrorHappened(new ErrorEventArgs(reason));
  97. // return false;
  98. // }
  99. // }
  100. // public void DataReceived(object sender, SerialDataReceivedEventArgs e)
  101. // {
  102. // if (_port.IsOpen)
  103. // {
  104. // string str = _port.ReadExisting();//字符串方式读
  105. // _buff += str;
  106. // int index = _buff.LastIndexOf(_port.NewLine);
  107. // if (index > 0)
  108. // {
  109. // index += _port.NewLine.Length;
  110. // string msg = _buff.Substring(0,index);
  111. // _buff = _buff.Substring(index);
  112. // LOG.Info(string.Format("Communication {0} Receive {1}.", _port.PortName, msg));
  113. // OnDataChanged(msg);
  114. // }
  115. // }
  116. // }
  117. // void ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
  118. // {
  119. //string reason = string.Format("Communication {0} {1}.", _port.PortName, e.EventType.ToString());
  120. // LOG.Error(reason);
  121. // OnErrorHappened(new ErrorEventArgs(reason));
  122. // }
  123. // public void ClearPortBuffer()
  124. // {
  125. // _port.DiscardInBuffer();
  126. // _port.DiscardOutBuffer();
  127. // }
  128. // }
  129. }