首次上传
This commit is contained in:
@@ -0,0 +1,213 @@
|
||||
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 System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Xml;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Temps
|
||||
{
|
||||
public sealed class AETemp : TempSensorBase
|
||||
{
|
||||
private AETempConnection _connection;
|
||||
private readonly R_TRIG _trigCommunicationError = new ();
|
||||
private readonly R_TRIG _trigFewChannels = new ();
|
||||
private readonly R_TRIG _trigMonitorExcepted = new ();
|
||||
private readonly LinkedList<HandlerBase> _lstHandler = new ();
|
||||
private readonly object _locker = new ();
|
||||
private double[] _tempBuff;
|
||||
|
||||
/// <summary>
|
||||
/// 设备通道启用
|
||||
/// </summary>
|
||||
private bool[] _channelInstalled;
|
||||
|
||||
|
||||
public override bool IsConnected => _connection is { IsConnected: true };
|
||||
|
||||
|
||||
public AETemp(string module, XmlElement node, string ioModule = "") : base(module, node, ioModule)
|
||||
{
|
||||
RTrigs.Add(_trigCommunicationError);
|
||||
RTrigs.Add(_trigFewChannels);
|
||||
RTrigs.Add(_trigMonitorExcepted);
|
||||
}
|
||||
|
||||
private void QueryTemp()
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
_lstHandler.AddLast(new AETempReadCommandHandler(this, "OUT", "1"));
|
||||
}
|
||||
}
|
||||
|
||||
protected override bool HandleInitialize()
|
||||
{
|
||||
InitController();
|
||||
|
||||
_channelInstalled=new bool[MaxChannels];
|
||||
|
||||
for (int i = 0; i < _channelInstalled.Length; i++)
|
||||
_channelInstalled[i] = SC.GetValue<bool>($"{ScBasePath}.{Name}.Channel{i+1}");
|
||||
|
||||
|
||||
_tempBuff = new double[MaxChannels];
|
||||
return true;
|
||||
}
|
||||
|
||||
private void InitController()
|
||||
{
|
||||
Address = SC.GetStringValue($"{ScBasePath}.{Name}.Address");
|
||||
_connection = new AETempConnection(Address);
|
||||
_connection.EnableLog(IsEnableLog);
|
||||
SC.RegisterValueChangedCallback($"TempSensors.EnableLogMessage", (obj) =>
|
||||
{
|
||||
_connection.EnableLog((bool)obj);
|
||||
});
|
||||
}
|
||||
|
||||
protected override double[] HandleReadTemp()
|
||||
{
|
||||
OnTimeQueryTemp();
|
||||
return IsConnected ? _tempBuff : null;
|
||||
}
|
||||
|
||||
protected void OnTimeQueryTemp()
|
||||
{
|
||||
try
|
||||
{
|
||||
_connection.MonitorTimeout();
|
||||
|
||||
if (!_connection.IsConnected || _connection.IsCommunicationError)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
_lstHandler.Clear();
|
||||
}
|
||||
|
||||
//只要断开,就进行连接
|
||||
if (!_connection.Connect())
|
||||
{
|
||||
_trigCommunicationError.CLK = !_connection.IsConnected;
|
||||
if (_trigCommunicationError.Q)
|
||||
{
|
||||
TempBasFunction.PM1PM2PostLog($"Can not connect with {_connection.Address}, {Module}.{Name}",EV.PostAlarmLog);
|
||||
|
||||
TempBasFunction.SetPm1Pm2IoForInterlock(true);
|
||||
}
|
||||
|
||||
_connection.ForceClear();
|
||||
Thread.Sleep(1000);//重连延时
|
||||
}
|
||||
else
|
||||
{
|
||||
//连接成功后不会再进入,所以也只是写入一次LOG
|
||||
LOG.Write($"{Module} {Name} Connected");
|
||||
EV.PostInfoLog(Module, $"{Name} Connected");
|
||||
TempBasFunction.PM1PM2PostLog($"{Name} Connected", EV.PostInfoLog);
|
||||
TempBasFunction.SetPm1Pm2IoForInterlock(false);
|
||||
}
|
||||
_trigCommunicationError.CLK = !_connection.IsConnected;
|
||||
_connection.ForceClear();
|
||||
}
|
||||
else
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
if (_lstHandler.Count == 0)
|
||||
QueryTemp();
|
||||
if (_lstHandler.Count > 0 && !_connection.IsBusy)
|
||||
{
|
||||
var handler = _lstHandler.First.Value;
|
||||
_lstHandler.RemoveFirst();
|
||||
if (handler != null)
|
||||
{
|
||||
_connection.Execute(handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_trigMonitorExcepted.CLK = true;
|
||||
if(_trigMonitorExcepted.Q)
|
||||
LOG.Write(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void ParseCommandInfo(string command, string message)
|
||||
{
|
||||
try
|
||||
{
|
||||
switch (command)
|
||||
{
|
||||
case "OUT":
|
||||
{
|
||||
if (message != null)
|
||||
{
|
||||
if (message.Contains(" "))
|
||||
{
|
||||
var strs = message.Split(' ');
|
||||
|
||||
if (strs.Length > 0 )
|
||||
{
|
||||
//判断AE温度通道启用,和上位机设定通道启用,是否相同
|
||||
if (strs.Length != _channelInstalled.Count(c => c == true))
|
||||
{
|
||||
_trigFewChannels.CLK = true;
|
||||
if (_trigFewChannels.Q)
|
||||
{
|
||||
TempBasFunction.PM1PM2PostLog("AE Error: Too few channels.", EV.PostAlarmLog);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
//温度数组,根据通道是否屏蔽来赋值,
|
||||
_trigFewChannels.RST = true;
|
||||
for (int i = 0; i < strs.Length; i++)
|
||||
{
|
||||
if (_channelInstalled[i] == true)
|
||||
{
|
||||
_tempBuff[i] = float.Parse(strs[i]);
|
||||
}
|
||||
}
|
||||
|
||||
//温度连续多次不变化侦测
|
||||
//TempInvariantCount = TempBasFunction.TempInvariantCount(TempDatasArray, out string tempInvariantData);
|
||||
//_trigTempNoChange.CLK = TempInvariantCount >= TempInvariantCountMax ? true : false;
|
||||
//if (_trigTempNoChange.Q)
|
||||
//{
|
||||
// TempBasFunction.PM1PM2PostLog($"{Name} {TempInvariantCount} temp no changeCount {tempInvariantData}", EV.PostWarningLog);
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
_connection?.SetCommunicationError(false, "");
|
||||
base.Reset();
|
||||
}
|
||||
|
||||
public override void Terminate()
|
||||
{
|
||||
_connection.Disconnect();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
using Aitex.Core.RT.Log;
|
||||
using MECF.Framework.Common.Communications;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Temps
|
||||
{
|
||||
public class AETempMessage : BinaryMessage
|
||||
{
|
||||
public byte DeviceAddress { get; set; }
|
||||
public byte FunctionCode { get; set; }
|
||||
public byte Length { get; set; }
|
||||
public byte[] Data { get; set; }
|
||||
|
||||
}
|
||||
public class AETempAsciiMessage : AsciiMessage
|
||||
{
|
||||
public string Data { get; set; }
|
||||
public string ErrorText { get; set; }
|
||||
}
|
||||
|
||||
class AETempConnection : TCPPortConnectionBase
|
||||
{
|
||||
private List<byte> _lstCacheBuffer = new List<byte>();
|
||||
public AETempConnection(string address) : base(address, "\r\n", true)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override bool SendMessage(string message)
|
||||
{
|
||||
return base.SendMessage(message);
|
||||
}
|
||||
public override bool SendMessage(byte[] message)
|
||||
{
|
||||
_lstCacheBuffer.Clear();
|
||||
return base.SendMessage(message);
|
||||
}
|
||||
protected override MessageBase ParseResponse(byte[] rawMessage)
|
||||
{
|
||||
_lstCacheBuffer.AddRange(rawMessage);
|
||||
byte[] temps = _lstCacheBuffer.ToArray();
|
||||
|
||||
AETempMessage msg = new AETempMessage();
|
||||
msg.IsResponse = false;
|
||||
msg.IsAck = false;
|
||||
msg.IsComplete = false;
|
||||
msg.RawMessage = _lstCacheBuffer.ToArray();
|
||||
|
||||
if (temps.Length < 9) return msg;
|
||||
|
||||
msg.DeviceAddress = temps[6];
|
||||
msg.FunctionCode = temps[7];
|
||||
if (msg.FunctionCode == 0x03)
|
||||
{
|
||||
msg.Length = temps[8];
|
||||
if (temps.Length != 9)
|
||||
{
|
||||
if (temps.Length < msg.Length + 9) return msg;
|
||||
}
|
||||
msg.Data = _lstCacheBuffer.Skip(9).Take(msg.Length).ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
msg.Data = _lstCacheBuffer.ToArray();
|
||||
}
|
||||
msg.IsResponse = true;
|
||||
msg.IsAck = true;
|
||||
msg.IsComplete = true;
|
||||
|
||||
return msg;
|
||||
}
|
||||
protected override MessageBase ParseResponse(string rawText)
|
||||
{
|
||||
AETempAsciiMessage msg = new AETempAsciiMessage();
|
||||
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;
|
||||
//}
|
||||
msg.Data = rawText.Replace("\r", "").Replace("\n", "");
|
||||
if (msg.Data.ToUpper().StartsWith("ERR"))
|
||||
{
|
||||
var errorCodeString = msg.Data.ToUpper().Replace("ERR", "").Replace(" ", "");
|
||||
int.TryParse(errorCodeString, out int errorCode);
|
||||
if (errorCode > 0)
|
||||
{
|
||||
msg.ErrorText = errorCodeString;
|
||||
msg.IsError = true;
|
||||
}
|
||||
}
|
||||
|
||||
msg.IsResponse = true;
|
||||
msg.IsAck = true;
|
||||
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
using MECF.Framework.Common.Communications;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Temps
|
||||
{
|
||||
public class AETempHandler : HandlerBase
|
||||
{
|
||||
public AETemp Device { get; }
|
||||
|
||||
public string _command;
|
||||
protected string _parameter;
|
||||
|
||||
|
||||
protected AETempHandler(AETemp device, string command, string parameter)
|
||||
: base(BuildMessage(command, parameter))
|
||||
{
|
||||
Device = device;
|
||||
_command = command;
|
||||
_parameter = parameter;
|
||||
Name = command;
|
||||
}
|
||||
private static string _CH = "\t";
|
||||
private static string _endLine = "\r";
|
||||
private static string BuildMessage(string command, string parameter)
|
||||
{
|
||||
if (string.IsNullOrEmpty(parameter))
|
||||
return command + _endLine;
|
||||
else
|
||||
return command +_CH + parameter + _endLine;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
AETempAsciiMessage response = msg as AETempAsciiMessage;
|
||||
ResponseMessage = msg;
|
||||
|
||||
if (response.IsAck)
|
||||
{
|
||||
SetState(EnumHandlerState.Acked);
|
||||
|
||||
if (msg.IsError)
|
||||
{
|
||||
//Device.NoteError($"Command '{_command}' Error: {response.Data}:{ErrorString(response.ErrorText)}");
|
||||
}
|
||||
else
|
||||
{
|
||||
SetState(EnumHandlerState.Completed);
|
||||
handled = true;
|
||||
//Device.NoteError(null);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
handled = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
private static Dictionary<string, string> ErrorDict = new Dictionary<string, string>()
|
||||
{
|
||||
{"1","Invalid message" },
|
||||
{"2","Number not found" },
|
||||
{"3","Number Invalid" },
|
||||
{"4","Parameter’s value not received" },
|
||||
{"5","Command not possible" }
|
||||
};
|
||||
private static string ErrorString(string errorCode)
|
||||
{
|
||||
if (ErrorDict.ContainsKey(errorCode))
|
||||
return ErrorDict[errorCode];
|
||||
else
|
||||
return "NotDefined error";
|
||||
}
|
||||
|
||||
}
|
||||
public class AETempReadCommandHandler : AETempHandler
|
||||
{
|
||||
public AETempReadCommandHandler(AETemp device, string command, string parameter)
|
||||
: base(device, command, parameter)
|
||||
{
|
||||
}
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
if (base.HandleMessage(msg, out handled))
|
||||
{
|
||||
var result = msg as AETempAsciiMessage;
|
||||
Device.ParseCommandInfo(_command, result.Data);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user