|
@@ -1,4 +1,5 @@
|
|
|
using System;
|
|
|
+using System.Collections.Concurrent;
|
|
|
using System.ComponentModel;
|
|
|
using System.IO.Ports;
|
|
|
using System.Reflection;
|
|
@@ -8,6 +9,7 @@ using System.Security.Permissions;
|
|
|
using System.Text;
|
|
|
using Aitex.Core.RT.Event;
|
|
|
using Aitex.Core.RT.Log;
|
|
|
+using Aitex.Core.RT.SCCore;
|
|
|
using MECF.Framework.Common.Utilities;
|
|
|
using Microsoft.Win32.SafeHandles;
|
|
|
|
|
@@ -40,6 +42,8 @@ namespace MECF.Framework.Common.Communications
|
|
|
private string _buff = "";
|
|
|
|
|
|
public bool EnableLog { get; set; }
|
|
|
+ public ConcurrentQueue<string> SendOrReceiveLog = new ConcurrentQueue<string>();
|
|
|
+ private string _COMLogConfig;
|
|
|
|
|
|
private bool _isAsciiMode;
|
|
|
public bool IsAsciiMode
|
|
@@ -51,6 +55,7 @@ namespace MECF.Framework.Common.Communications
|
|
|
|
|
|
public AsyncSerialPort(string name, int baudRate, int dataBits, Parity parity = Parity.None, StopBits stopBits = StopBits.One, string newline = "\r", bool isAsciiMode = true)
|
|
|
{
|
|
|
+ _COMLogConfig = SC.GetStringValue("System.COMLogFlag");
|
|
|
_isAsciiMode = isAsciiMode;
|
|
|
_isLineBased = !string.IsNullOrEmpty(newline);
|
|
|
|
|
@@ -145,7 +150,7 @@ namespace MECF.Framework.Common.Communications
|
|
|
LOG.Info(string.Format("Communication {0} Send {1} succeeded.", _port.PortName, string.Join(" ", Array.ConvertAll(Encoding.ASCII.GetBytes(msg), x => x.ToString("X2")))));
|
|
|
|
|
|
}
|
|
|
-
|
|
|
+ AppendSendOrReceiveLog("Write", string.Join(" ", Array.ConvertAll(Encoding.ASCII.GetBytes(msg), x => x.ToString("X2"))));
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
@@ -175,6 +180,7 @@ namespace MECF.Framework.Common.Communications
|
|
|
LOG.Info(string.Format("Communication {0} Send {1} succeeded.", _port.PortName, string.Join(" ", Array.ConvertAll(msg, x => x.ToString("X2")))));
|
|
|
LOG.Info(string.Format("Communication {0} Send {1} succeeded.", _port.PortName, Encoding.ASCII.GetString(msg)));
|
|
|
}
|
|
|
+ AppendSendOrReceiveLog("Write", string.Join(" ", Array.ConvertAll(msg, x => x.ToString("X2"))));
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
@@ -234,6 +240,8 @@ namespace MECF.Framework.Common.Communications
|
|
|
|
|
|
if (OnDataChanged != null)
|
|
|
OnDataChanged(msg);
|
|
|
+
|
|
|
+ AppendSendOrReceiveLog("Read", string.Join(" ", Array.ConvertAll(Encoding.ASCII.GetBytes(msg), x => x.ToString("X2"))));
|
|
|
}
|
|
|
}
|
|
|
else
|
|
@@ -245,9 +253,10 @@ namespace MECF.Framework.Common.Communications
|
|
|
LOG.Info(string.Format("Communication {0} Receive {1}.", _port.PortName, string.Join(" ", Array.ConvertAll(Encoding.ASCII.GetBytes(msg), x => x.ToString("X2")))));
|
|
|
|
|
|
}
|
|
|
-
|
|
|
if (OnDataChanged != null)
|
|
|
OnDataChanged(msg);
|
|
|
+
|
|
|
+ AppendSendOrReceiveLog("Read", string.Join(" ", Array.ConvertAll(Encoding.ASCII.GetBytes(msg), x => x.ToString("X2"))));
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -274,9 +283,10 @@ namespace MECF.Framework.Common.Communications
|
|
|
LOG.Info(string.Format("Communication {0} Receive {1}.", _port.PortName, Encoding.ASCII.GetString(buffer)));
|
|
|
|
|
|
}
|
|
|
-
|
|
|
if (OnBinaryDataChanged != null)
|
|
|
OnBinaryDataChanged(buffer);
|
|
|
+
|
|
|
+ AppendSendOrReceiveLog("Read", string.Join(" ", Array.ConvertAll(buffer, x => x.ToString("X2"))));
|
|
|
}
|
|
|
|
|
|
void ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
|
|
@@ -342,6 +352,28 @@ namespace MECF.Framework.Common.Communications
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
+ private void AppendSendOrReceiveLog(string title, string content)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrWhiteSpace(_COMLogConfig) || !_COMLogConfig.Contains(_port.PortName)) return;
|
|
|
+
|
|
|
+ var count = SendOrReceiveLog.Count;
|
|
|
+ for (var i = count; i > 400; i--)
|
|
|
+ {
|
|
|
+ SendOrReceiveLog.TryDequeue(out string _);
|
|
|
+ }
|
|
|
+ SendOrReceiveLog.Enqueue($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} {_port?.PortName} {title ?? string.Empty} [{content ?? string.Empty}]");
|
|
|
+ }
|
|
|
+ public void ClearSendOrReceiveLog()
|
|
|
+ {
|
|
|
+ if (!SendOrReceiveLog.IsEmpty)
|
|
|
+ {
|
|
|
+ var count = SendOrReceiveLog.Count;
|
|
|
+ for (var i = 0; i < count; i++)
|
|
|
+ {
|
|
|
+ SendOrReceiveLog.TryDequeue(out string _);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
internal static class SerialPortExtensions
|