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,326 @@
using Aitex.Core.Common.DeviceData;
using Aitex.Core.RT.DataCenter;
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 MECF.Framework.Common.Device.Bases;
using System;
using System.Collections.Generic;
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFMatchs.Kaimei
{
public class KaimeiRFMatch : RfMatchBase, IConnection
{
public string Address => Connection.Address;
public bool IsConnected => Connection.IsConnected && !_connection.IsCommunicationError;
public bool Connect()
{
return _connection.Connect();
}
public bool Disconnect()
{
return _connection.Disconnect();
}
public byte SlaveAddress { get; private set; } = 0x01;
public string PortStatus { get; set; } = "Closed";
private KaimeiRFMatchConnection _connection;
public KaimeiRFMatchConnection Connection
{
get { return _connection; }
}
public override AITRfMatchData DeviceData
{
get
{
return new AITRfMatchData()
{
Module = Module,
DeviceName = Name,
DisplayName = Name,
TuneMode1 = TuneMode1,
TunePosition1 = TunePosition1,
LoadPosition1 = LoadPosition1,
LoadPosition1SetPoint = LoadPosition1Setpoint,
TunePosition1SetPoint = TunePosition1Setpoint,
TuneRange = (float)SC.GetValue<double>($"{Module}.{Name}.TuneRange"),
LoadRange = (float)SC.GetValue<double>($"{Module}.{Name}.LoadRange"),
IsInterlockOk = true,
};
}
}
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>();
private DeviceTimer _QueryTimer = new DeviceTimer();
private readonly int _QueryInterval = 2000;
private object _locker = new object();
private bool _enableLog;
private string _address;
private string _scRoot;
public KaimeiRFMatch(string module, string name, string scRoot) : base(module, name)
{
_scRoot = scRoot;
}
public override bool Initialize()
{
base.Initialize();
_address = SC.GetStringValue($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.Address");
_enableLog = SC.GetValue<bool>($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.EnableLogMessage");
_connection = new KaimeiRFMatchConnection(_address);
_connection.EnableLog(_enableLog);
if (_connection.Connect())
{
PortStatus = "Open";
EV.PostInfoLog(Module, $"{Module}.{Name} connected");
}
//_lstMonitorHandler.AddLast(new CommetRFMatchGetActualCapHandler(this));
_thread = new PeriodicJob(100, OnTimer, $"{Module}.{Name} MonitorHandler", true);
_QueryTimer.Start(_QueryInterval);
_lstMonitorHandler.AddLast(new KaimeiRFMatchGetStatusHandler(this));
DATA.Subscribe($"{Module}.{Name}.IsConnected", () => IsConnected);
DATA.Subscribe($"{Module}.{Name}.Address", () => Address);
OP.Subscribe($"{Module}.{Name}.Reconnect", (string cmd, object[] args) =>
{
Disconnect();
Connect();
return true;
});
//for recipe
OP.Subscribe($"{Module}.{Name}.SetLoad1", (out string reason, int time, object[] param) =>
{
reason = string.Empty;
if (TuneMode1Setpoint != EnumRfMatchTuneMode.Auto)
SetLoad1(Convert.ToSingle(param[0]));
return true;
});
//for recipe
OP.Subscribe($"{Module}.{Name}.SetTune1", (out string reason, int time, object[] param) =>
{
reason = string.Empty;
if (TuneMode1Setpoint != EnumRfMatchTuneMode.Auto)
SetTune1(Convert.ToSingle(param[0]));
return true;
});
//for recipe
OP.Subscribe($"{Module}.{Name}.SetTuneMode1", (out string reason, int time, object[] param) =>
{
reason = string.Empty;
SetTuneMode1(param[0].ToString().ToUpper() == "AUTO" ? EnumRfMatchTuneMode.Auto : EnumRfMatchTuneMode.Manual);
return true;
});
OP.Subscribe($"{Module}.{Name}.SavePreset", (function, args) =>
{
SavePreset();
return 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($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.Address"));
if (!_connection.Connect())
{
EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
}
else
{
//_lstHandler.AddLast(new CommetRFMatchQueryPinHandler(this, _deviceAddress));
//_lstHandler.AddLast(new CommetRFMatchSetCommModeHandler(this, _deviceAddress, EnumRfPowerCommunicationMode.Host));
}
}
return true;
}
HandlerBase handler = null;
if (!_connection.IsBusy)
{
lock (_locker)
{
if (_lstHandler.Count == 0)
{
if (_QueryTimer.IsTimeout())
{
foreach (var monitorHandler in _lstMonitorHandler)
{
_lstHandler.AddLast(monitorHandler);
}
_QueryTimer.Start(_QueryInterval);
}
}
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}");
}
base.Monitor();
}
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();
}
#region Command Functions
internal void NoteActualCap(float value)
{
}
public override void SetTuneMode1(EnumRfMatchTuneMode enumRfMatchTuneMode)
{
TuneMode1Setpoint = enumRfMatchTuneMode;
//if (SC.ContainsItem("System.IsSimulatorMode") && SC.GetValue<bool>("System.IsSimulatorMode"))
// TuneMode1 = TuneMode1Setpoint;
lock (_locker)
{
_lstHandler.AddLast(new KaimeiRFMatchPresetValveHandler(this, TuneMode1Setpoint, TunePosition1Setpoint, LoadPosition1Setpoint));
}
}
public override void SetLoad1(float load)
{
LoadPosition1Setpoint = load;
//if (SC.ContainsItem("System.IsSimulatorMode") && SC.GetValue<bool>("System.IsSimulatorMode"))
// LoadPosition1 = LoadPosition1Setpoint;
lock (_locker)
{
_lstHandler.AddLast(new KaimeiRFMatchPresetValveHandler(this, TuneMode1Setpoint, TunePosition1Setpoint, LoadPosition1Setpoint));
}
}
public override void SetTune1(float tune)
{
TunePosition1Setpoint = tune;
//if (SC.ContainsItem("System.IsSimulatorMode") && SC.GetValue<bool>("System.IsSimulatorMode"))
// TunePosition1 = TunePosition1Setpoint;
lock (_locker)
{
_lstHandler.AddLast(new KaimeiRFMatchPresetValveHandler(this, TuneMode1Setpoint, TunePosition1Setpoint, LoadPosition1Setpoint));
}
}
public void SavePreset()
{
lock (_locker)
{
_lstHandler.AddLast(new KaimeiRFMatchSetCurrentValveHandler(this));
}
}
#endregion
#region Properties
public string Error { get; private set; }
#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.PostAlarmLog(Module, $"{Module}.{Name} error, {reason}");
}
Error = reason;
}
else
{
Error = null;
}
}
#endregion
}
}
@@ -0,0 +1,88 @@
using Aitex.Core.RT.Log;
using MECF.Framework.Common.Communications;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFMatchs.Kaimei
{
public class KaimeiRFMatchMessage : BinaryMessage
{
public string Command { get; set; }
public string ErrorCode { get; set; }
public byte[] Data { get; set; }
}
public class KaimeiRFMatchConnection : SerialPortConnectionBase
{
private List<byte> _msgBuffer = new List<byte>();
public KaimeiRFMatchConnection(string portName, int baudRate = 38400, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
: base(portName, baudRate, dataBits, parity, stopBits, "", false)
{
}
public override bool SendMessage(byte[] message)
{
return base.SendMessage(message);
}
protected override MessageBase ParseResponse(byte[] rawMessage)
{
KaimeiRFMatchMessage msg = new KaimeiRFMatchMessage();
if (rawMessage == null || rawMessage.Length < 1)
{
return null;
}
_msgBuffer.AddRange(rawMessage);
if (_msgBuffer.Count < 5)
{
return msg;
}
if(_msgBuffer[1] == 0x03)
{
var dataLength = _msgBuffer[2];
if(_msgBuffer.Count < 3 + dataLength + 2)
{
return msg;
}
else
{
msg.RawMessage = _msgBuffer.Take(3 + dataLength + 2).ToArray();
msg.Data = _msgBuffer.Skip(3).Take(dataLength).ToArray();
_msgBuffer.RemoveRange(0, msg.RawMessage.Length);
msg.IsResponse = true;
msg.IsAck = true;
msg.IsComplete = true;
return msg;
}
}
else if(_msgBuffer[1] == 0x10)
{
//SlaveAddress,FunctionCode,StartingAddressHigh,StartingAddressLow,NumberAddressHigh,NumberAddressLow,CRC,CRC
if(_msgBuffer.Count < 8)
{
return msg;
}
else
{
msg.RawMessage = _msgBuffer.Take(8).ToArray();
_msgBuffer.RemoveRange(0, msg.RawMessage.Length);
msg.IsResponse = true;
msg.IsAck = true;
msg.IsComplete = true;
return msg;
}
}
else
{
msg.IsFormatError = true;
LOG.Error($"invalid response");
return msg;
}
}
}
}
@@ -0,0 +1,238 @@
using Aitex.Core.Common.DeviceData;
using MECF.Framework.Common.Communications;
using MECF.Framework.Common.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFMatchs.Kaimei
{
public abstract class KaimeiRFMatchHandler : HandlerBase
{
public KaimeiRFMatch Device { get; }
public string _command;
protected string _parameter;
protected KaimeiRFMatchHandler(KaimeiRFMatch device, string command)
: base(BuildMessage(ToByteArray(command)))
{
Device = device;
_command = command;
_address = device.SlaveAddress;
Name = command;
}
protected KaimeiRFMatchHandler(KaimeiRFMatch device, string command, ushort parameter)
: base(BuildMessage(ToByteArray(command), UshortToByteArray(parameter)))
{
Device = device;
_command = command;
_parameter = parameter.ToString();
_address = device.SlaveAddress;
Name = command;
}
protected KaimeiRFMatchHandler(KaimeiRFMatch device, string command, string parameter1, ushort parameter2, ushort parameter3)
: base(BuildMessage(ToByteArray(command), ToByteArray(parameter1), UshortToByteArray(parameter2), UshortToByteArray(parameter3)))
{
Device = device;
_command = command;
_parameter = $"{parameter1},{parameter2},{parameter3}";
_address = device.SlaveAddress;
Name = command;
}
protected KaimeiRFMatchHandler(KaimeiRFMatch device, string command, string parameter)
: base(BuildMessage(ToByteArray(command), ToByteArray(parameter)))
{
Device = device;
_command = command;
_parameter = parameter;
_address = device.SlaveAddress;
Name = command;
}
protected KaimeiRFMatchHandler(KaimeiRFMatch device, string command, byte[] parameter)
: base(BuildMessage(ToByteArray(command), parameter))
{
Device = device;
_command = command;
//_parameter = parameter;
Name = command;
}
private static byte _address = 0x01;
protected static byte[] BuildMessage(byte[] commandArray, byte[] argumentArray1 = null, byte[] argumentArray2 = null, byte[] argumentArray3 = null)
{
List<byte> buffer = new List<byte>();
buffer.Add(_address);
if (commandArray != null && commandArray.Length > 0)
{
buffer.AddRange(commandArray);
}
if (argumentArray1 != null && argumentArray1.Length > 0)
{
buffer.AddRange(argumentArray1);
}
if (argumentArray2 != null && argumentArray2.Length > 0)
{
buffer.AddRange(argumentArray2.Reverse());
}
if (argumentArray3 != null && argumentArray3.Length > 0)
{
buffer.AddRange(argumentArray3.Reverse());
}
var checkSum = Crc16.CRC16_ModbusRTU(buffer.ToArray());
var ret = BitConverter.GetBytes(checkSum);
buffer.AddRange(ret);
return buffer.ToArray();
}
protected bool HandleMessage(MessageBase msg)
{
KaimeiRFMatchMessage response = msg as KaimeiRFMatchMessage;
if (!response.IsComplete)
return false;
return true;
}
protected static string Ushort2String(ushort num)
{
var bytes = UshortToByteArray(num);
return string.Join(",", bytes,0);
}
protected static byte[] ToByteArray(string parameter)
{
if (parameter == null)
return new byte[] { };
return parameter.Split(',').Select(para => Convert.ToByte(para, 16)).ToArray();
}
protected static byte[] IntToByteArray(int num)
{
byte[] bytes = BitConverter.GetBytes(num);
return bytes;
}
protected static byte[] UshortToByteArray(ushort num)
{
byte[] bytes = BitConverter.GetBytes(num);
return bytes;
}
protected static byte[] ShortToByteArray(short num)
{
byte[] bytes = BitConverter.GetBytes(num);
return bytes;
}
protected int ByteArrayToInt(byte[] bytes)
{
if (bytes.Length == 4)
{
int temp = BitConverter.ToInt32(bytes, 0);
return temp;
}
return 0;
}
protected short ByteArrayToShort(byte[] bytes)
{
if (bytes.Length == 2)
{
short temp = BitConverter.ToInt16(bytes, 0);
return temp;
}
return 0;
}
protected ushort ByteArrayToUshort(byte[] bytes)
{
if (bytes.Length == 2)
{
ushort temp = BitConverter.ToUInt16(bytes, 0);
return temp;
}
return 0;
}
}
public class KaimeiRFMatchSetCurrentValveHandler : KaimeiRFMatchHandler
{
public KaimeiRFMatchSetCurrentValveHandler(KaimeiRFMatch device)
: base(device, "10", $"00,08,00,02,04,FF,FF,FF,FF")
{
}
public override bool HandleMessage(MessageBase msg, out bool handled)
{
if (HandleMessage(msg))
{
var result = msg as KaimeiRFMatchMessage;
handled = true;
return true;
}
handled = false;
return false;
}
}
public class KaimeiRFMatchPresetValveHandler : KaimeiRFMatchHandler
{
public KaimeiRFMatchPresetValveHandler(KaimeiRFMatch device, EnumRfMatchTuneMode mode, float tune, float load)
: base(device, "10", $"00,05,00,03,06,{(mode == EnumRfMatchTuneMode.Auto ? "00,00":"00,01")}", (ushort)((0x1 << 12) + (((int)tune * 10) & 0x0FFF)), (ushort)((0x1 << 12) + (((int)load * 10) & 0x0FFF)))
{
}
public override bool HandleMessage(MessageBase msg, out bool handled)
{
if (HandleMessage(msg))
{
var result = msg as KaimeiRFMatchMessage;
handled = true;
return true;
}
handled = false;
return false;
}
}
public class KaimeiRFMatchGetStatusHandler : KaimeiRFMatchHandler
{
public KaimeiRFMatchGetStatusHandler(KaimeiRFMatch device)
: base(device, "03", "00,00,00,06")
{
}
public override bool HandleMessage(MessageBase msg, out bool handled)
{
if (HandleMessage(msg))
{
var result = msg as KaimeiRFMatchMessage;
var datas = result.Data.ToList();
if (datas.Count == 12)//ox00 no error
{
var tuneArray = datas.Skip(6).Take(2).Reverse().ToArray();
var loadArray = datas.Skip(8).Take(2).Reverse().ToArray();
var modeArray = datas.Skip(10).Take(2).Reverse().ToArray();
Device.TunePosition1 = BitConverter.ToInt16(tuneArray,0)/10.0f;
Device.LoadPosition1 = BitConverter.ToInt16(loadArray, 0)/10.0f;
Device.TuneMode1 = modeArray[1] == 0x00 ? EnumRfMatchTuneMode.Auto : EnumRfMatchTuneMode.Manual;
var reason = string.Empty;
Device.NoteError(reason);
}
handled = true;
return true;
}
handled = false;
return false;
}
}
}