首次上传
This commit is contained in:
+435
@@ -0,0 +1,435 @@
|
||||
using Aitex.Core.Common;
|
||||
using Aitex.Core.RT.DataCenter;
|
||||
using Aitex.Core.RT.Event;
|
||||
using Aitex.Core.RT.OperationCenter;
|
||||
using Aitex.Core.RT.SCCore;
|
||||
using Aitex.Core.Util;
|
||||
using MECF.Framework.Common.Equipment;
|
||||
using MECF.Framework.Common.SubstrateTrackings;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.SMIFs.Fortrend
|
||||
{
|
||||
public class FortrendSMIF : SMIFBase
|
||||
{
|
||||
private PeriodicJob _monitorJob;
|
||||
|
||||
public FortrendSMIF(string module, XmlElement node, string ioModule = "")
|
||||
{
|
||||
var attrModule = node.GetAttribute("module");
|
||||
Module = string.IsNullOrEmpty(attrModule) ? module : attrModule;
|
||||
Name = node.GetAttribute("id");
|
||||
Display = node.GetAttribute("display");
|
||||
DeviceID = node.GetAttribute("schematicId");
|
||||
|
||||
_diReady = ParseDiNode("diReady", node, ioModule);
|
||||
_diPodInPlace = ParseDiNode("diPodInPlace", node, ioModule);
|
||||
//暂时无用处
|
||||
//_diAlarm = ParseDiNode("diAlarm", node, ioModule);
|
||||
//_diReadyToUnload = ParseDiNode("diReadyToUnload", node, ioModule);
|
||||
|
||||
DoUnloadEnable = ParseDoNode("doUnloadEnable", node, ioModule);
|
||||
DoStartRun = ParseDoNode("doStartRun", node, ioModule);
|
||||
DoPortLock = ParseDoNode("doPodLock", node, ioModule);
|
||||
DoLoadEnable = ParseDoNode("doLoadEnable", node, ioModule);
|
||||
DoHomeEnable = ParseDoNode("doHomeEnable", node, ioModule);
|
||||
|
||||
if (SC.GetStringValue("System.InchType") == "Inch6")
|
||||
{
|
||||
_diCassetteExist = ParseDiNode("diCassette6", node, ioModule);
|
||||
}
|
||||
else
|
||||
{
|
||||
_diCassetteExist = ParseDiNode("diCassette8", node, ioModule);
|
||||
}
|
||||
|
||||
DataSubscribe();
|
||||
|
||||
OpSubscribe();
|
||||
}
|
||||
|
||||
public override bool Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
if (IsInstalled)
|
||||
{
|
||||
_monitorJob = new PeriodicJob(1000, OnMonitor, $"{Module}Monitior", true);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void DataSubscribe()
|
||||
{
|
||||
DATA.Subscribe($"{Module}.Mode", () => Mode);
|
||||
DATA.Subscribe($"{Module}.IsSMIFReady", () => IsSMIFReady);
|
||||
DATA.Subscribe($"{Module}.IsSMIFAlarm", () => IsSMIFAlarm);
|
||||
DATA.Subscribe($"{Module}.IsHomed", () => IsHomed);
|
||||
DATA.Subscribe($"{Module}.IsPodPresent", () => IsPodPresent);
|
||||
DATA.Subscribe($"{Module}.PortStatus", () => PortStatus);
|
||||
}
|
||||
|
||||
private void OpSubscribe()
|
||||
{
|
||||
OP.Subscribe($"{Module}.SwitchMode", InvokeSwitchMode);
|
||||
}
|
||||
|
||||
public override void OnReceived(string receiveMsg)
|
||||
{
|
||||
//从服务器收到信息
|
||||
string msg = receiveMsg.Replace(Delimiter, "");
|
||||
|
||||
if (!msg.StartsWith("FSD"))
|
||||
EV.PostInfoLog(Module, $"Receive {msg}");
|
||||
|
||||
if (msg.StartsWith("HCA"))
|
||||
{
|
||||
string result = msg.Replace("HCA ", "");
|
||||
|
||||
switch (result)
|
||||
{
|
||||
case "OK":
|
||||
{
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case "DENIED":
|
||||
{
|
||||
EV.PostAlarmLog(Module, "Command rejected not recognized or invalid parameter.");
|
||||
}
|
||||
break;
|
||||
|
||||
case "BUSY":
|
||||
{
|
||||
EV.PostAlarmLog(Module, "Command rejected, PLUS is busy.");
|
||||
}
|
||||
break;
|
||||
|
||||
case "CANNOT_PERFORM":
|
||||
{
|
||||
EV.PostAlarmLog(Module, "Command rejected, PLUS not in the proper state to perform the The Host command. PLUS is busy, PLUS in alarm status or prepositioning requirements not met.");
|
||||
}
|
||||
break;
|
||||
|
||||
case "NO_POD":
|
||||
{
|
||||
EV.PostAlarmLog(Module, "Command rejected, no pod has been placed on PLUS port plate.");
|
||||
}
|
||||
break;
|
||||
|
||||
case "NOT_READY":
|
||||
{
|
||||
EV.PostAlarmLog(Module, "Command rejected, all host ready conditions or interlocks have not been satisfied.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
IsDone = true;
|
||||
}
|
||||
else if (msg.StartsWith("AERS"))
|
||||
{
|
||||
//事件
|
||||
string eventMsg = msg.Replace("AERS ", "");
|
||||
if (eventMsg == "REACH_HOME")
|
||||
{
|
||||
IsHomeDone = true;
|
||||
}
|
||||
else if (eventMsg == "CMPL_LOAD")
|
||||
{
|
||||
IsLoadDone = true;
|
||||
}
|
||||
else if (eventMsg == "CMPL_UNLOAD")
|
||||
{
|
||||
IsUnloadDone = true;
|
||||
}
|
||||
else if (eventMsg == "CMPL_LOCK")
|
||||
{
|
||||
IsLockDone = true;
|
||||
}
|
||||
else if (eventMsg == "CMPL_UNLOCK")
|
||||
{
|
||||
IsUnLockDone = true;
|
||||
}
|
||||
else if (eventMsg == "AUTO_MODE")
|
||||
{
|
||||
Mode = "AUTO";
|
||||
IsSwitchDone = true;
|
||||
}
|
||||
else if (eventMsg == "MANUAL_MODE")
|
||||
{
|
||||
Mode = "MANUAL";
|
||||
IsSwitchDone = true;
|
||||
}
|
||||
else if (eventMsg.Contains("WAFER_MAPPING_EVT"))
|
||||
{
|
||||
//Mapping结果事件
|
||||
string mapping = eventMsg.Replace("WAFER_MAPPING_EVT ", "");
|
||||
ModuleName cassModuleName = Module == "SMIFA" ? ModuleName.CassAL : ModuleName.CassAR;
|
||||
for (int i = 0; i < mapping.Length; i++)
|
||||
{
|
||||
if (int.TryParse(mapping[i].ToString(), out int result))
|
||||
{
|
||||
//先将对应Wafer删除
|
||||
WaferManager.Instance.DeleteWafer(cassModuleName, i);
|
||||
|
||||
//0-Full Status(未知状态) 1-Wafer Present 2-Empty 4-Cross Slotted
|
||||
switch (result)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
WaferManager.Instance.CreateWafer(cassModuleName, i, WaferStatus.Unknown);
|
||||
}
|
||||
break;
|
||||
|
||||
case 1:
|
||||
{
|
||||
WaferManager.Instance.CreateWafer(cassModuleName, i, WaferStatus.Normal);
|
||||
}
|
||||
break;
|
||||
|
||||
case 2:
|
||||
{
|
||||
WaferManager.Instance.DeleteWafer(cassModuleName, i);
|
||||
}
|
||||
break;
|
||||
|
||||
case 4:
|
||||
{
|
||||
WaferManager.Instance.CreateWafer(cassModuleName, i, WaferStatus.Crossed);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (msg.StartsWith("ARS"))
|
||||
{
|
||||
//报警 ARS XXYY ZZZZZZ(CR)(LF)
|
||||
string alarm = msg.Replace("ARS ", "");
|
||||
int index = alarm.IndexOf(' ');
|
||||
string alarmCode = alarm.Substring(0, index);
|
||||
string alarmText = alarm.Substring(index + 1);
|
||||
|
||||
EV.PostAlarmLog(Module, $"{Module} Alarm,Alarm Code is {alarmCode},Alarm Text: {alarmText}");
|
||||
}
|
||||
//状态查询结果
|
||||
else if (msg.StartsWith("FSD"))
|
||||
{
|
||||
IsDone = true;
|
||||
|
||||
string query = msg.Replace("FSD", "");
|
||||
if (int.TryParse(query.Substring(0, 1), out int formCode))
|
||||
{
|
||||
if (formCode == 0)
|
||||
{
|
||||
//general status
|
||||
List<string> resultList = msg.Replace("FSD0 ", "").Split(' ').ToList();
|
||||
foreach (string item in resultList)
|
||||
{
|
||||
if (item.StartsWith("MODE"))
|
||||
{
|
||||
Mode = item.Replace("MODE=", "");
|
||||
}
|
||||
//else if (item.StartsWith("READY"))
|
||||
//{
|
||||
// if (bool.TryParse(item.Replace("READY=", ""), out bool ready))
|
||||
// {
|
||||
// IsSMIFReady = ready;
|
||||
// }
|
||||
//}
|
||||
else if (item.StartsWith("HOME"))
|
||||
{
|
||||
if (bool.TryParse(item.Replace("HOME=", ""), out bool home))
|
||||
{
|
||||
IsHomed = home;
|
||||
}
|
||||
}
|
||||
//else if (item.StartsWith("PIP"))
|
||||
//{
|
||||
// if (bool.TryParse(item.Replace("PIP=", ""), out bool isPodPresent))
|
||||
// {
|
||||
// IsPodPresent = isPodPresent;
|
||||
// }
|
||||
//}
|
||||
else if (item.StartsWith("PRTST"))
|
||||
{
|
||||
PortStatus = item.Replace("PRTST=", "");
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (formCode == 1)
|
||||
{
|
||||
//software part number
|
||||
}
|
||||
else if (formCode == 2)
|
||||
{
|
||||
//Cassette map status
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
if (!IsSMIFReady)
|
||||
{
|
||||
ClearCmdQueue();
|
||||
IsDone = true;
|
||||
SMIFReset();
|
||||
}
|
||||
|
||||
base.Reset();
|
||||
}
|
||||
|
||||
public void SMIFReset()
|
||||
{
|
||||
RemoteCommand("RESET");
|
||||
}
|
||||
|
||||
public void Home()
|
||||
{
|
||||
//if (!DoHomeEnable.SetValue(true, out string reason))
|
||||
//{
|
||||
// EV.PostWarningLog(Module, reason);
|
||||
// return;
|
||||
//}
|
||||
|
||||
IsHomeDone = false;
|
||||
RemoteCommand("HOME");
|
||||
}
|
||||
|
||||
private bool InvokeSwitchMode(string arg1, object[] arg2)
|
||||
{
|
||||
SwitchMode(arg2[0].ToString());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SwitchMode(string mode)
|
||||
{
|
||||
IsSwitchDone = false;
|
||||
RemoteCommand(mode);
|
||||
}
|
||||
|
||||
public void PortLock()
|
||||
{
|
||||
IsLockDone = false;
|
||||
//RemoteCommand("LOCK");
|
||||
|
||||
if (!DoPortLock.SetValue(true, out string reason))
|
||||
{
|
||||
EV.PostWarningLog(Module, reason);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void PortUnLock()
|
||||
{
|
||||
IsUnLockDone = false;
|
||||
//RemoteCommand("UNLOCK");
|
||||
|
||||
if (!DoPortLock.SetValue(false, out string reason))
|
||||
{
|
||||
EV.PostWarningLog(Module, reason);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void Load()
|
||||
{
|
||||
if (!IsHomed)
|
||||
{
|
||||
EV.PostAlarmLog(Module, "SMIF has not home!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_diCassetteExist.Value)
|
||||
{
|
||||
EV.PostAlarmLog(Module, "Cassette Exist,can not Load!");
|
||||
return;
|
||||
}
|
||||
|
||||
IsLoadDone = false;
|
||||
//RemoteCommand("LOAD");
|
||||
|
||||
if (!DoStartRun.SetValue(true, out string reason))
|
||||
{
|
||||
EV.PostWarningLog(Module, reason);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void Unload()
|
||||
{
|
||||
if (!IsHomed)
|
||||
{
|
||||
EV.PostAlarmLog(Module, "SMIF has not home!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_diCassetteExist.Value)
|
||||
{
|
||||
EV.PostAlarmLog(Module, "Cassette not Exist,can not Unload!");
|
||||
return;
|
||||
}
|
||||
|
||||
IsUnloadDone = false;
|
||||
//RemoteCommand("UNLOAD");
|
||||
|
||||
if (!DoStartRun.SetValue(true, out string reason))
|
||||
{
|
||||
EV.PostWarningLog(Module, reason);
|
||||
}
|
||||
}
|
||||
|
||||
public void CloseDoStart()
|
||||
{
|
||||
if (!DoStartRun.SetValue(false, out string reason))
|
||||
{
|
||||
EV.PostWarningLog(Module, reason);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void QueryState(int code)
|
||||
{
|
||||
AddActionToQueue($"FSR FC={code}\r\n");
|
||||
}
|
||||
|
||||
private bool OnMonitor()
|
||||
{
|
||||
if (IsConnected && IsDone)
|
||||
QueryState(0);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Abort()
|
||||
{
|
||||
ClearCmdQueue();
|
||||
IsDone = true;
|
||||
}
|
||||
|
||||
//远程命令
|
||||
public void RemoteCommand(string command,string param = "")
|
||||
{
|
||||
if (IsConnected)
|
||||
{
|
||||
string msg = $"HCS {command}";
|
||||
if (!string.IsNullOrEmpty(param))
|
||||
{
|
||||
msg += $" {param}";
|
||||
}
|
||||
msg += "\r\n";
|
||||
|
||||
AddActionToQueue(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+217
@@ -0,0 +1,217 @@
|
||||
using Aitex.Core.RT.Device;
|
||||
using Aitex.Core.RT.Event;
|
||||
using Aitex.Core.RT.IOCore;
|
||||
using Aitex.Core.RT.Log;
|
||||
using Aitex.Core.RT.SCCore;
|
||||
using Aitex.Core.Util;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.SMIFs.Fortrend
|
||||
{
|
||||
public abstract class SMIFBase : BaseDevice, IDevice
|
||||
{
|
||||
protected bool IsInstalled;
|
||||
|
||||
private string _scRoot;
|
||||
|
||||
private bool _enableLog;
|
||||
|
||||
//完成标志
|
||||
public bool IsDone = true;
|
||||
|
||||
public bool IsHomeDone;
|
||||
|
||||
public bool IsLoadDone;
|
||||
|
||||
public bool IsUnloadDone;
|
||||
|
||||
public bool IsLockDone;
|
||||
|
||||
public bool IsUnLockDone;
|
||||
|
||||
public bool IsSwitchDone;
|
||||
|
||||
public bool IsCassetteExist => _diCassetteExist != null ? _diCassetteExist.Value : false;
|
||||
|
||||
public string Mode;
|
||||
|
||||
public string PortStatus;
|
||||
|
||||
public bool IsPodPresent => _diPodInPlace != null ? _diPodInPlace.Value : false;
|
||||
|
||||
public bool IsSMIFReady => _diReady != null ? _diReady.Value : false;
|
||||
|
||||
public bool IsSMIFAlarm => _diAlarm != null ? _diAlarm.Value : true;
|
||||
|
||||
public bool IsHomed;
|
||||
|
||||
//当前错误码
|
||||
public int CurErrorCode;
|
||||
|
||||
protected DIAccessor _diReady = null;
|
||||
|
||||
protected DIAccessor _diAlarm = null;
|
||||
|
||||
protected DIAccessor _diPodInPlace = null;
|
||||
|
||||
protected DIAccessor _diReadyToUnload = null;
|
||||
|
||||
protected DIAccessor _diCassetteExist = null;
|
||||
|
||||
public DOAccessor DoUnloadEnable = null;
|
||||
|
||||
public DOAccessor DoStartRun = null;
|
||||
|
||||
public DOAccessor DoPortLock = null;
|
||||
|
||||
public DOAccessor DoLoadEnable = null;
|
||||
|
||||
public DOAccessor DoHomeEnable = null;
|
||||
|
||||
private SerialPortEx _serialPort;
|
||||
|
||||
public bool IsConnected => _serialPort != null ? _serialPort.IsOpen : false;
|
||||
|
||||
private R_TRIG _trigger = new R_TRIG();
|
||||
|
||||
public string Delimiter => _serialPort?.Delimiter;
|
||||
|
||||
|
||||
private readonly object _locker = new object();
|
||||
|
||||
private ConcurrentQueue<string> _actionQueue = new ConcurrentQueue<string>();
|
||||
|
||||
public SMIFBase()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public SMIFBase(string module, string name)
|
||||
{
|
||||
Module = module;
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public virtual bool Initialize()
|
||||
{
|
||||
IsInstalled = SC.GetValue<bool>($"System.SetUp.Is{Module}Installed");
|
||||
|
||||
if(IsInstalled )
|
||||
{
|
||||
InitSerialPort();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void InitSerialPort()
|
||||
{
|
||||
try
|
||||
{
|
||||
string portName = string.Empty;
|
||||
if (string.IsNullOrEmpty(_scRoot))
|
||||
{
|
||||
portName = SC.GetStringValue($"{Name}.PortName");
|
||||
_enableLog = SC.GetValue<bool>($"{Name}.EnableLogMessage");
|
||||
}
|
||||
else
|
||||
{
|
||||
portName = SC.GetStringValue($"{_scRoot}.{Module}.{Name}.PortName");
|
||||
_enableLog = SC.GetValue<bool>($"{_scRoot}.{Module}.{Name}.EnableLogMessage");
|
||||
}
|
||||
|
||||
_serialPort = new SerialPortEx(portName);
|
||||
_serialPort.Delimiter = "\r\n";
|
||||
_serialPort.OnReceivedEvent += OnReceived;
|
||||
|
||||
_serialPort.Connect();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
EV.PostAlarmLog(Module, $"{Module} connect IP:{_serialPort.PortName} failed");
|
||||
LOG.Error(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void OnReceived(string msg)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected void AddActionToQueue(string msg)
|
||||
{
|
||||
if (IsConnected)
|
||||
_actionQueue.Enqueue(msg);
|
||||
}
|
||||
|
||||
protected void ClearCmdQueue()
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
//ConcurrentQueue<T>是线程安全的,所以它不会暴露或提供Clear方法清空
|
||||
while (_actionQueue.TryDequeue(out _))
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void HandleMonitor()
|
||||
{
|
||||
if(!IsInstalled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//断连后将指令队列清空
|
||||
_trigger.CLK = !_serialPort.IsOpen;
|
||||
if (_trigger.Q)
|
||||
{
|
||||
ClearCmdQueue();
|
||||
EV.PostAlarmLog(Module, $"{_serialPort.PortName} Disconnected!");
|
||||
}
|
||||
|
||||
lock (_locker)
|
||||
{
|
||||
//连接成功 && SMIF不是Alarm && SMIF 空闲状态
|
||||
if (_serialPort != null && _serialPort.IsOpen /*&& IsSMIFReady*/ && IsDone)
|
||||
{
|
||||
if (_actionQueue.Count > 0 && _actionQueue.TryDequeue(out string msg))
|
||||
{
|
||||
IsDone = false;
|
||||
SendMessage(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SendMessage(string msg)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (IsConnected)
|
||||
{
|
||||
_serialPort.Send(msg);
|
||||
|
||||
if (!msg.StartsWith("FSR"))
|
||||
EV.PostInfoLog(Module, $"向{Name}发送指令:{msg.Replace("\r", "").Replace("\n", "")}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Error(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Reset()
|
||||
{
|
||||
_serialPort?.Reset();
|
||||
}
|
||||
|
||||
public virtual void Terminate()
|
||||
{
|
||||
_serialPort?.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
+303
@@ -0,0 +1,303 @@
|
||||
using Aitex.Core.RT.Log;
|
||||
using Aitex.Core.Util;
|
||||
using System;
|
||||
using System.IO.Ports;
|
||||
using System.Text;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.SMIFs.Fortrend
|
||||
{
|
||||
public class SerialPortEx
|
||||
{
|
||||
#region 变量属性
|
||||
private bool _retryConnect = true;
|
||||
|
||||
private SerialPort _serialPort = new SerialPort();
|
||||
|
||||
public event Action<string> OnReceivedEvent;
|
||||
|
||||
//串口号
|
||||
private string _portName = "COM1";
|
||||
//波特率
|
||||
private int _baudRate = 9600;
|
||||
//校验位
|
||||
private Parity _parity = Parity.None;
|
||||
//停止位
|
||||
private StopBits _stopBits = StopBits.One;
|
||||
//数据位
|
||||
private int _dataBits = 8;
|
||||
|
||||
//分隔符
|
||||
public string Delimiter = "\r\n";
|
||||
|
||||
private StringBuilder _buffer = new StringBuilder();
|
||||
|
||||
private PeriodicJob _monitorThread;
|
||||
|
||||
/// <summary>
|
||||
/// 串口号
|
||||
/// </summary>
|
||||
public string PortName
|
||||
{
|
||||
get { return _portName; }
|
||||
set { _portName = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 波特率
|
||||
/// </summary>
|
||||
public int BaudRate
|
||||
{
|
||||
get { return _baudRate; }
|
||||
set { _baudRate = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 奇偶校验位
|
||||
/// </summary>
|
||||
public Parity Parity
|
||||
{
|
||||
get { return _parity; }
|
||||
set { _parity = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据位
|
||||
/// </summary>
|
||||
public int DataBits
|
||||
{
|
||||
get { return _dataBits; }
|
||||
set { _dataBits = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 停止位
|
||||
/// </summary>
|
||||
public StopBits StopBits
|
||||
{
|
||||
get { return _stopBits; }
|
||||
set { _stopBits = value; }
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 串口关闭/打开
|
||||
/// <summary>
|
||||
/// 端口是否已经打开
|
||||
/// </summary>
|
||||
public bool IsOpen => _serialPort != null ? _serialPort.IsOpen : false;
|
||||
|
||||
#region 构造函数
|
||||
/// <summary>
|
||||
/// 无参构造函数
|
||||
/// </summary>
|
||||
public SerialPortEx()
|
||||
{
|
||||
BoundEvents();
|
||||
}
|
||||
|
||||
public SerialPortEx(string portName)
|
||||
{
|
||||
this._portName = portName;
|
||||
|
||||
BoundEvents();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 参数构造函数(使用枚举参数构造)
|
||||
/// </summary>
|
||||
/// /// <param name="portName">串口号</param>
|
||||
/// <param name="baudRate">波特率</param>
|
||||
/// <param name="parity">奇偶校验位</param>
|
||||
/// <param name="stopBits">停止位</param>
|
||||
/// <param name="dataBits">数据位</param>
|
||||
|
||||
public SerialPortEx(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits)
|
||||
{
|
||||
this._portName = portName;
|
||||
this._baudRate = baudRate;
|
||||
this._parity = parity;
|
||||
this._dataBits = dataBits;
|
||||
this._stopBits = stopBits;
|
||||
|
||||
BoundEvents();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 参数构造函数(使用字符串参数构造)
|
||||
/// </summary>
|
||||
/// /// <param name="portName">串口号</param>
|
||||
/// <param name="baudRate">波特率</param>
|
||||
/// <param name="parity">奇偶校验位</param>
|
||||
/// <param name="stopBits">停止位</param>
|
||||
/// <param name="dataBits">数据位</param>
|
||||
public SerialPortEx(string portName, int baudRate, string parity, int dataBits, string stopBits)
|
||||
{
|
||||
this._portName = portName;
|
||||
this._baudRate = baudRate;
|
||||
this._dataBits = dataBits;
|
||||
this._parity = (Parity)Enum.Parse(typeof(Parity), parity);
|
||||
this._stopBits = (StopBits)Enum.Parse(typeof(StopBits), stopBits);
|
||||
|
||||
BoundEvents();
|
||||
}
|
||||
|
||||
private void BoundEvents()
|
||||
{
|
||||
_serialPort.DataReceived += new SerialDataReceivedEventHandler(OnDataReceived);
|
||||
_serialPort.ErrorReceived += new SerialErrorReceivedEventHandler(OnErrorReceived);
|
||||
|
||||
_monitorThread = new PeriodicJob(3000, OnMonitor,$"{_serialPort.PortName}", true);
|
||||
}
|
||||
#endregion
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_retryConnect = true;
|
||||
}
|
||||
|
||||
private bool OnMonitor()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_serialPort != null && !_serialPort.IsOpen)
|
||||
{
|
||||
if (_retryConnect)
|
||||
{
|
||||
_retryConnect = false;
|
||||
_serialPort.Open();
|
||||
|
||||
if (_serialPort.IsOpen)
|
||||
{
|
||||
LOG.Info($"Retry Connect PortName : {_serialPort?.PortName} success");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#region 事件处理函数
|
||||
/// <summary>
|
||||
/// 数据接收处理
|
||||
/// </summary>
|
||||
private void OnDataReceived(object sender, SerialDataReceivedEventArgs e)
|
||||
{
|
||||
// 读取所有可用数据
|
||||
string data = _serialPort.ReadExisting();
|
||||
_buffer.Append(data);
|
||||
|
||||
string bufferContent = _buffer.ToString();
|
||||
int delimiterIndex;
|
||||
|
||||
// 循环处理所有完整的消息
|
||||
while ((delimiterIndex = bufferContent.IndexOf(Delimiter)) != -1)
|
||||
{
|
||||
string msg = bufferContent.Substring(0, delimiterIndex + Delimiter.Length);
|
||||
// 移除处理过的数据和分隔符
|
||||
_buffer.Remove(0, delimiterIndex + Delimiter.Length);
|
||||
// 更新缓冲区内容
|
||||
bufferContent = _buffer.ToString();
|
||||
|
||||
// 处理消息(注意跨线程调用)
|
||||
OnReceivedEvent?.Invoke(msg);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 错误处理函数
|
||||
/// </summary>
|
||||
public virtual void OnErrorReceived(object sender, SerialErrorReceivedEventArgs e)
|
||||
{
|
||||
LOG.Error($"{_serialPort.PortName}" + e.ToString());
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 打开端口
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public void Connect()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_serialPort.IsOpen)
|
||||
{
|
||||
_serialPort.Close();
|
||||
}
|
||||
|
||||
_serialPort.PortName = _portName;
|
||||
_serialPort.BaudRate = _baudRate;
|
||||
_serialPort.Parity = _parity;
|
||||
_serialPort.DataBits = _dataBits;
|
||||
_serialPort.StopBits = _stopBits;
|
||||
|
||||
_serialPort.ReadBufferSize = 4096;
|
||||
_serialPort.WriteBufferSize = 4096;
|
||||
|
||||
_serialPort.Open();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Error(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭端口
|
||||
/// </summary>
|
||||
public void Close()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_serialPort.IsOpen)
|
||||
{
|
||||
_serialPort.Close();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Error(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 丢弃来自串行驱动程序的接收和发送缓冲区的数据
|
||||
/// </summary>
|
||||
public void DiscardBuffer()
|
||||
{
|
||||
_serialPort.DiscardInBuffer();
|
||||
_serialPort.DiscardOutBuffer();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 写入数据
|
||||
public bool Send(string sendMsg)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_serialPort.IsOpen)
|
||||
{
|
||||
byte[] sendBytes = Encoding.UTF8.GetBytes(sendMsg);
|
||||
_serialPort.Write(sendBytes, 0, sendBytes.Length);
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG.Error($"{_serialPort.PortName} is not open");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Error(ex.Message,ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user