refactor: 将Framework库和RT、UI放在同一个解决方案中。

This commit is contained in:
SL
2026-06-01 17:48:28 +08:00
parent 1948f4290d
commit b73d39a6b1
4331 changed files with 506148 additions and 49363 deletions
@@ -0,0 +1,417 @@
using Aitex.Core.RT.DataCenter;
using Aitex.Core.RT.Device;
using Aitex.Core.RT.Event;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.OperationCenter;
using Aitex.Core.RT.SCCore;
using Aitex.Core.Util;
using MECF.Framework.Common.Communications;
using System;
using System.Collections.Generic;
using System.Threading;
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Temps.P116PIDTC
{
public class AdressInfo
{
public string Name { get; set; }
public byte DeviceAddress { get; set; }
public int UnitAddress { get; set; }
public int Length { get; set; }
public int Data;
public string Remark { get; set; }//H3-N2-H4-N5-H6(H3代表连续3个地址有数据,N2代表H3与H4中间有两个地址为预留)
}
public class P116PIDTC : BaseDevice, IConnection,IDevice
{
private P116PIDTCConnection _connection;
private bool _activeMonitorStatus;
private int _errorCode;
private R_TRIG _trigCommunicationError = new R_TRIG();
private R_TRIG _trigRetryConnect = new R_TRIG();
private PeriodicJob _thread;
//private int tempCount = 1;
private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
private object _locker = new object();
private bool _enableLog = true;
private string _scRoot;
//private int _actualTempValue = 999;
//private int _settingTempValue = 888;
public int moduleCount { get; set; }
private int _tempWarningMaxDiff = 1; //报警最大温度(实际温度与设定温度之间的差值)
//private R_TRIG[] TempWarningTrig = new R_TRIG[12]; //温度报警
//string[] WarningInfo = new string[12] { "HeatTMAPanel","HeatTCSPanel","OuterPanel","MidPanel","InnerPanel","HeatShowerOuter", "HeatShowerInner", "HeatShowerMid", "HeatDptPanel","HeatLeakSource","HeatLeakBypass","HeatDptGate"};
//public float[] ActualTemp = new float[12];
//public float[] SettingTemp = new float[12];
public float fPVInValue, fTargetSP;
public int iAM = -1;
public string Address
{
get; set;
}
public bool IsConnected
{
get
{
return _connection != null && _connection.IsConnected;
}
}
public bool Connect()
{
return true;
}
public bool Disconnect()
{
return true;
}
public P116PIDTC(string module, string name, string scRoot) : base(module, name, name, name)
{
_scRoot = scRoot;
_activeMonitorStatus = true;
}
~P116PIDTC()
{
_connection.Disconnect();
}
public void QueryTemp()
{
_lstHandler.AddLast(new P116PIDTCQueryHandler(this, "QueryPVInValue", 0x01, 0x00, 0x01, 0x00, 0x01));
_lstHandler.AddLast(new P116PIDTCQueryHandler(this, "QueryTargetSP", 0x01, 0x00, 0x02, 0x00, 0x01));
_lstHandler.AddLast(new P116PIDTCQueryHandler(this, "QueryAM", 0x01, 0x01, 0x11, 0x00, 0x01));
}
public void ResetDevice()
{
}
public void QueryError()
{
EV.PostInfoLog(Module, "Query error");
}
public bool Initialize()
{
// string portName = SC.GetStringValue($"{_scRoot}.{Module}.{Name}.Address");
string portName =SC.GetStringValue($"{Name}.Address");
Address = portName;
//int address = SC.GetValue<int>($"{_scRoot}.{Name}.DeviceAddress");
moduleCount= SC.GetValue<int>($"{Name}.Modules");
_enableLog = SC.GetValue<bool>($"{Name}.EnableLogMessage");
_connection = new P116PIDTCConnection(portName);
_connection.EnableLog(_enableLog);
_tempWarningMaxDiff = SC.GetValue<int>($"{Name}.TempWarnMaxDiff");
//
fPVInValue = fTargetSP = 0.0f;
iAM = -1;
int count = SC.ContainsItem("System.ComPortRetryCount") ? SC.GetValue<int>("System.ComPortRetryCount") : 5;
int sleep = SC.ContainsItem("System.ComPortRetryDelayTime") ? SC.GetValue<int>("System.ComPortRetryDelayTime") : 2;
if (sleep <= 0 || sleep > 10)
sleep = 2;
int retry = 0;
do
{
_connection.Disconnect();
Thread.Sleep(sleep * 1000);
if (_connection.Connect())
{
EV.PostInfoLog(Module, $"{Name} connected");
break;
}
if (count > 0 && retry++ > count)
{
LOG.Write($"Retry connect {Module}.{Name} stop retry.");
EV.PostAlarmLog(Module, $"Can't connect to {Module}.{Name}.");
break;
}
Thread.Sleep(sleep * 1000);
LOG.Write($"Retry connect {Module}.{Name} for the {retry + 1} time.");
} while (true);
_thread = new PeriodicJob(200, OnTimer, $"{Module}.{Name} MonitorHandler", true);
DATA.Subscribe($"{Module}.{Name}.PVInValue", () => fPVInValue);
DATA.Subscribe($"{Module}.{Name}.TargetSP", () => fTargetSP) ;
DATA.Subscribe($"{Module}.{Name}.AM", () => iAM);
OP.Subscribe($"{Module}.{Name}.WritePVInValue", SetPVInValue);
OP.Subscribe($"{Module}.{Name}.WriteTargetSP", SetTargetSP);
OP.Subscribe($"{Module}.{Name}.WriteAM", SetAM); //0-Auto,1-Manual,2-Off
ConnectionManager.Instance.Subscribe($"{Name}", this);
return true;
}
public int _connecteTimes { get; set; }
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($"{Name}.Address"));
if (!_connection.Connect())
{
EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
}
else
{
//重连成功后将TargetSP设置为0
SetTargetSP("WriteTargetSP", new object[] { 0 });
}
}
if (_connecteTimes++ < 3)
_connection.ForceClear();
else _connecteTimes= 4;
return true;
}
HandlerBase handler = null;
//if (!_connection.IsBusy)
//{
lock (_locker)
{
if (_lstHandler.Count == 0 && !IsConfig)
QueryTemp();
if (_lstHandler.Count > 0 && !_connection.IsBusy)
{
handler = _lstHandler.First.Value;
_lstHandler.RemoveFirst();
if (handler != null)
{
_connection.Execute(handler);
}
}
}
}
catch (Exception ex)
{
LOG.Write(ex);
}
return true;
}
internal void NoteError()
{
}
public void Monitor()
{
try
{
_connection.EnableLog(_enableLog);
if (_connecteTimes < 4) return;
_trigCommunicationError.CLK = _connection.IsCommunicationError;
if (_trigCommunicationError.Q)
{
fPVInValue = 0f;
fTargetSP = 0f;
iAM = -1;
EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
}
}
catch (Exception ex)
{
LOG.Write(ex);
}
}
public bool IsConfig { get; set; }
public bool SetPVInValue(string cmd, object [] obj)//设置单个地址 0x0001
{
IsConfig = true;
_lstHandler.Clear();
lock (_locker)
{
if (obj.Length > 0)
{
byte DeviceAddress = 0x01;
int adress = 0x0001;
byte addressH = Convert.ToByte((adress >> 8) & 0xff); ;
byte addressL = Convert.ToByte(adress & 0xff);
//var floatdata = Convert.ToSingle(objs[1]) * 10;
//var data = Convert.ToInt16(floatdata);
var data = Convert.ToInt16(obj[0]);
byte dataH = Convert.ToByte((data >> 8) & 0xff);
byte dataL = Convert.ToByte(data & 0xff);
_lstHandler.AddLast(new P116PIDTCWriteSingleHandler(this, "WritePVInValue", DeviceAddress, addressH, addressL, dataH, dataL));
}
}
return true;
}
public bool SetTargetSP(string cmd, object[] obj)//设置单个地址 0x0002
{
IsConfig = true;
_lstHandler.Clear();
lock (_locker)
{
if (obj.Length > 0)
{
byte DeviceAddress = 0x01;
int adress = 0x0002;
byte addressH = Convert.ToByte((adress >> 8) & 0xff); ;
byte addressL = Convert.ToByte(adress & 0xff);
var data = Convert.ToInt16(obj[0]) * 10;//10倍值
byte dataH = Convert.ToByte((data >> 8) & 0xff);
byte dataL = Convert.ToByte(data & 0xff);
_lstHandler.AddLast(new P116PIDTCWriteSingleHandler(this, "WriteTargetSP", DeviceAddress, addressH, addressL, dataH, dataL));
}
}
return true;
}
public bool SetAM(string cmd, object[] obj)//设置单个地址 0x0111 = 273
{
IsConfig = true;
_lstHandler.Clear();
lock (_locker)
{
if (obj.Length > 0)
{
byte DeviceAddress = 0x01;
int adress = 0x0111;
byte addressH = Convert.ToByte((adress >> 8) & 0xff); ;
byte addressL = Convert.ToByte(adress & 0xff);
var data = Convert.ToInt16(obj[0]);
if (data == 0)
{
//开启自动
byte dataH = Convert.ToByte((data >> 8) & 0xff);
byte dataL = Convert.ToByte(data & 0xff);
_lstHandler.AddLast(new P116PIDTCWriteSingleHandler(this, "WriteAM", DeviceAddress, addressH, addressL, dataH, dataL));
}
else if(data == 2)
{
//关闭输出
//A-M:address 273
data = 0x1;
byte dataH = Convert.ToByte((data >> 8) & 0xff);
byte dataL = Convert.ToByte(data & 0xff);
_lstHandler.AddLast(new P116PIDTCWriteSingleHandler(this, "WriteAM", DeviceAddress, addressH, addressL, dataH, dataL));
//OP: address 3
adress = 0x0003;
addressH = Convert.ToByte((adress >> 8) & 0xff); ;
addressL = Convert.ToByte(adress & 0xff);
data = 0x0;
dataH = Convert.ToByte((data >> 8) & 0xff);
dataL = Convert.ToByte(data & 0xff);
_lstHandler.AddLast(new P116PIDTCWriteSingleHandler(this, "WriteAM", DeviceAddress, addressH, addressL, dataH, dataL));
}
else
{
EV.PostWarningLog(Module, "Find wrong parameter when writing AM.");
}
}
}
return true;
}
public void Reset()
{
_connecteTimes = 0;
_connection.SetCommunicationError(false, "");
_trigCommunicationError.RST = true;
_enableLog = SC.GetValue<bool>($"{Name}.EnableLogMessage");
_trigRetryConnect.RST = true;
}
public void RespondAbnormal(int funcode, int abnormalcode)
{
if (funcode == 0x83)
{
foreach (var code in AbnormalCode)
{
if (code.Key == abnormalcode)
EV.PostWarningLog(Name, $"Query Abnormal:{code.Value}");
}
}
if (funcode == 0x90 || funcode == 0x86)
{
foreach (var code in AbnormalCode)
{
if (code.Key == abnormalcode)
EV.PostWarningLog(Name, $"Write Abnormal:{code.Value}");
}
}
}
public Dictionary<int, string> AbnormalCode = new Dictionary<int, string>()
{
{ 1,"ncorrect function code, this machine does not correspond to the function code." },
{ 2,"Incorrect data address" },
{ 3,"Incorrect data." },
{ 4,"operation error" },
};
public void SetActiveMonitor(bool active)
{
_activeMonitorStatus = active;
}
public void SetErrorCode(int errorCode)
{
_errorCode = errorCode;
}
public void Terminate()
{
_connection.Disconnect();
}
}
}
@@ -0,0 +1,82 @@
using MECF.Framework.Common.Communications;
using System.Collections.Generic;
using System.Linq;
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Temps.P116PIDTC
{
public class P116PIDTCMessage : BinaryMessage
{
public byte DeviceAddress { get; set; }
public byte FunctionCode { get; set; }
public byte Length { get; set; }
public byte[] Data { get; set; }
}
public class P116PIDTCConnection:SerialPortConnectionBase
{
private List<byte> _lstCacheBuffer = new List<byte>();
public P116PIDTCConnection(string portName) : base(portName, 9600, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, "\n", false)
{
}
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();
P116PIDTCMessage msg = new P116PIDTCMessage();
msg.IsResponse = false;
msg.IsAck = false;
msg.IsComplete = false;
msg.RawMessage = _lstCacheBuffer.ToArray();
if (temps.Length < 4) return msg;
msg.DeviceAddress = temps[0];
msg.FunctionCode = temps[1];
if (msg.FunctionCode !=0x03)
{
msg.Data = _lstCacheBuffer.ToArray();
}
else
{
msg.Length = temps[2];
if (temps.Length < msg.Length + 5)
{
//msg.IsFormatError = true;
return msg;
}
msg.Data = _lstCacheBuffer.Skip(3).Take(msg.Length).ToArray();
}
msg.IsResponse = true;
msg.IsAck = true;
msg.IsComplete = true;
return msg;
}
private static byte ModRTU_CRC(byte[] buffer)
{
//ushort crc = 0xFFFF;
// var buf = System.Text.Encoding.UTF8.GetBytes(String.Join(Environment.NewLine, buffer));
var buf = buffer;
var len = buffer.Length;
byte temp = buffer[0];
for (int i = 1; i < buffer.Length; i++)
{
temp = (byte)(temp ^ buffer[i]);
}
return (byte)~temp;
}
}
}
@@ -0,0 +1,166 @@
using MECF.Framework.Common.Communications;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Temps.P116PIDTC
{
public abstract class P116PIDTCHandler : HandlerBase
{
public P116PIDTC Device { get; }
protected P116PIDTCHandler(P116PIDTC device, byte[] commandvalue)
: base(BuildMessage(commandvalue))
{
Device = device;
}
private static byte[] BuildMessage(byte[] commandvalue)
{
byte[] crc = ModRTU_CRC(commandvalue);
List<byte> result = commandvalue.ToList();
foreach (byte b in crc)
{
result.Add(b);
}
return result.ToArray();
}
public override bool HandleMessage(MessageBase msg, out bool handled)
{
var result = msg as P116PIDTCMessage;
ResponseMessage = msg;
handled = true;
return true;
}
private static byte[] ModRTU_CRC(byte[] buffer)
{
ushort crc = 0xFFFF;
// var buf = System.Text.Encoding.UTF8.GetBytes(String.Join(Environment.NewLine, buffer));
var buf = buffer;
var len = buffer.Length;
for (var pos = 0; pos < len; pos++)
{
crc ^= buf[pos]; // XOR byte into least sig. byte of crc
for (var i = 8; i != 0; i--)
// Loop over each bit
if ((crc & 0x0001) != 0)
{
// If the LSB is set
crc >>= 1; // Shift right and XOR 0xA001
crc ^= 0xA001;
}
else // Else LSB is not set
{
crc >>= 1; // Just shift right
}
}
// Note, this number has low and high bytes swapped, so use it accordingly (or swap bytes)
return BitConverter.GetBytes(crc);
}
}
public class P116PIDTCQueryHandler : P116PIDTCHandler
{
public P116PIDTCQueryHandler(P116PIDTC device,string name, byte groupAddress,byte offerHigh,byte offerLow,byte dataHigh,byte dataLow)
: base(device, new byte[] { groupAddress, 3, offerHigh, offerLow, dataHigh, dataLow })
{
Name = name;//"Query Temp";
}
public override bool HandleMessage(MessageBase msg, out bool handled)
{
var result = msg as P116PIDTCMessage;
handled = false;
if (!result.IsResponse) return true;
short rvalue = 0;
rvalue = (short)(rvalue ^ result.Data[0]);
rvalue = (short)(rvalue << 8);
rvalue = (short)(rvalue ^ result.Data[1]);
//if(Name.Contains("Actual"))
//Device.ActualTemp = rvalue;
//else
//Device.SettingTemp = rvalue;
if (Name.Contains("QueryPVInValue"))
{
Device.fPVInValue =Convert.ToSingle(rvalue)/10;
}
else if(Name.Contains("QueryTargetSP"))
{
Device.fTargetSP = Convert.ToSingle(rvalue)/10;
}
else if (Name.Contains("QueryAM"))
{
Device.iAM = Convert.ToInt32(rvalue);
}
else
{
}
Device._connecteTimes = 0;
ResponseMessage = msg;
handled = true;
Thread.Sleep(500);
return true;
}
}
public class P116PIDTCWriteSingleHandler : P116PIDTCHandler
{
public P116PIDTCWriteSingleHandler(P116PIDTC device, string name, byte groupAddress, byte offerHigh, byte offerLow, byte dataHigh, byte dataLow)
: base(device, new byte[] { groupAddress, 0x06, offerHigh, offerLow, dataHigh, dataLow })
{
Name = name;
}
public override bool HandleMessage(MessageBase msg, out bool handled)
{
var result = msg as P116PIDTCMessage;
handled = false;
if (!result.IsResponse) return true;
if (result.FunctionCode == 0x86)
{
Device.RespondAbnormal(result.FunctionCode, result.Data[2]);
}
ResponseMessage = msg;
handled = true;
Device.IsConfig = false;
Thread.Sleep(1000);
return true;
}
}
public class P116PIDTCWriteHandler : P116PIDTCHandler
{
public P116PIDTCWriteHandler(P116PIDTC device, string name,byte groupAddress, byte offerHigh, byte offerLow,byte unitH,byte unitL,byte length, byte dataHigh, byte dataLow)
: base(device, new byte[] { groupAddress, 0x10, offerHigh, offerLow, unitH, unitL, length, dataHigh, dataLow })
{
Name = name;
}
public override bool HandleMessage(MessageBase msg, out bool handled)
{
var result = msg as P116PIDTCMessage;
handled = false;
if (!result.IsResponse) return true;
if (result.FunctionCode == 0x90)
{
Device.RespondAbnormal(result.FunctionCode, result.Data[2]);
}
ResponseMessage = msg;
handled = true;
Device.IsConfig = false;
Thread.Sleep(1000);
return true;
}
}
}