refactor: 将Framework库和RT、UI放在同一个解决方案中。
This commit is contained in:
@@ -0,0 +1,374 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.Ports;
|
||||
using System.Linq;
|
||||
using Aitex.Core.RT.Event;
|
||||
using Aitex.Core.RT.Log;
|
||||
using Aitex.Core.RT.SCCore;
|
||||
using Aitex.Core.Util;
|
||||
using MECF.Framework.Common.Communications;
|
||||
using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Common;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Pumps.SkyPump
|
||||
{
|
||||
|
||||
|
||||
public class SkyPump : SerialPortDevice, IConnection
|
||||
{
|
||||
public string Address { get { return _address; }}
|
||||
public bool IsConnected { get; }
|
||||
public bool Connect()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Disconnect()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public string PortStatus { get; set; } = "Closed";
|
||||
|
||||
private SkyPumpConnection _connection;
|
||||
public SkyPumpConnection Connection
|
||||
{
|
||||
get { return _connection; }
|
||||
}
|
||||
|
||||
private R_TRIG _trigError = new R_TRIG();
|
||||
|
||||
private R_TRIG _trigCommunicationError = new R_TRIG();
|
||||
private R_TRIG _trigRetryConnect = new R_TRIG();
|
||||
|
||||
private PeriodicJob _thread;
|
||||
|
||||
private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
|
||||
private LinkedList<HandlerBase> _lstMonitorHandler = new LinkedList<HandlerBase>();
|
||||
|
||||
public List<IOResponse> IOResponseList { get; set; } = new List<IOResponse>();
|
||||
|
||||
|
||||
private object _locker = new object();
|
||||
|
||||
private bool _enableLog;
|
||||
private string _address;
|
||||
private string _scRoot;
|
||||
public SkyPump(string module, string name, string scRoot, string portName) : base(module, name)
|
||||
{
|
||||
_scRoot = scRoot;
|
||||
PortName = portName;
|
||||
}
|
||||
private void ResetPropertiesAndResponses()
|
||||
{
|
||||
|
||||
foreach (var ioResponse in IOResponseList)
|
||||
{
|
||||
ioResponse.ResonseContent = null;
|
||||
ioResponse.ResonseRecievedTime = DateTime.Now;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool Initialize(string portName)
|
||||
{
|
||||
base.Initialize(portName);
|
||||
|
||||
ResetPropertiesAndResponses();
|
||||
|
||||
if (_connection != null && _connection.IsConnected && PortName == portName)
|
||||
return true;
|
||||
|
||||
if (_connection != null && _connection.IsConnected)
|
||||
_connection.Disconnect();
|
||||
|
||||
PortName = portName;
|
||||
|
||||
_address = SC.GetStringValue($"{_scRoot}.{Module}.{Name}.DeviceAddress");
|
||||
_enableLog = SC.GetValue<bool>($"{_scRoot}.{Module}.{Name}.EnableLogMessage");
|
||||
_connection = new SkyPumpConnection(PortName);
|
||||
_connection.EnableLog(_enableLog);
|
||||
|
||||
if (_connection.Connect())
|
||||
{
|
||||
PortStatus = "Open";
|
||||
EV.PostInfoLog(Module, $"{Module}.{Name} connected");
|
||||
}
|
||||
|
||||
_thread = new PeriodicJob(100, OnTimer, $"{Module}.{Name} MonitorHandler", true);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool InitConnection(string portName, int bautRate, int dataBits, Parity parity, StopBits stopBits)
|
||||
{
|
||||
|
||||
_connection = new SkyPumpConnection(portName, bautRate, dataBits, parity, stopBits);
|
||||
|
||||
if (_connection.Connect())
|
||||
{
|
||||
EV.PostInfoLog(Module, $"{Module}.{Name} connected");
|
||||
}
|
||||
|
||||
_thread = new PeriodicJob(100, OnTimer, $"{Module}.{Name} MonitorHandler", true);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool OnTimer()
|
||||
{
|
||||
try
|
||||
{
|
||||
//_connection.MonitorTimeout();
|
||||
|
||||
if (!_connection.IsConnected || _connection.IsCommunicationError)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
_lstHandler.Clear();
|
||||
}
|
||||
|
||||
_trigRetryConnect.CLK = !_connection.IsConnected;
|
||||
if (_trigRetryConnect.Q)
|
||||
{
|
||||
_connection.SetPortAddress(SC.GetStringValue($"{ScBasePath}.{Name}.Address"));
|
||||
if (!_connection.Connect())
|
||||
{
|
||||
EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
|
||||
}
|
||||
else
|
||||
{
|
||||
//_lstHandler.AddLast(new SkyPumpQueryPinHandler(this, _deviceAddress));
|
||||
//_lstHandler.AddLast(new SkyPumpSetCommModeHandler(this, _deviceAddress, EnumRfPowerCommunicationMode.Host));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
HandlerBase handler = null;
|
||||
if (!_connection.IsBusy)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
if (_lstHandler.Count == 0)
|
||||
{
|
||||
foreach (var monitorHandler in _lstMonitorHandler)
|
||||
{
|
||||
_lstHandler.AddLast(monitorHandler);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (_lstHandler.Count > 0)
|
||||
{
|
||||
handler = _lstHandler.First.Value;
|
||||
|
||||
_lstHandler.RemoveFirst();
|
||||
}
|
||||
}
|
||||
|
||||
if (handler != null)
|
||||
{
|
||||
_connection.Execute(handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Write(ex);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public override void Monitor()
|
||||
{
|
||||
try
|
||||
{
|
||||
//_connection.EnableLog(_enableLog);
|
||||
|
||||
|
||||
_trigCommunicationError.CLK = _connection.IsCommunicationError;
|
||||
if (_trigCommunicationError.Q)
|
||||
{
|
||||
EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Write(ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
_trigError.RST = true;
|
||||
|
||||
_connection.SetCommunicationError(false, "");
|
||||
_trigCommunicationError.RST = true;
|
||||
|
||||
//_enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");
|
||||
|
||||
_trigRetryConnect.RST = true;
|
||||
|
||||
base.Reset();
|
||||
}
|
||||
|
||||
public override bool Home(out string reason)
|
||||
{
|
||||
return base.Home(out reason);
|
||||
}
|
||||
|
||||
|
||||
#region Command Functions
|
||||
public void PerformRawCommand(string command, string completeEvent, string comandArgument)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
_lstHandler.AddLast(new SkyPumpRawCommandHandler(this, command, completeEvent));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void Start()
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
_lstHandler.AddLast(new SkyPumpStartHandler(this));
|
||||
}
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
_lstHandler.AddLast(new SkyPumpStopHandler(this));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
internal void NoteStart(bool value)
|
||||
{
|
||||
PumpStarted = value;
|
||||
}
|
||||
|
||||
public void MonitorRawCommand(bool isSelected, string command, string completeEvent,string parameter)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(SkyPumpRawCommandHandler) && ((SkyPumpHandler)handler)._command == command);
|
||||
if (isSelected)
|
||||
{
|
||||
if (!existHandlers.Any())
|
||||
_lstMonitorHandler.AddFirst(new SkyPumpRawCommandHandler(this, command, completeEvent));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (existHandlers.Any())
|
||||
{
|
||||
_lstMonitorHandler.Remove(existHandlers.First());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void ReadRunParameters(bool isSelected)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(SkyPumpReadRunParaHandler));
|
||||
if (isSelected)
|
||||
{
|
||||
if (!existHandlers.Any())
|
||||
_lstMonitorHandler.AddFirst(new SkyPumpReadRunParaHandler(this));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (existHandlers.Any())
|
||||
{
|
||||
_lstMonitorHandler.Remove(existHandlers.First());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
public string Error { get; private set; }
|
||||
public bool PumpStarted { get; private set; }
|
||||
public string FDP_Current { get; private set; }
|
||||
public string ROOTS_Current { get; private set; }
|
||||
public string FDP_Temp { get; private set; }
|
||||
public string ROOTS_Temp { get; private set; }
|
||||
public string N2_Flow { get; private set; }
|
||||
public string Pressure { get; private set; }
|
||||
public string RunTime { get; private set; }
|
||||
public string ErrSta1 { get; private set; }
|
||||
public string ErrSta2 { get; private set; }
|
||||
public string RunSta1 { get; private set; }
|
||||
public string RunSta2 { get; private set; }
|
||||
public string ErrSta3 { get; private set; }
|
||||
|
||||
internal void NoteRunPara(string data)
|
||||
{
|
||||
var str = data.ToArray();
|
||||
FDP_Current = data.Substring(11, 2);
|
||||
ROOTS_Current = data.Substring(13, 2);
|
||||
FDP_Temp = data.Substring(15, 3);
|
||||
ROOTS_Temp = data.Substring(18, 3);
|
||||
N2_Flow = data.Substring(21, 2);
|
||||
Pressure = data.Substring(23, 4);
|
||||
RunTime = data.Substring(27, 5);
|
||||
ErrSta1 = data.Substring(32, 1);
|
||||
ErrSta2 = data.Substring(33, 1);
|
||||
RunSta1 = data.Substring(34, 1);
|
||||
RunSta2 = data.Substring(35, 1);
|
||||
ErrSta3 = data.Substring(36, 1);
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Note Functions
|
||||
private R_TRIG _trigWarningMessage = new R_TRIG();
|
||||
|
||||
|
||||
|
||||
public void NoteError(string reason)
|
||||
{
|
||||
if (reason != null)
|
||||
{
|
||||
_trigWarningMessage.CLK = true;
|
||||
if (_trigWarningMessage.Q)
|
||||
{
|
||||
EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
|
||||
}
|
||||
Error = reason;
|
||||
}
|
||||
else
|
||||
{
|
||||
Error = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal void NoteRawCommandInfo(string command, string data)
|
||||
{
|
||||
var curIOResponse = IOResponseList.Find(res => res.SourceCommandName == command);
|
||||
if (curIOResponse != null)
|
||||
{
|
||||
IOResponseList.Remove(curIOResponse);
|
||||
}
|
||||
IOResponseList.Add(new IOResponse() { SourceCommand = command, ResonseContent = data, ResonseRecievedTime = DateTime.Now });
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
}
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
using System.IO.Ports;
|
||||
using System.Linq;
|
||||
using Aitex.Core.RT.Log;
|
||||
using MECF.Framework.Common.Communications;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Pumps.SkyPump
|
||||
{
|
||||
public class SkyPumpMessage : AsciiMessage
|
||||
{
|
||||
public string Data { get; set; }
|
||||
public string Address { get; set; }
|
||||
}
|
||||
|
||||
public class SkyPumpConnection : SerialPortConnectionBase
|
||||
{
|
||||
private static string _endLine = "\r\n";
|
||||
|
||||
private string _cachedBuffer = string.Empty;
|
||||
|
||||
public SkyPumpConnection(string portName, int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
|
||||
: base(portName, baudRate, dataBits, parity, stopBits, _endLine, true)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override bool SendMessage(string message)
|
||||
{
|
||||
return base.SendMessage(message);
|
||||
}
|
||||
|
||||
protected override MessageBase ParseResponse(string rawText)
|
||||
{
|
||||
SkyPumpMessage msg = new SkyPumpMessage();
|
||||
msg.RawMessage = rawText;
|
||||
|
||||
if (rawText.Length <= 0)
|
||||
{
|
||||
LOG.Error($"empty response,");
|
||||
msg.IsFormatError = true;
|
||||
return msg;
|
||||
}
|
||||
if (rawText.Length <= 4)
|
||||
{
|
||||
LOG.Error($"too short response,");
|
||||
msg.IsFormatError = true;
|
||||
return msg;
|
||||
}
|
||||
rawText = rawText.Substring(1, rawText.Length - 4);//remove start char and end char
|
||||
msg.Address = rawText.Substring(0,2);
|
||||
msg.Data = rawText.Substring(2);
|
||||
msg.IsResponse = true;
|
||||
msg.IsAck = true;
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
private bool CheckSum(string responseContent)
|
||||
{
|
||||
string responseSum = responseContent.Substring(responseContent.Length - 2);
|
||||
string checkContent = responseContent.Substring(0, responseContent.Length - 2);
|
||||
var charArray = checkContent.ToArray();
|
||||
int sum = charArray.Sum(chr => (int)chr);
|
||||
string sSum = sum.ToString("X");
|
||||
|
||||
if (sSum.Length < 2)
|
||||
sSum = "0" + sSum;
|
||||
else
|
||||
sSum = sSum.Substring(sSum.Length - 2);
|
||||
|
||||
return responseSum == sSum;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
using MECF.Framework.Common.Communications;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Pumps.SkyPump
|
||||
{
|
||||
public abstract class SkyPumpHandler : HandlerBase
|
||||
{
|
||||
public SkyPump Device { get; }
|
||||
|
||||
public string _command;
|
||||
protected string _completeEvent;
|
||||
|
||||
|
||||
protected SkyPumpHandler(SkyPump device, string command,string completeEvent)
|
||||
: base(BuildMessage(device.Address, command))
|
||||
{
|
||||
Device = device;
|
||||
_command = command;
|
||||
_completeEvent = completeEvent;
|
||||
Name = command;
|
||||
}
|
||||
|
||||
private static string _startLine = "@";
|
||||
private static string _endLine = "\r";
|
||||
private static string BuildMessage(string address, string command)
|
||||
{
|
||||
return _startLine + address + command + _endLine;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
|
||||
{
|
||||
SkyPumpMessage response = msg as SkyPumpMessage;
|
||||
ResponseMessage = msg;
|
||||
|
||||
if (response.IsAck)
|
||||
{
|
||||
SetState(EnumHandlerState.Acked);
|
||||
|
||||
if (response.Address != Device.Address)
|
||||
{
|
||||
Device.NoteError("Invalid address");
|
||||
msg.IsFormatError = true;
|
||||
}
|
||||
else if (!response.Data.Contains(_completeEvent))
|
||||
{
|
||||
Device.NoteError("Invalid response, no expected command feedback");
|
||||
msg.IsFormatError = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
SetState(EnumHandlerState.Completed);
|
||||
transactionComplete = true;
|
||||
Device.NoteError(null);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
transactionComplete = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class SkyPumpRawCommandHandler : SkyPumpHandler
|
||||
{
|
||||
public SkyPumpRawCommandHandler(SkyPump device, string command, string completeEvent)
|
||||
: base(device, command, completeEvent)
|
||||
{
|
||||
}
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
if(base.HandleMessage(msg, out handled))
|
||||
{
|
||||
var result = msg as SkyPumpMessage;
|
||||
Device.NoteRawCommandInfo(_command, result.RawMessage);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public class SkyPumpStartHandler : SkyPumpHandler
|
||||
{
|
||||
public SkyPumpStartHandler(SkyPump device)
|
||||
: base(device, "CON_FDP_ON", "FDP_ONOK")
|
||||
{
|
||||
|
||||
}
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
if(base.HandleMessage(msg, out handled))
|
||||
{
|
||||
Device.NoteStart(true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public class SkyPumpStopHandler : SkyPumpHandler
|
||||
{
|
||||
public SkyPumpStopHandler(SkyPump device)
|
||||
: base(device, "CON_FDP_OFF", "FDP_OFFOK")
|
||||
{
|
||||
|
||||
}
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
if (base.HandleMessage(msg, out handled))
|
||||
{
|
||||
Device.NoteStart(false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public class SkyPumpReadRunParaHandler : SkyPumpHandler
|
||||
{
|
||||
public SkyPumpReadRunParaHandler(SkyPump device)
|
||||
: base(device, "READ_RUN_PARA", "RUN_PARA")
|
||||
{
|
||||
}
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
if (base.HandleMessage(msg, out handled))
|
||||
{
|
||||
var result = msg as SkyPumpMessage;
|
||||
|
||||
if(result.Data.Length != 40)
|
||||
{
|
||||
Device.NoteError("Invalid 'RunParameter'response, the length is not 40");
|
||||
}
|
||||
else
|
||||
{
|
||||
Device.NoteRunPara(result.Data);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user