|
@@ -7,13 +7,19 @@ using MECF.Framework.Common.Utilities;
|
|
|
|
|
|
namespace MECF.Framework.Common.Communications
|
|
|
{
|
|
|
-
|
|
|
+
|
|
|
public class AsyncSerialPort : IDisposable
|
|
|
{
|
|
|
- public string PortName { get { return _port.PortName; } set {
|
|
|
+ public string PortName
|
|
|
{
|
|
|
- _port.PortName = value;
|
|
|
- } } }
|
|
|
+ get { return _port.PortName; }
|
|
|
+ set
|
|
|
+ {
|
|
|
+ {
|
|
|
+ _port.PortName = value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
public event Action<string> OnErrorHappened;
|
|
|
|
|
@@ -37,7 +43,7 @@ namespace MECF.Framework.Common.Communications
|
|
|
}
|
|
|
private bool _isLineBased;
|
|
|
|
|
|
- public AsyncSerialPort(string name, int baudRate, int dataBits, Parity parity = Parity.None, StopBits stopBits = StopBits.One, string newline = "\r", bool isAsciiMode=true)
|
|
|
+ public AsyncSerialPort(string name, int baudRate, int dataBits, Parity parity = Parity.None, StopBits stopBits = StopBits.One, string newline = "\r", bool isAsciiMode = true)
|
|
|
{
|
|
|
_isAsciiMode = isAsciiMode;
|
|
|
_isLineBased = !string.IsNullOrEmpty(newline);
|
|
@@ -73,7 +79,7 @@ namespace MECF.Framework.Common.Communications
|
|
|
|
|
|
public bool Open()
|
|
|
{
|
|
|
-
|
|
|
+
|
|
|
if (_port.IsOpen) Close();
|
|
|
//Close();
|
|
|
try
|
|
@@ -87,7 +93,7 @@ namespace MECF.Framework.Common.Communications
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
string reason = _port.PortName + " port open failed,please check configuration." + e.Message;
|
|
|
- //ProcessError( reason );
|
|
|
+ //ProcessError( reason );
|
|
|
LOG.Write(reason);
|
|
|
return false;
|
|
|
}
|
|
@@ -111,7 +117,7 @@ namespace MECF.Framework.Common.Communications
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
string reason = _port.PortName + " port close failed." + e.Message;
|
|
|
- ProcessError( reason );
|
|
|
+ ProcessError(reason);
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
@@ -143,7 +149,7 @@ namespace MECF.Framework.Common.Communications
|
|
|
{
|
|
|
string reason = string.Format("Communication {0} Send {1} failed. {2}.", _port.PortName, msg, e.Message);
|
|
|
LOG.Info(reason);
|
|
|
- ProcessError( reason );
|
|
|
+ ProcessError(reason);
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
@@ -199,7 +205,7 @@ namespace MECF.Framework.Common.Communications
|
|
|
private void AsciiDataReceived()
|
|
|
{
|
|
|
string str = _port.ReadExisting(); //字符串方式读
|
|
|
-
|
|
|
+ LOG.Info($"AsciiDataReceived dataStr:{str}");
|
|
|
if (_isLineBased)
|
|
|
{
|
|
|
_buff += str;
|
|
@@ -241,7 +247,7 @@ namespace MECF.Framework.Common.Communications
|
|
|
|
|
|
}
|
|
|
|
|
|
- public void BinaryDataReceived( )
|
|
|
+ public void BinaryDataReceived()
|
|
|
{
|
|
|
byte[] readBuffer = new byte[_port.BytesToRead];
|
|
|
int readCount = _port.Read(readBuffer, 0, readBuffer.Length);
|
|
@@ -272,7 +278,7 @@ namespace MECF.Framework.Common.Communications
|
|
|
{
|
|
|
string reason = string.Format("Communication {0} {1}.", _port.PortName, e.EventType.ToString());
|
|
|
LOG.Error(reason);
|
|
|
- ProcessError( reason );
|
|
|
+ ProcessError(reason);
|
|
|
}
|
|
|
|
|
|
|