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,173 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using MECF.Framework.Common.Communications;
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.AAF
{
public class DWPressureMessage : BinaryMessage
{
public byte DeviceAddress { get; set; }
public byte FunctionCode { get; set; }
public byte Length { get; set; }
public byte[] Data { get; set; }
}
public class DWPressureConnection : SerialPortConnectionBase
{
private List<byte> _lstCacheBuffer = new List<byte>();
public DWPressureConnection(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();
DWPressureMessage msg = new DWPressureMessage();
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];
msg.Length = temps[2];
if (temps.Length < msg.Length + 5) 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;
}
}
public abstract class DWPressureHandler : HandlerBase
{
public DWPressure Device { get; }
protected DWPressureHandler(DWPressure 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 DWPressureMessage;
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 DWPressureQueryPressureHandler : DWPressureHandler
{
public DWPressureQueryPressureHandler(DWPressure device, byte groupAddress)
: base(device, new byte[] { groupAddress, 3,0,0,0,1 })
{
Name = "Query pressure";
}
public override bool HandleMessage(MessageBase msg, out bool handled)
{
var result = msg as DWPressureMessage;
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 (result.DeviceAddress == 1)
{
Device.PressureValue1 = rvalue;
}
else
{
Device.PressureValue2 = rvalue;
}
ResponseMessage = msg;
handled = true;
Thread.Sleep(2000);
return true;
}
}
}
@@ -0,0 +1,301 @@
using System;
using System.Collections.Generic;
using System.Threading;
using Aitex.Core.RT.DataCenter;
using Aitex.Core.RT.Device;
using Aitex.Core.RT.Event;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.SCCore;
using Aitex.Core.Util;
using MECF.Framework.Common.Communications;
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.AAF
{
public class DWPressure : BaseDevice, IConnection
{
private DWPressureConnection _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 pressureCount = 1;
private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
private object _locker = new object();
private bool _enableLog = true;
private string _scRoot;
private int _pressureValue1 = 999;
private int _pressureValue2 = 888;
private int _pressureValue3 = 777;
public int PressureValue1
{
get { return _pressureValue1; }
set { _pressureValue1 = value; }
}
public int PressureValue2
{
get { return _pressureValue2; }
set { _pressureValue2 = value; }
}
public int PressureValue3
{
get { return _pressureValue3; }
set { _pressureValue3 = value; }
}
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 DWPressure(string module, string name, string scRoot) : base(module, name, name, name)
{
_scRoot = scRoot;
_activeMonitorStatus = true;
}
~DWPressure()
{
_connection.Disconnect();
}
public DWPressure(string module, string name, string scRoot, int count) : base(module, name, name, name)
{
pressureCount = count;
_scRoot = scRoot;
_activeMonitorStatus = true;
}
public void QueryPressure()
{
if (pressureCount == 1)
{ _lstHandler.AddLast(new DWPressureQueryPressureHandler(this, 1)); }
if (pressureCount == 2)
{
_lstHandler.AddLast(new DWPressureQueryPressureHandler(this, 1));
_lstHandler.AddLast(new DWPressureQueryPressureHandler(this, 3));
}
if (pressureCount == 3)
{
_lstHandler.AddLast(new DWPressureQueryPressureHandler(this, 1));
_lstHandler.AddLast(new DWPressureQueryPressureHandler(this, 3));
_lstHandler.AddLast(new DWPressureQueryPressureHandler(this, 5));
}
}
public void ResetDevice()
{
}
public void QueryError()
{
EV.PostInfoLog(Module, "Query error");
}
public bool Initialize()
{
//DATA.Subscribe($"{Module}.{Name}.Power", () => _power);
//DATA.Subscribe($"{Module}.{Name}.ErrorCode", () => _errorCode);
//DATA.Subscribe($"{Module}.{Name}.IsAtSpeed", () => _isAtSpeed);
//DATA.Subscribe($"{Module}.{Name}.IsAccelerate", () => _isAccelerate);
string portName = SC.GetStringValue($"{_scRoot}.{Name}.Address");
Address = portName;
int address = SC.GetValue<int>($"{_scRoot}.{Name}.DeviceAddress");
_enableLog = SC.GetValue<bool>($"{_scRoot}.{Name}.EnableLogMessage");
_connection = new DWPressureConnection(portName);
_connection.EnableLog(_enableLog);
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($"{Name}.DiffPressure1", () => _pressureValue1);
DATA.Subscribe($"{Name}.DiffPressure2", () => _pressureValue2);
DATA.Subscribe($"{Name}.DiffPressure3", () => _pressureValue3);
ConnectionManager.Instance.Subscribe($"{Name}", this);
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($"{_scRoot}.{Name}.Address"));
if (!_connection.Connect())
{
EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
}
}
_connection.ForceClear();
return true;
}
HandlerBase handler = null;
//if (!_connection.IsBusy)
//{
lock (_locker)
{
if(_lstHandler.Count == 0)
QueryPressure();
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);
_trigCommunicationError.CLK = _connection.IsCommunicationError;
if (_trigCommunicationError.Q)
{
EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
}
}
catch (Exception ex)
{
LOG.Write(ex);
}
}
public void Reset()
{
_connection.SetCommunicationError(false, "");
_trigCommunicationError.RST = true;
_enableLog = SC.GetValue<bool>($"{Module}.{Name}.EnableLogMessage");
_trigRetryConnect.RST = true;
}
public void SetPumpOnOff(bool isOn)
{
lock (_locker)
{
}
}
public void SetActiveMonitor(bool active)
{
_activeMonitorStatus = active;
}
public void SetErrorCode(int errorCode)
{
_errorCode = errorCode;
}
public void Terminate()
{
_connection.Disconnect();
}
}
}