2024-04-08 10:40:44 +08:00
|
|
|
|
using Aitex.Core.RT.Event;
|
2023-11-02 10:28:53 +08:00
|
|
|
|
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.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
|
|
|
|
|
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Temps
|
|
|
|
|
{
|
2024-04-08 10:40:44 +08:00
|
|
|
|
public class SensorTemp : TempSensorBase
|
2023-11-02 10:28:53 +08:00
|
|
|
|
{
|
|
|
|
|
private SensorTempConnection _connection;
|
2024-04-08 10:40:44 +08:00
|
|
|
|
private readonly R_TRIG _trigCommunicationError = new ();
|
|
|
|
|
private readonly R_TRIG _trigMonitorExcepted = new ();
|
|
|
|
|
private readonly LinkedList<HandlerBase> _lstHandler = new();
|
|
|
|
|
private readonly object _locker = new ();
|
2023-11-02 10:28:53 +08:00
|
|
|
|
private string PMName;
|
2024-04-08 10:40:44 +08:00
|
|
|
|
private double[] _tempBuff;
|
|
|
|
|
|
|
|
|
|
public override bool IsConnected => _connection is { IsConnected: true };
|
2023-11-02 10:28:53 +08:00
|
|
|
|
|
2024-04-08 10:40:44 +08:00
|
|
|
|
|
2023-11-02 10:28:53 +08:00
|
|
|
|
|
|
|
|
|
public SensorTemp(string module, XmlElement node, string ioModule = "") : base(module, node, ioModule)
|
|
|
|
|
{
|
2024-04-08 10:40:44 +08:00
|
|
|
|
RTrigs.Add(_trigCommunicationError);
|
|
|
|
|
RTrigs.Add(_trigMonitorExcepted);
|
2023-11-02 10:28:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-08 10:40:44 +08:00
|
|
|
|
private void QueryTemp()
|
2023-11-02 10:28:53 +08:00
|
|
|
|
{
|
|
|
|
|
SetQuery("QueryBup", "00mw0\r");
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-08 10:40:44 +08:00
|
|
|
|
private void SetQuery(string name, string strCmd)
|
2023-11-02 10:28:53 +08:00
|
|
|
|
{
|
|
|
|
|
lock (_locker)
|
|
|
|
|
{
|
|
|
|
|
if (_connection.IsBusy)
|
|
|
|
|
{
|
|
|
|
|
_connection.ForceClear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] bCmd = Encoding.Default.GetBytes(strCmd);
|
|
|
|
|
|
|
|
|
|
_lstHandler.AddLast(new SensorTempQueryHandler(this, name, bCmd));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-04-08 10:40:44 +08:00
|
|
|
|
protected override bool HandleInitialize()
|
2023-11-02 10:28:53 +08:00
|
|
|
|
{
|
2024-04-08 10:40:44 +08:00
|
|
|
|
InitController();
|
|
|
|
|
_tempBuff = new double[MaxChannels];
|
2023-11-02 10:28:53 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-08 10:40:44 +08:00
|
|
|
|
private void InitController()
|
2023-11-02 10:28:53 +08:00
|
|
|
|
{
|
|
|
|
|
//添加参数设置
|
2024-04-08 10:40:44 +08:00
|
|
|
|
PMName = SC.GetStringValue($"{ScBasePath}.{Name}.PMName");
|
|
|
|
|
Address = SC.GetStringValue($"{ScBasePath}.{Name}.Address");
|
2023-11-02 10:28:53 +08:00
|
|
|
|
_connection = new SensorTempConnection(Address);
|
2024-04-08 10:40:44 +08:00
|
|
|
|
_connection.EnableLog(IsEnableLog);
|
2023-11-03 10:04:22 +08:00
|
|
|
|
|
2023-11-20 13:17:51 +08:00
|
|
|
|
SC.RegisterValueChangedCallback($"TempDevice.EnableLogMessage", (obj) => { _connection.EnableLog((bool)obj); });
|
2023-11-02 10:28:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-08 10:40:44 +08:00
|
|
|
|
protected override void HandleMonitor()
|
2023-11-02 10:28:53 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_connection.MonitorTimeout();
|
|
|
|
|
|
|
|
|
|
if (!_connection.IsConnected)
|
|
|
|
|
{
|
|
|
|
|
|
2024-04-08 10:40:44 +08:00
|
|
|
|
lock (_locker)
|
|
|
|
|
{
|
|
|
|
|
_lstHandler.Clear();
|
|
|
|
|
}
|
2023-11-02 10:28:53 +08:00
|
|
|
|
|
|
|
|
|
//只要断开,就进行连接
|
|
|
|
|
if (!_connection.Connect())
|
|
|
|
|
{
|
|
|
|
|
_trigCommunicationError.CLK = !_connection.IsConnected;
|
|
|
|
|
if (_trigCommunicationError.Q)
|
|
|
|
|
{
|
2023-11-02 17:46:03 +08:00
|
|
|
|
Thread.Sleep(2000);//不加延时软件刚启动可能不打印
|
2024-04-08 10:40:44 +08:00
|
|
|
|
TempBasFunction.PMPostLog(PMName, $"Can not connect with {_connection.Address}, {Name}", EV.PostAlarmLog);
|
|
|
|
|
TempBasFunction.SetPmIoForInterlock(PMName, true);
|
2023-11-02 10:28:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else//已连接,自动复位断联信号
|
|
|
|
|
{
|
|
|
|
|
_trigCommunicationError.RST = true;
|
2024-04-08 10:40:44 +08:00
|
|
|
|
TempBasFunction.PMPostLog(PMName, $"{PMName} {Name} {Address} reconnected.", EV.PostInfoLog);
|
|
|
|
|
TempBasFunction.SetPmIoForInterlock(PMName, false);
|
2023-11-02 10:28:53 +08:00
|
|
|
|
}
|
2023-11-02 17:46:03 +08:00
|
|
|
|
Thread.Sleep(1000);//重连延时
|
2023-11-02 10:28:53 +08:00
|
|
|
|
_connection.ForceClear();
|
|
|
|
|
}
|
2024-04-08 10:40:44 +08:00
|
|
|
|
else
|
2023-11-02 10:28:53 +08:00
|
|
|
|
{
|
2024-04-08 10:40:44 +08:00
|
|
|
|
lock (_locker)
|
2023-11-02 10:28:53 +08:00
|
|
|
|
{
|
2024-04-08 10:40:44 +08:00
|
|
|
|
if (_lstHandler.Count == 0)
|
|
|
|
|
QueryTemp();
|
|
|
|
|
|
|
|
|
|
if (_lstHandler.Count > 0 && !_connection.IsBusy)
|
|
|
|
|
{
|
|
|
|
|
var handler = _lstHandler.First.Value;
|
|
|
|
|
_lstHandler.RemoveFirst();
|
|
|
|
|
if (handler != null)
|
|
|
|
|
_connection.Execute(handler);
|
2023-11-02 10:28:53 +08:00
|
|
|
|
|
2024-04-08 10:40:44 +08:00
|
|
|
|
}
|
2023-11-02 10:28:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-03 10:04:22 +08:00
|
|
|
|
|
|
|
|
|
if (_connection.IsCommunicationError)
|
|
|
|
|
{
|
|
|
|
|
//ResponseError();
|
|
|
|
|
}
|
2024-04-08 10:40:44 +08:00
|
|
|
|
|
|
|
|
|
_trigMonitorExcepted.CLK = false;
|
2023-11-02 10:28:53 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-04-08 10:40:44 +08:00
|
|
|
|
_trigMonitorExcepted.CLK = true;
|
|
|
|
|
if(_trigMonitorExcepted.Q)
|
|
|
|
|
LOG.Write(ex);
|
2023-11-02 10:28:53 +08:00
|
|
|
|
}
|
2024-04-08 10:40:44 +08:00
|
|
|
|
}
|
2023-11-02 10:28:53 +08:00
|
|
|
|
|
2024-04-08 10:40:44 +08:00
|
|
|
|
protected override double[] HandleReadTemp()
|
|
|
|
|
{
|
|
|
|
|
return IsConnected ? _tempBuff : null;
|
2023-11-02 10:28:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ResponseQuery(string name, byte[] data)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-04-08 10:40:44 +08:00
|
|
|
|
var sData = Encoding.Default.GetString(data);
|
2023-11-02 10:28:53 +08:00
|
|
|
|
if (name == "QueryBup")
|
|
|
|
|
{
|
2024-04-08 10:40:44 +08:00
|
|
|
|
var strTemp = sData;
|
2023-11-02 10:28:53 +08:00
|
|
|
|
if (!sData.Contains("\r"))
|
|
|
|
|
return;
|
|
|
|
|
|
2024-04-08 10:40:44 +08:00
|
|
|
|
var temp = Convert.ToInt32(strTemp.Replace("\r", ""), 16);
|
|
|
|
|
_tempBuff[0] = (float)temp / 10;
|
2023-11-07 13:36:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//温度连续多次不变化侦测
|
2024-04-08 10:40:44 +08:00
|
|
|
|
//TempInvariantCount = TempBasFunction.TempInvariantCount(TempDatasArray, out string tempInvariantData);
|
2023-11-07 13:36:12 +08:00
|
|
|
|
//_trigTempNoChange.CLK = TempInvariantCount >= TempInvariantCountMax ? true : false;
|
|
|
|
|
//if (_trigTempNoChange.Q)
|
|
|
|
|
//{
|
2024-04-08 10:40:44 +08:00
|
|
|
|
// TempBasFunction.PMPostLog(PMName, $"{PMName} {Name} {TempInvariantCount} temp no changeCount {tempInvariantData}", EV.PostWarningLog);
|
2023-11-07 13:36:12 +08:00
|
|
|
|
//}
|
2023-11-02 10:28:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ResponseError()
|
|
|
|
|
{
|
2023-11-02 17:46:03 +08:00
|
|
|
|
_trigCommunicationError.CLK = true;
|
|
|
|
|
if (_trigCommunicationError.Q)
|
|
|
|
|
{
|
2024-04-08 10:40:44 +08:00
|
|
|
|
TempBasFunction.PMPostLog(PMName, $"{PMName} {Name} {Address} could not receive temp", EV.PostAlarmLog);
|
|
|
|
|
TempBasFunction.SetPmIoForInterlock(PMName, true);
|
2023-11-02 17:46:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-02 10:28:53 +08:00
|
|
|
|
}
|
2024-04-08 10:40:44 +08:00
|
|
|
|
|
|
|
|
|
public override bool Connect()
|
2023-11-02 10:28:53 +08:00
|
|
|
|
{
|
2024-04-08 10:40:44 +08:00
|
|
|
|
return _connection.Connect();
|
2023-11-02 10:28:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-08 10:40:44 +08:00
|
|
|
|
public override void Terminate()
|
2023-11-02 10:28:53 +08:00
|
|
|
|
{
|
|
|
|
|
_connection.Disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|