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,381 @@
using System;
using System.Collections.Generic;
using Aitex.Core.Common.DeviceData;
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.Common.Device.Bases;
using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Common;
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Pumps.Hanbell
{
public class HanbellPump : PumpBase, IConnection
{
public string Address { get { return _slaveID; } }
public bool IsConnected { get; }
public bool Connect()
{
return true;
}
public bool Disconnect()
{
return true;
}
public string PortStatus { get; set; } = "Closed";
private HanbellPumpTCPConnection _connection;
public HanbellPumpTCPConnection Connection
{
get { return _connection; }
}
public override AITPumpData DeviceData
{
get
{
AITPumpData data = new AITPumpData()
{
IsOn = IsOn,
IsError = IsError,
OverTemp = IsOverTemperature,
DeviceModule = Module,
DeviceName = "Pump",
};
return data;
}
}
private R_TRIG _trigError = new R_TRIG();
private R_TRIG _trigWarningMessage = 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 _scRoot;
private string _slaveID = "158";
public HanbellPump(string module, string name, string scRoot) : base(module, name)
{
_scRoot = scRoot;
}
private void ResetPropertiesAndResponses()
{
foreach (var ioResponse in IOResponseList)
{
ioResponse.ResonseContent = null;
ioResponse.ResonseRecievedTime = DateTime.Now;
}
}
public override bool Initialize()
{
base.Initialize();
ResetPropertiesAndResponses();
if (_connection != null && _connection.IsConnected)
return true;
string deviceIP = "";
if (!string.IsNullOrEmpty(_scRoot))
{
_enableLog = SC.GetValue<bool>($"{_scRoot}.{Module}.{Name}.EnableLogMessage");
deviceIP = SC.GetStringValue($"{_scRoot}.{Module}.{Name}.DeviceAddress");
}
else
{
_enableLog = SC.GetValue<bool>($"{Module}.{Name}.EnableLogMessage");
deviceIP = SC.GetStringValue($"{Module}.{Name}.DeviceAddress");
}
_connection = new HanbellPumpTCPConnection(deviceIP);
_connection.EnableLog(_enableLog);
if (_connection.Connect())
{
PortStatus = "Open";
EV.PostInfoLog(Module, $"{Module}.{Name} connected");
}
else
{
PortStatus = "Close";
EV.PostInfoLog(Module, $"{Module}.{Name} connect failed");
//return false;
}
System.Threading.Thread.Sleep(500);
_thread = new PeriodicJob(1000, OnTimer, $"{Module}.{Name} MonitorHandler", true);
//RequestRegisters(true, 2201, 45);
_lstMonitorHandler.AddFirst(new HanbellPumpRequestRegistersHandler(this, byte.Parse(_slaveID), 2201, 45));
return true;
}
private bool OnTimer()
{
try
{
//return true;
_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 HanbellPumpQueryPinHandler(this, _deviceAddress));
//_lstHandler.AddLast(new HanbellPumpSetCommModeHandler(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;
}
internal void NoteDPCurrentHigh(int value)
{
DPCurrentHigh = value == 1 ? "true" : "false";
}
internal void NoteBPCurrentHigh(int value)
{
BPCurrentHigh = value == 1 ? "true" : "false";
}
internal void NoteDPPower(int power)
{
DPPower = power;
}
internal void NoteBPPower(int power)
{
BPPower = power;
}
internal void NoteDPSpeed(int speed)
{
DPSpeed = speed;
}
internal void NoteBPSpeed(int speed)
{
BPSpeed = speed;
}
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);
}
}
internal void NoteVaccumPumpCompleted(bool isOn)
{
if (isOn)
{
VaccumPumpOnCompleted = true;
}
else
{
VaccumPumpOffCompleted = true;
}
}
internal void NoteBPPumpOnOff(bool isOn)
{
if (isOn)
{
BPPumpOnCompleted = true;
}
else
{
BPPumpOffCompleted = true;
}
}
public override void Reset()
{
_trigError.RST = true;
_connection.SetCommunicationError(false, "");
_trigCommunicationError.RST = true;
//_enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");
_trigRetryConnect.RST = true;
IsError = false;
base.Reset();
}
#region Command Functions
public void PerformRawCommand(string command, string comandArgument)
{
lock (_locker)
{
_lstHandler.AddLast(new HanbellPumpRawCommandHandler(this, byte.Parse(_slaveID), command, comandArgument));
}
}
public override void SetPumpOnOff(bool isOn)
{
if (isOn)
StartVaccumPump();
else
StopVaccumPump();
}
public void StartVaccumPump()
{
lock (_locker)
{
_lstHandler.AddFirst(new HanbellPumpStartVaccumPumpHandler(this, byte.Parse(_slaveID)));
}
}
public void StopVaccumPump()
{
lock (_locker)
{
_lstHandler.AddFirst(new HanbellPumpStopVaccumPumpHandler(this, byte.Parse(_slaveID)));
}
}
public void StartBPPump()
{
lock (_locker)
{
_lstHandler.AddFirst(new HanbellPumpStartBPPumpHandler(this, byte.Parse(_slaveID)));
}
}
public void StopBPPump()
{
lock (_locker)
{
_lstHandler.AddFirst(new HanbellPumpStopBPPumpHandler(this, byte.Parse(_slaveID)));
}
}
public void ResetPump()
{
lock (_locker)
{
_lstHandler.AddFirst(new HanbellPumpResetHandler(this, byte.Parse(_slaveID)));
}
}
#endregion
#region Properties
public string CommunicationMode { get; private set; }
public int DPPower { get; private set; }
public int BPPower { get; private set; }
public int DPSpeed { get; private set; }
public int BPSpeed { get; private set; }
public string BPCurrentHigh { get; private set; }
public string DPCurrentHigh { get; private set; }
public override bool IsOn { get; set; }
public bool VaccumPumpOnCompleted { get; private set; }
public bool VaccumPumpOffCompleted { get; private set; }
public bool BPPumpOnCompleted { get; private set; }
public bool BPPumpOffCompleted { get; private set; }
#endregion
#region Note Functions
internal void NoteRawCommandInfo(string command, string data)
{
var curIOResponse = IOResponseList.Find(res => res.SourceCommand == command);
if (curIOResponse != null)
{
IOResponseList.Remove(curIOResponse);
}
IOResponseList.Add(new IOResponse() { SourceCommand = command, ResonseContent = data, ResonseRecievedTime = DateTime.Now });
}
public void NoteError(string reason)
{
if (reason != null)
{
_trigWarningMessage.CLK = true;
if (_trigWarningMessage.Q)
{
EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
}
IsError = true;
}
}
#endregion
}
}
@@ -0,0 +1,116 @@
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using Aitex.Core.RT.Log;
using MECF.Framework.Common.Communications;
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Pumps.Hanbell
{
public class HanbellPumpMessage : BinaryMessage
{
public byte Address { get; set; }
public byte FunctionCode { get; set; }
public byte[] Data { get; set; }
}
public class HanbellPumpConnection : SerialPortConnectionBase
{
private string _cachedBuffer = string.Empty;
public HanbellPumpConnection(string portName, int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
: base(portName, baudRate, dataBits, parity, stopBits, "\r", false)
{
}
public override bool SendMessage(byte[] message)
{
return base.SendMessage(message);
}
protected override MessageBase ParseResponse(byte[] rawMessage)
{
HanbellPumpMessage msg = new HanbellPumpMessage();
msg.RawMessage = rawMessage;
//if (temps.Length < 4) return msg;
msg.Address = rawMessage[0];
msg.FunctionCode = rawMessage[1];
//msg.Length = temps[2];
if (rawMessage.Length < 4)
{
msg.IsFormatError = true;
LOG.Error($"invalid response");
return msg;
}
msg.Data = rawMessage.Skip(2).Take(rawMessage.Length - 2).ToArray();
msg.IsResponse = true;
msg.IsAck = true;
msg.IsComplete = true;
return msg;
}
}
public class HanbellPumpTCPConnection : TCPPortConnectionBase
{
private List<byte> _msgBuffer = new List<byte>();
public HanbellPumpTCPConnection(string address)
: base(address, "", false)
{
}
public override bool SendMessage(byte[] message)
{
return base.SendMessage(message);
}
protected override MessageBase ParseResponse(byte[] rawMessage)
{
HanbellPumpMessage msg = new HanbellPumpMessage();
if (rawMessage == null || rawMessage.Length == 0)
{
return msg;
}
_msgBuffer.AddRange(rawMessage);
if(_msgBuffer.Count < 12)
{
return msg;
}
if(_msgBuffer[7] == 0x05)
{
msg.RawMessage = _msgBuffer.Take(12).ToArray();
msg.Address = msg.RawMessage[6];
msg.FunctionCode = msg.RawMessage[7];
msg.IsResponse = true;
msg.IsAck = true;
msg.IsComplete = true;
_msgBuffer.RemoveRange(0,12);
}
else if(_msgBuffer[7] == 0x03)
{
if(_msgBuffer.Count >= 99)
{
msg.RawMessage = _msgBuffer.Take(99).ToArray();
msg.Data = _msgBuffer.Skip(8).Take(91).ToArray();
msg.Address = msg.RawMessage[6];
msg.FunctionCode = msg.RawMessage[7];
msg.IsResponse = true;
msg.IsAck = true;
msg.IsComplete = true;
_msgBuffer.RemoveRange(0, 99);
}
}
return msg;
}
}
}
@@ -0,0 +1,284 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MECF.Framework.Common.Communications;
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Pumps.Hanbell
{
public abstract class HanbellPumpHandler : HandlerBase
{
public HanbellPump Device { get; }
private byte _address;
protected byte _command;
protected HanbellPumpHandler(HanbellPump device, byte address, byte command, byte[] data)
: base(BuildMessage(address, command, data))
{
Device = device;
_address = address;
_command = command;
}
private static byte[] BuildMessage(byte address, byte command, byte[] data)
{
List<byte> buffer = new List<byte>();
//MODBUS TCP
buffer.Add(0x00);
buffer.Add(0x00);
buffer.Add(0x00);
buffer.Add(0x00);
int length = 2 + (data != null ? data.Length:0);
buffer.Add((byte)((length >> 8) & 0xFF));
buffer.Add((byte)(length & 0xFF));
buffer.Add(address);
buffer.Add(command);
if (data != null && data.Length > 0)
{
buffer.AddRange(data);
}
// CRC ??
//buffer.Add(CalcSum(buffer, buffer.Count));
return buffer.ToArray();
}
//host->unit, unit->host(ack), unit->host(csr), host->unit(ack)
public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
{
HanbellPumpMessage response = msg as HanbellPumpMessage;
ResponseMessage = msg;
Device.NoteError(null);
if (response.Address != _address || response.FunctionCode != _command)
{
transactionComplete = false;
return false;
}
if (response.IsComplete)
{
SetState(EnumHandlerState.Completed);
transactionComplete = true;
return true;
}
transactionComplete = false;
return false;
}
public void SendAck()
{
Device.Connection.SendMessage(new byte[] { 0x06 });
}
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;
}
}
public class HanbellPumpRequestRegistersHandler : HanbellPumpHandler
{
public HanbellPumpRequestRegistersHandler(HanbellPump device, byte address, int startAddress, int registerCount)
: base(device, address, 0x03, BuildData(startAddress, registerCount))
{
Name = "RequestRegisters";
}
private static byte[] BuildData(int startAddress, int registerCount)
{
return new byte[] { (byte)(startAddress >> 8), (byte)startAddress, (byte)(registerCount >> 8), (byte)registerCount };
}
public override bool HandleMessage(MessageBase msg, out bool handled)
{
if (base.HandleMessage(msg, out handled))
{
var result = msg as HanbellPumpMessage;
if (result.Data.Length < 91)//91=data length + registerCount * 2
{
Device.NoteError("invalid response for command RequestRegisters");
}
else
{
//2031
var digitalOutputStatus1High = result.Data[61];
var digitalOutputStatus1Low = result.Data[62];
Device.IsOn = ((digitalOutputStatus1Low & 0x00000002) == 0) || ((digitalOutputStatus1Low & 0x00000001) == 0);
Device.NoteDPPower(result.Data[1] * 256 + result.Data[2]);
Device.NoteBPPower(result.Data[3] * 256 + result.Data[4]);
Device.NoteDPSpeed(result.Data[5] * 256 + result.Data[6]);
Device.NoteBPSpeed(result.Data[37] * 256 + result.Data[8]);
int waring0 = result.Data[45] * 256 + result.Data[46];
Device.NoteDPCurrentHigh(waring0 & (1 << 15));
Device.NoteBPCurrentHigh(waring0 & (1 << 14));
int waring1 = result.Data[47] * 256 + result.Data[48];
int waring2 = result.Data[49] * 256 + result.Data[50];
int alarm0 = result.Data[51] * 256 + result.Data[52];
int alarm1 = result.Data[53] * 256 + result.Data[54];
int alarm2 = result.Data[55] * 256 + result.Data[56];
Device.IsError = ((digitalOutputStatus1Low & 0x0000004) == 0) || ((digitalOutputStatus1Low & 0x000008) == 0) ||
waring0 + waring1 + waring2 != 0 || alarm0 + alarm1 + alarm2 != 0;
//if (waring0 + waring1 + waring2 != 0)
//{
// Device.NoteError("pump is in warning status");
//}
//if (alarm0 + alarm1 + alarm2 != 0)
//{
// Device.NoteError("pump is in alarm status");
//}
}
}
return true;
}
}
public class HanbellPumpStartVaccumPumpHandler : HanbellPumpHandler
{
public HanbellPumpStartVaccumPumpHandler(HanbellPump device, byte address)
: base(device, address, 0x05, BuildData())
{
Name = "StartVaccumPump";
}
private static byte[] BuildData()
{
return new byte[] { 0x00,0x0A,0xFF,0x00 };
}
public override bool HandleMessage(MessageBase msg, out bool handled)
{
if (base.HandleMessage(msg, out handled))
{
Device.NoteVaccumPumpCompleted(true);
}
return true;
}
}
public class HanbellPumpStopVaccumPumpHandler : HanbellPumpHandler
{
public HanbellPumpStopVaccumPumpHandler(HanbellPump device, byte address)
: base(device, address, 0x05, BuildData())
{
Name = "StopVaccumPump";
}
private static byte[] BuildData()
{
return new byte[] { 0x00, 0x0A, 0x00, 0x00 };
}
public override bool HandleMessage(MessageBase msg, out bool handled)
{
if (base.HandleMessage(msg, out handled))
{
Device.NoteVaccumPumpCompleted(false);
}
return true;
}
}
public class HanbellPumpStartBPPumpHandler : HanbellPumpHandler
{
public HanbellPumpStartBPPumpHandler(HanbellPump device, byte address)
: base(device, address, 0x05, BuildData())
{
Name = "StartBPPump";
}
private static byte[] BuildData()
{
return new byte[] { 0x00, 0x0B, 0xFF, 0x00 };
}
public override bool HandleMessage(MessageBase msg, out bool handled)
{
if (base.HandleMessage(msg, out handled))
{
Device.NoteBPPumpOnOff(true);
}
return true;
}
}
public class HanbellPumpStopBPPumpHandler : HanbellPumpHandler
{
public HanbellPumpStopBPPumpHandler(HanbellPump device, byte address)
: base(device, address, 0x05, BuildData())
{
Name = "StopBPPump";
}
private static byte[] BuildData()
{
return new byte[] { 0x00, 0x0B, 0x00, 0x00 };
}
public override bool HandleMessage(MessageBase msg, out bool handled)
{
if (base.HandleMessage(msg, out handled))
{
Device.NoteBPPumpOnOff(false);
}
return true;
}
}
public class HanbellPumpResetHandler : HanbellPumpHandler
{
public HanbellPumpResetHandler(HanbellPump device, byte address)
: base(device, address, 0x05, BuildData())
{
Name = "Reset";
}
private static byte[] BuildData()
{
return new byte[] { 0x00, 0x0C, 0xFF, 0x00 };
}
}
public class HanbellPumpRawCommandHandler : HanbellPumpHandler
{
public HanbellPumpRawCommandHandler(HanbellPump device, byte address, string command, string parameter = null)
: base(device, address, byte.Parse(command), BuildData(parameter))
{
}
private static byte[] BuildData(string parameter)
{
if (parameter == null)
return new byte[] { };
return parameter.Split(',').Select(para => Convert.ToByte(para,16)).ToArray();
}
public override bool HandleMessage(MessageBase msg, out bool handled)
{
base.HandleMessage(msg, out handled);
var result = msg as HanbellPumpMessage;
Device.NoteRawCommandInfo(_command.ToString("X2"), string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
return true;
}
}
}