首次上传
This commit is contained in:
@@ -0,0 +1,451 @@
|
||||
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.OperationCenter;
|
||||
using Aitex.Core.RT.SCCore;
|
||||
using Aitex.Core.Util;
|
||||
using MECF.Framework.Common.Communications;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.AAF
|
||||
{
|
||||
public class FfuAAF : BaseDevice, IConnection
|
||||
{
|
||||
private FfuAAFConnection _connection;
|
||||
|
||||
private string _deviceAddress = "001";
|
||||
|
||||
private bool _isOn;
|
||||
|
||||
private bool _activeMonitorStatus;
|
||||
|
||||
private int _nMaxSpeed;
|
||||
public int NMaxSpeed { get => _nMaxSpeed; }
|
||||
|
||||
private int _errorCode;
|
||||
|
||||
private RD_TRIG _trigPumpOnOff = new RD_TRIG();
|
||||
private R_TRIG _trigError = new R_TRIG();
|
||||
private R_TRIG _trigOverTemp = new R_TRIG();
|
||||
|
||||
private R_TRIG _trigWarningMessage = new R_TRIG();
|
||||
private string _lastError = string.Empty;
|
||||
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 object _locker = new object();
|
||||
|
||||
private bool _enableLog = true;
|
||||
|
||||
private string _scRoot;
|
||||
|
||||
private int _ffu1Speed = 88;
|
||||
private int _ffu2Speed = 77;
|
||||
private int _ffu3Speed;
|
||||
|
||||
private int _ffuCount;
|
||||
|
||||
public int FFU1Speed
|
||||
{
|
||||
get { return _ffu1Speed; }
|
||||
set { _ffu1Speed = value; }
|
||||
}
|
||||
public int FFU2Speed
|
||||
{
|
||||
get { return _ffu2Speed; }
|
||||
set { _ffu2Speed = value; }
|
||||
}
|
||||
public int FFU3Speed
|
||||
{
|
||||
get { return _ffu3Speed; }
|
||||
set { _ffu3Speed = 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 FfuAAF(string module, string name, string scRoot) : base(module, name, name, name)
|
||||
{
|
||||
_scRoot = scRoot;
|
||||
_activeMonitorStatus = true;
|
||||
_ffuCount = 1;
|
||||
}
|
||||
public FfuAAF(string module, string name, string scRoot, int ffucount) : base(module, name, name, name)
|
||||
{
|
||||
_scRoot = scRoot;
|
||||
_activeMonitorStatus = true;
|
||||
_ffuCount = ffucount;
|
||||
}
|
||||
|
||||
~FfuAAF()
|
||||
{
|
||||
_connection.Disconnect();
|
||||
|
||||
}
|
||||
|
||||
public void QuerySpeedFanaddress(int fanaddress)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
_lstHandler.Clear();
|
||||
if (_connection.IsBusy) _connection.ForceClear();
|
||||
|
||||
List<byte> cmddata = new List<byte>() { 0x15, (byte)(0x20 + fanaddress), 0x1 };
|
||||
byte checksum = ModRTU_CRC(cmddata.ToArray());
|
||||
cmddata.Add(checksum);
|
||||
_connection.SendMessage(cmddata.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
public void QuerySpeed()
|
||||
{
|
||||
Thread.Sleep(500);
|
||||
_lstHandler.AddLast(new FfuAAFQuerySpeedHandler(this, 1, 1));
|
||||
|
||||
if (_ffuCount == 2)
|
||||
{
|
||||
Thread.Sleep(500);
|
||||
_lstHandler.AddLast(new FfuAAFQuerySpeedHandler(this, 3, 1));
|
||||
}
|
||||
if (_ffuCount == 3)
|
||||
{
|
||||
Thread.Sleep(500);
|
||||
_lstHandler.AddLast(new FfuAAFQuerySpeedHandler(this, 5, 1));
|
||||
}
|
||||
}
|
||||
|
||||
public void ResetDevice()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void QueryError()
|
||||
{
|
||||
|
||||
_lstHandler.AddLast(new FfuAAFQueryStatusHandler(this, 1, 1));
|
||||
_lstHandler.AddLast(new FfuAAFQueryStatusHandler(this, 3, 1));
|
||||
|
||||
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");
|
||||
|
||||
_deviceAddress = address.ToString("D3");
|
||||
_enableLog = SC.GetValue<bool>($"{_scRoot}.{Name}.EnableLogMessage");
|
||||
|
||||
_nMaxSpeed = SC.GetValue<int>($"{_scRoot}.{Name}.MaxSpeed");
|
||||
_connection = new FfuAAFConnection(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(1000, OnTimer, $"{Name} MonitorHandler", true);
|
||||
DATA.Subscribe($"{Name}.FFU1Speed", () => _ffu1Speed);
|
||||
DATA.Subscribe($"{Name}.FFU2Speed", () => _ffu2Speed);
|
||||
DATA.Subscribe($"{Name}.FFU3Speed", () => _ffu3Speed);
|
||||
|
||||
OP.Subscribe($"{Name}.SetSpeed", (cmd, param) =>
|
||||
{
|
||||
if (!int.TryParse((string)param[0], out var speed))
|
||||
{
|
||||
EV.PostWarningLog(Module, "invalid speed.");
|
||||
return false;
|
||||
}
|
||||
SetSpeed(speed);
|
||||
|
||||
EV.PostInfoLog(Module, $"{Name} speed set to {speed}");
|
||||
return true;
|
||||
});
|
||||
|
||||
|
||||
OP.Subscribe($"{Name}.SetFFUSpeedBySC", (cmd, param) =>
|
||||
{
|
||||
int speed = SC.GetValue<int>("System.FFUFanSpeedSetPoint");
|
||||
SetSpeed(speed);
|
||||
EV.PostInfoLog(Module, $"Set {Name} speed");
|
||||
return true;
|
||||
});
|
||||
OP.Subscribe("FFU1.SetFFUSpeedBySC", (cmd, param) =>
|
||||
{
|
||||
int speed = SC.GetValue<int>("System.FFU1FanSpeedSetPoint");
|
||||
SetSpeed(speed,1);
|
||||
EV.PostInfoLog(Module, "Set FFU1 speed");
|
||||
return true;
|
||||
});
|
||||
OP.Subscribe("FFU2.SetFFUSpeedBySC", (cmd, param) =>
|
||||
{
|
||||
int speed = SC.GetValue<int>("System.FFU2FanSpeedSetPoint");
|
||||
SetSpeed(speed, 3);
|
||||
EV.PostInfoLog(Module, "Set FFU2 speed");
|
||||
return true;
|
||||
});
|
||||
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}");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
HandlerBase handler = null;
|
||||
QuerySpeed();
|
||||
if (!_connection.IsBusy)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
//if (_lstHandler.Count == 0 && _activeMonitorStatus)
|
||||
//{
|
||||
|
||||
//}
|
||||
|
||||
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 NoteError()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Monitor()
|
||||
{
|
||||
try
|
||||
{
|
||||
_connection.EnableLog(_enableLog);
|
||||
|
||||
_trigPumpOnOff.CLK = _isOn;
|
||||
if (_trigPumpOnOff.R)
|
||||
{
|
||||
EV.PostInfoLog(Module, $"{Module}.{Name} is on");
|
||||
}
|
||||
|
||||
if (_trigPumpOnOff.T)
|
||||
{
|
||||
EV.PostInfoLog(Module, $"{Module}.{Name} is off");
|
||||
}
|
||||
|
||||
|
||||
_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()
|
||||
{
|
||||
_trigError.RST = true;
|
||||
_trigOverTemp.RST = true;
|
||||
_trigWarningMessage.RST = true;
|
||||
|
||||
_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;
|
||||
}
|
||||
|
||||
|
||||
internal void NoteOnOff(bool isOn)
|
||||
{
|
||||
_isOn = isOn;
|
||||
}
|
||||
|
||||
|
||||
public void SetSpeed(float speed)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
_lstHandler.Clear();
|
||||
if (_connection.IsBusy) _connection.ForceClear();
|
||||
|
||||
List<byte> cmddata = new List<byte>() { 0x35, 0x40, 0x0, (byte)(speed * 250 / _nMaxSpeed) };
|
||||
byte checksum = ModRTU_CRC(cmddata.ToArray());
|
||||
cmddata.Add(checksum);
|
||||
_connection.SendMessage(cmddata.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
public void SetSpeed(float speed,int ffuAddress)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
_lstHandler.Clear();
|
||||
if (_connection.IsBusy) _connection.ForceClear();
|
||||
|
||||
List<byte> cmddata = new List<byte>() { 0x35, (byte)(0x40+ ffuAddress), 0x01, (byte)(speed * 250 / _nMaxSpeed) };
|
||||
byte checksum = ModRTU_CRC(cmddata.ToArray());
|
||||
cmddata.Add(checksum);
|
||||
_connection.SendMessage(cmddata.ToArray());
|
||||
}
|
||||
}
|
||||
public void SetErrorCode(int errorCode)
|
||||
{
|
||||
_errorCode = errorCode;
|
||||
}
|
||||
|
||||
|
||||
public void SetError(string reason)
|
||||
{
|
||||
_trigWarningMessage.CLK = true;
|
||||
if (_trigWarningMessage.Q)
|
||||
{
|
||||
EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
|
||||
|
||||
}
|
||||
}
|
||||
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 void Terminate()
|
||||
{
|
||||
_connection.Disconnect();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MECF.Framework.Common.Communications;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.AAF
|
||||
{
|
||||
public class FfuAAFMessage : BinaryMessage
|
||||
{
|
||||
public byte Preamble { get; set; }
|
||||
public byte Command { get; set; }
|
||||
public byte GroupAddress { get; set; }
|
||||
public byte Data1 { get; set; }
|
||||
public byte Data2 { get; set; }
|
||||
}
|
||||
|
||||
public class FfuAAFConnection : SerialPortConnectionBase
|
||||
{
|
||||
private List<byte> _lstCacheBuffer = new List<byte>();
|
||||
public FfuAAFConnection(string portName) : base(portName,9600,8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One,"\r",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();
|
||||
|
||||
FfuAAFMessage msg = new FfuAAFMessage();
|
||||
msg.IsResponse = false;
|
||||
msg.IsAck = false;
|
||||
msg.IsComplete = false;
|
||||
msg.RawMessage = _lstCacheBuffer.ToArray();
|
||||
|
||||
if (temps.Length < 4) return msg;
|
||||
|
||||
if (temps.LastOrDefault() != ModRTU_CRC(temps.Take(temps.Length - 1).ToArray()))
|
||||
return msg;
|
||||
|
||||
msg.Preamble = temps[0];
|
||||
msg.Command = temps[1];
|
||||
msg.GroupAddress = temps[2];
|
||||
if (temps.Length > 4) msg.Data1 = temps[3];
|
||||
if (temps.Length > 5) msg.Data2 = temps[4];
|
||||
|
||||
|
||||
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,154 @@
|
||||
using System.Collections.Generic;
|
||||
using MECF.Framework.Common.Communications;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.AAF
|
||||
{
|
||||
public abstract class FfuAAFHandler : HandlerBase
|
||||
{
|
||||
public FfuAAF Device { get; }
|
||||
public byte FanAddress;
|
||||
|
||||
public bool IsNeedFeedback;
|
||||
|
||||
protected FfuAAFHandler(FfuAAF device, byte prefix, byte command, byte groupAddress, byte[] datas)
|
||||
: base(BuildMessage(prefix, command, groupAddress, datas))
|
||||
{
|
||||
Device = device;
|
||||
IsNeedFeedback = true;
|
||||
}
|
||||
protected FfuAAFHandler(FfuAAF device, byte prefix, byte command, byte groupAddress, byte[] datas,bool needfeedback)
|
||||
: base(BuildMessage(prefix, command, groupAddress, datas))
|
||||
{
|
||||
Device = device;
|
||||
IsNeedFeedback = needfeedback;
|
||||
}
|
||||
|
||||
private static byte[] BuildMessage(byte prefix, byte command, byte groupAddress,byte[] datas)
|
||||
{
|
||||
List<byte> result = new List<byte>() { prefix, command , groupAddress };
|
||||
if (datas != null)
|
||||
{
|
||||
foreach(byte data in datas)
|
||||
result.Add(data);
|
||||
}
|
||||
result.Add(ModRTU_CRC(result.ToArray()));
|
||||
return result.ToArray();
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
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;
|
||||
byte temp = buffer[0];
|
||||
|
||||
for (int i = 1; i < buffer.Length; i++)
|
||||
{
|
||||
temp = (byte)(temp ^ buffer[i]);
|
||||
}
|
||||
return (byte)~temp;
|
||||
}
|
||||
}
|
||||
|
||||
public class FfuAAFQuerySpeedHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFQuerySpeedHandler(FfuAAF device,byte fanaddress, byte groupAddress)
|
||||
: base(device, 0x15, (byte)(0x20 + fanaddress), groupAddress, null)
|
||||
{
|
||||
Name = "Query Speed";
|
||||
FanAddress = fanaddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
|
||||
if (FanAddress == 1) Device.FFU1Speed = result.Data1 * Device.NMaxSpeed / 250;
|
||||
if (FanAddress == 3) Device.FFU2Speed = result.Data1 * Device.NMaxSpeed / 250;
|
||||
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class FfuAAFQueryStatusHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFQueryStatusHandler(FfuAAF device,byte fanaddress, byte groupAddress)
|
||||
: base(device, 0x35, fanaddress, groupAddress, new byte[] { 0x00 })
|
||||
{
|
||||
Name = "Query Status";
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
//if (result.IsError)
|
||||
//{
|
||||
// Device.SetError(result.Data);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// if (int.TryParse(result.Data, out int code))
|
||||
// {
|
||||
// Device.SetErrorCode(code);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Device.SetError(result.Data + "format error");
|
||||
// }
|
||||
//}
|
||||
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class FfuAAFSetSpeedHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFSetSpeedHandler(FfuAAF device, byte groupAddress,int speed)
|
||||
: base(device, 0x35, 0x00, groupAddress, new byte[] { (byte)(speed*250/device.NMaxSpeed) },false)
|
||||
{
|
||||
Name = "Query Status";
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
//if (result.IsError)
|
||||
//{
|
||||
// Device.SetError(result.Data);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// if (int.TryParse(result.Data, out int code))
|
||||
// {
|
||||
// Device.SetErrorCode(code);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Device.SetError(result.Data + "format error");
|
||||
// }
|
||||
//}
|
||||
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,650 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
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 MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.AAF2
|
||||
{
|
||||
public class FfuAAF : BaseDevice, IConnection, IDevice
|
||||
{
|
||||
private FfuAAFConnection _connection;
|
||||
|
||||
private string _deviceAddress = "001";
|
||||
|
||||
public byte DeviceAddress { get; set; }
|
||||
|
||||
|
||||
public byte GroupAddress { get; set; }
|
||||
|
||||
public byte Speedfactor { get; set; }
|
||||
|
||||
public int MaxSpeed1 { get; set; }
|
||||
|
||||
public int MaxSpeed2 { get; set; }
|
||||
|
||||
public int MaxSpeed3 { get; set; }
|
||||
|
||||
public int Status { get; set; }
|
||||
|
||||
|
||||
private bool _isOn;
|
||||
|
||||
private bool _activeMonitorStatus;
|
||||
|
||||
private int _nMaxSpeed = 0;
|
||||
public int NMaxSpeed { get => _nMaxSpeed; }
|
||||
|
||||
private int _errorCode;
|
||||
|
||||
private RD_TRIG _trigPumpOnOff = new RD_TRIG();
|
||||
private R_TRIG _trigError = new R_TRIG();
|
||||
private R_TRIG _trigOverTemp = new R_TRIG();
|
||||
|
||||
private R_TRIG _trigWarningMessage = new R_TRIG();
|
||||
private string _lastError = string.Empty;
|
||||
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 object _locker = new object();
|
||||
|
||||
private bool _enableLog = true;
|
||||
|
||||
private string _scRoot;
|
||||
|
||||
//private AsyncSerial serialPort;
|
||||
|
||||
private int _ffuSpeed;
|
||||
|
||||
private string[] StatusStringList = new string[] { "normal", "running fault", "stop fault" };
|
||||
|
||||
public int Speed
|
||||
{
|
||||
get { return _ffuSpeed; }
|
||||
set { _ffuSpeed = 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 FfuAAF(string scRoot, string module, string name) : base(module, name, name, name)
|
||||
{
|
||||
|
||||
_scRoot = scRoot;
|
||||
_activeMonitorStatus = true;
|
||||
//serialPort = new AsyncSerial(port, 9600, 8);
|
||||
//serialPort.OnDataChanged += (OnDataChanged);
|
||||
//serialPort.OnErrorHappened += (OnErrorHandler);
|
||||
}
|
||||
|
||||
private void OnErrorHandler(ErrorEventArgs args)
|
||||
{
|
||||
}
|
||||
|
||||
private void OnDataChanged(string message)
|
||||
{
|
||||
}
|
||||
|
||||
public void SetGroupAddress(byte newGroupAddress)
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFSetGroupAddressHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x00, newGroupAddress }));
|
||||
}
|
||||
|
||||
public void GetGroupAddress()
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFGetGroupAddressHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x00 }));
|
||||
}
|
||||
|
||||
public void SetDeviceAddress(byte newDeviceAddress)
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFSetAddressHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x01, newDeviceAddress }));
|
||||
}
|
||||
|
||||
public void GetDeviceAddress()
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFGetAddressHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x01 }));
|
||||
}
|
||||
|
||||
public void QueryMaxSpeed1()
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFQueryMaxSpeed1Handler(this, DeviceAddress, GroupAddress, new byte[] { 0x08 }));
|
||||
}
|
||||
|
||||
public void QueryMaxSpeed2()
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFQueryMaxSpeed2Handler(this, DeviceAddress, GroupAddress, new byte[] { 0x09 }));
|
||||
}
|
||||
|
||||
public void QueryMaxSpeed3()
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFQueryMaxSpeed3Handler(this, DeviceAddress, GroupAddress, new byte[] { 0x0a }));
|
||||
}
|
||||
/// <summary>
|
||||
/// 查转速因子
|
||||
/// </summary>
|
||||
public void QuerySpeedFactor()
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFQuerySpeedFactorHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x11 }));
|
||||
}
|
||||
|
||||
public void SetSpeedFactor(byte newSpeedFactor)
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFSetSpeedFactorHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x11, newSpeedFactor }));
|
||||
}
|
||||
|
||||
public void QuerySpeed()
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFQuerySpeedHandler(this, DeviceAddress, GroupAddress));
|
||||
}
|
||||
|
||||
public void SetSpeed(int speed)
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFSetSpeedHandler(this, DeviceAddress, GroupAddress, new byte[] { (byte)(speed * 250.0 / MaxSpeed2) }));
|
||||
}
|
||||
|
||||
public void ResetDevice()
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFResetDeviceHandler(this, DeviceAddress, GroupAddress));
|
||||
}
|
||||
|
||||
public void QueryError()
|
||||
{
|
||||
|
||||
_lstHandler.AddLast(new FfuAAFQueryStatusHandler(this, DeviceAddress, GroupAddress));
|
||||
|
||||
EV.PostInfoLog(Module, "Query error");
|
||||
}
|
||||
|
||||
private int ChangedStatus(int value)
|
||||
{
|
||||
int returnValue = 0;
|
||||
switch (value)
|
||||
{
|
||||
case 0:
|
||||
returnValue = 0;
|
||||
break;
|
||||
case 0x80:
|
||||
returnValue = 1;
|
||||
break;
|
||||
case 0x10:
|
||||
returnValue = 2;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
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}.{Module}.{Name}.Address");
|
||||
Address = portName;
|
||||
|
||||
int address = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.DeviceAddress");
|
||||
|
||||
DeviceAddress = (byte)address;
|
||||
DATA.Subscribe($"{Name}.DeviceAddress", () => DeviceAddress);
|
||||
|
||||
GroupAddress = (byte)SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.GroupAddress");
|
||||
DATA.Subscribe($"{Name}.GroupAddress", () => GroupAddress);
|
||||
|
||||
Status = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.Status");
|
||||
DATA.Subscribe($"{Name}.Status", () => StatusStringList[ChangedStatus(Status)]);
|
||||
|
||||
_deviceAddress = address.ToString("D3");
|
||||
_enableLog = SC.GetValue<bool>($"{_scRoot}.{Module}.{Name}.EnableLogMessage");
|
||||
|
||||
Speedfactor = (byte)SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.Speedfactor");
|
||||
DATA.Subscribe($"{Name}.Speedfactor", () => Speedfactor);
|
||||
|
||||
// _nMaxSpeed = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.MaxSpeed1");
|
||||
MaxSpeed1 = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.MaxSpeed1");
|
||||
DATA.Subscribe($"{Name}.MaxSpeed1", () => MaxSpeed1);
|
||||
|
||||
MaxSpeed2 = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.MaxSpeed2");
|
||||
DATA.Subscribe($"{Name}.MaxSpeed2", () => MaxSpeed2);
|
||||
|
||||
MaxSpeed3 = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.MaxSpeed3");
|
||||
DATA.Subscribe($"{Name}.MaxSpeed3", () => MaxSpeed3);
|
||||
|
||||
_connection = new FfuAAFConnection(portName);
|
||||
_connection.EnableLog(_enableLog);
|
||||
|
||||
if (_connection.Connect())
|
||||
{
|
||||
EV.PostInfoLog(Module, $"{Module}.{Name} connected");
|
||||
}
|
||||
|
||||
_thread = new PeriodicJob(1000, OnTimer, $"{Name} MonitorHandler", true);
|
||||
|
||||
_ffuSpeed = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.Speed");
|
||||
|
||||
DATA.Subscribe($"{Name}.Speed", () => _ffuSpeed);
|
||||
|
||||
|
||||
OP.Subscribe($"{Name}SetFFUGroupAddress", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Set FFU Group Address.");
|
||||
return false;
|
||||
}
|
||||
int.TryParse((string)param[2], out var newgroupAddress);
|
||||
GroupAddress = (byte)newgroupAddress;
|
||||
SetGroupAddress((byte)newgroupAddress);
|
||||
EV.PostInfoLog(Module, $"{Name}Set FFU Group Address {newgroupAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}GetFFUGroupAddress", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Get FFU Group Address.");
|
||||
return false;
|
||||
}
|
||||
GetGroupAddress();
|
||||
EV.PostInfoLog(Module, $"{Name}Get FFU Group Address {groupAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}SetFFUDeviceAddress", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Set FFU Device Address.");
|
||||
return false;
|
||||
}
|
||||
int.TryParse((string)param[2], out var newAddress);
|
||||
SetDeviceAddress((byte)newAddress);
|
||||
EV.PostInfoLog(Module, $"{Name}Set FFU Device Address {newAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}GetFFUDeviceAddress", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Get FFU Device Address.");
|
||||
return false;
|
||||
}
|
||||
GetDeviceAddress();
|
||||
EV.PostInfoLog(Module, $"{Name}Get FFU Device Address {groupAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}SetFFUSpeedfactor", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Set FFU speed factor.");
|
||||
return false;
|
||||
}
|
||||
int.TryParse((string)param[2], out var newSpeedFactor);
|
||||
SetSpeedFactor((byte)newSpeedFactor);
|
||||
EV.PostInfoLog(Module, $"{Name}Set FFU speed factor {newSpeedFactor}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}GetFFUSpeedfactor", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Get FFU speed factor.");
|
||||
return false;
|
||||
}
|
||||
QuerySpeedFactor();
|
||||
EV.PostInfoLog(Module, $"{Name}Get FFU speed factor {groupAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}GetFFUStatus", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Get FFU Status.");
|
||||
return false;
|
||||
}
|
||||
QueryError();
|
||||
EV.PostInfoLog(Module, $"{Name}Get FFU Status {groupAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
|
||||
OP.Subscribe($"{Name}SetFFUSpeed", (cmd, param) =>
|
||||
{
|
||||
if (param == null)
|
||||
{
|
||||
string tempName = "FFU1";
|
||||
if (Name == "FfuAAF1")
|
||||
{
|
||||
tempName = "FFU1";
|
||||
}
|
||||
else if(Name == "FfuAAF2")
|
||||
{
|
||||
tempName = "FFU2";
|
||||
}
|
||||
int speed = SC.GetValue<int>($"System.{tempName}FanSpeedSetPoint");
|
||||
SetSpeed(speed);
|
||||
EV.PostInfoLog(Module, $"{Name} speed set to {speed}");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "invalid speed.");
|
||||
return false;
|
||||
}
|
||||
int.TryParse((string)param[2], out var speed);
|
||||
//SetSpeed((int)(speed * 250 * 1.0 / 1440));
|
||||
SetSpeed(speed);
|
||||
EV.PostInfoLog(Module, $"{Name} speed set to {speed}");
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}GetFFUSpeed", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "FFU Query speed.");
|
||||
return false;
|
||||
}
|
||||
QuerySpeed();
|
||||
EV.PostInfoLog(Module, $"{Name}FFU Query Speed {groupAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}GetFFUMaxSpeed1", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Get FFU GetFFUMaxSpeed1.");
|
||||
return false;
|
||||
}
|
||||
QueryMaxSpeed1();
|
||||
EV.PostInfoLog(Module, $"{Name}Get FFU GetFFUMaxSpeed1 {groupAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}GetFFUMaxSpeed2", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Get FFU GetFFUMaxSpeed2.");
|
||||
return false;
|
||||
}
|
||||
QueryMaxSpeed2();
|
||||
EV.PostInfoLog(Module, $"{Name}Get FFU GetFFUMaxSpeed2 {groupAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}GetFFUMaxSpeed3", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Get FFU GetFFUMaxSpeed3.");
|
||||
return false;
|
||||
}
|
||||
QueryMaxSpeed3();
|
||||
EV.PostInfoLog(Module, $"{Name}Get FFU GetFFUMaxSpeed3 {groupAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
|
||||
OP.Subscribe($"{Name}SetFFUReset", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Set FFU Reset.");
|
||||
return false;
|
||||
}
|
||||
ResetDevice();
|
||||
EV.PostInfoLog(Module, $"{Name}Set FFU Reset {groupAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}.SetFFUSpeedBySC", (cmd, param) =>
|
||||
{
|
||||
int speed = SC.GetValue<int>("System.FFUFanSpeedSetPoint");
|
||||
SetSpeed(speed);
|
||||
EV.PostInfoLog(Module, $"Set {Name} speed");
|
||||
return true;
|
||||
});
|
||||
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}");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
HandlerBase handler = null;
|
||||
if (!_connection.IsBusy)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
if (_lstHandler.Count == 0 && _activeMonitorStatus)
|
||||
{
|
||||
QuerySpeed();
|
||||
}
|
||||
|
||||
if (_lstHandler.Count > 0)
|
||||
{
|
||||
handler = _lstHandler.First.Value;
|
||||
|
||||
_lstHandler.RemoveFirst();
|
||||
}
|
||||
}
|
||||
|
||||
if (handler != null)
|
||||
{
|
||||
_connection.Execute(handler);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Write(ex);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
internal void NoteError()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Monitor()
|
||||
{
|
||||
try
|
||||
{
|
||||
_connection.EnableLog(_enableLog);
|
||||
|
||||
_trigPumpOnOff.CLK = _isOn;
|
||||
if (_trigPumpOnOff.R)
|
||||
{
|
||||
EV.PostInfoLog(Module, $"{Module}.{Name} is on");
|
||||
}
|
||||
|
||||
if (_trigPumpOnOff.T)
|
||||
{
|
||||
EV.PostInfoLog(Module, $"{Module}.{Name} is off");
|
||||
}
|
||||
|
||||
|
||||
_trigCommunicationError.CLK = _connection.IsCommunicationError;
|
||||
if (_trigCommunicationError.Q)
|
||||
{
|
||||
EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Write(ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void Terminate()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_connection != null)
|
||||
{
|
||||
_connection.Disconnect();
|
||||
_connection = null;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Write(ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_trigError.RST = true;
|
||||
_trigOverTemp.RST = true;
|
||||
_trigWarningMessage.RST = true;
|
||||
|
||||
_connection.SetCommunicationError(false, "");
|
||||
_trigCommunicationError.RST = true;
|
||||
|
||||
_enableLog = SC.GetValue<bool>($"System.{Module}.{Name}.EnableLogMessage");
|
||||
|
||||
_trigRetryConnect.RST = true;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void SetPumpOnOff(bool isOn)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void SetActiveMonitor(bool active)
|
||||
{
|
||||
_activeMonitorStatus = active;
|
||||
}
|
||||
|
||||
|
||||
internal void NoteOnOff(bool isOn)
|
||||
{
|
||||
_isOn = isOn;
|
||||
}
|
||||
|
||||
|
||||
public void SetSpeed(float speed)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
_lstHandler.Clear();
|
||||
if (_connection.IsBusy) _connection.ForceClear();
|
||||
|
||||
List<byte> cmddata = new List<byte>() { 0x35, 0x40, 0x0, (byte)(speed * 250.0 / _nMaxSpeed) };
|
||||
byte checksum = ModRTU_CRC(cmddata.ToArray());
|
||||
cmddata.Add(checksum);
|
||||
_connection.SendMessage(cmddata.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void SetErrorCode(int errorCode)
|
||||
{
|
||||
_errorCode = errorCode;
|
||||
}
|
||||
|
||||
|
||||
public void SetError(string reason)
|
||||
{
|
||||
_trigWarningMessage.CLK = true;
|
||||
if (_trigWarningMessage.Q)
|
||||
{
|
||||
EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
|
||||
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MECF.Framework.Common.Communications;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.AAF2
|
||||
{
|
||||
public class FfuAAFMessage : BinaryMessage
|
||||
{
|
||||
public byte Preamble { get; set; }
|
||||
public byte Command { get; set; }
|
||||
public byte GroupAddress { get; set; }
|
||||
public byte Data1 { get; set; }
|
||||
public byte Data2 { get; set; }
|
||||
}
|
||||
|
||||
public class FfuAAFConnection : SerialPortConnectionBase
|
||||
{
|
||||
private List<byte> _lstCacheBuffer = new List<byte>();
|
||||
public FfuAAFConnection(string portName) : base(portName,9600,8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One,"\r",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();
|
||||
|
||||
FfuAAFMessage msg = new FfuAAFMessage();
|
||||
msg.IsResponse = false;
|
||||
msg.IsAck = false;
|
||||
msg.IsComplete = false;
|
||||
msg.RawMessage = _lstCacheBuffer.ToArray();
|
||||
|
||||
if (temps.Length < 4) return msg;
|
||||
|
||||
if (temps.LastOrDefault() != ModRTU_CRC(temps.Take(temps.Length - 1).ToArray()))
|
||||
return msg;
|
||||
|
||||
msg.Preamble = temps[0];
|
||||
msg.Command = temps[1];
|
||||
msg.GroupAddress = temps[2];
|
||||
if (temps.Length > 4) msg.Data1 = temps[3];
|
||||
if (temps.Length > 5) msg.Data2 = temps[4];
|
||||
|
||||
|
||||
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;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+370
@@ -0,0 +1,370 @@
|
||||
using System.Collections.Generic;
|
||||
using MECF.Framework.Common.Communications;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.AAF2
|
||||
{
|
||||
public abstract class FfuAAFHandler : HandlerBase
|
||||
{
|
||||
public FfuAAF Device { get; }
|
||||
public byte DeviceAddress;
|
||||
public byte GroupAddress;
|
||||
|
||||
public bool IsNeedFeedback;
|
||||
|
||||
protected FfuAAFHandler(FfuAAF device, byte prefix, byte command, byte groupAddress, byte[] datas)
|
||||
: base(BuildMessage(prefix, command, groupAddress, datas))
|
||||
{
|
||||
Device = device;
|
||||
IsNeedFeedback = true;
|
||||
|
||||
}
|
||||
protected FfuAAFHandler(FfuAAF device, byte prefix, byte command, byte groupAddress, byte[] datas, bool needfeedback)
|
||||
: base(BuildMessage(prefix, command, groupAddress, datas))
|
||||
{
|
||||
Device = device;
|
||||
IsNeedFeedback = needfeedback;
|
||||
}
|
||||
|
||||
private static byte[] BuildMessage(byte prefix, byte command, byte groupAddress, byte[] datas)
|
||||
{
|
||||
List<byte> result = new List<byte>() { prefix, command, groupAddress };
|
||||
if (datas != null)
|
||||
{
|
||||
foreach (byte data in datas)
|
||||
result.Add(data);
|
||||
}
|
||||
result.Add(ModRTU_CRC(result.ToArray()));
|
||||
return result.ToArray();
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
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;
|
||||
byte temp = buffer[0];
|
||||
|
||||
for (int i = 1; i < buffer.Length; i++)
|
||||
{
|
||||
temp = (byte)(temp ^ buffer[i]);
|
||||
}
|
||||
return (byte)~temp;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置组地址
|
||||
/// </summary>
|
||||
public class FfuAAFSetGroupAddressHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFSetGroupAddressHandler(FfuAAF device, byte fanaddress, byte groupAddress, byte[] datas)
|
||||
: base(device, 0x55, (byte)(0xC0 + fanaddress), groupAddress, datas)
|
||||
{
|
||||
Name = "Set Group Address";
|
||||
DeviceAddress = fanaddress;
|
||||
GroupAddress = groupAddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 查询组地址
|
||||
/// </summary>
|
||||
public class FfuAAFGetGroupAddressHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFGetGroupAddressHandler(FfuAAF device, byte fanaddress, byte groupAddress, byte[] values)
|
||||
: base(device, 0x35, (byte)(0xE0 + fanaddress), groupAddress, values)
|
||||
{
|
||||
Name = "Query Group Address";
|
||||
DeviceAddress = fanaddress;
|
||||
GroupAddress = groupAddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
result.GroupAddress = result.Data1;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置机地址
|
||||
/// </summary>
|
||||
public class FfuAAFSetAddressHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFSetAddressHandler(FfuAAF device, byte fanaddress, byte groupAddress, byte[] datas)
|
||||
: base(device, 0x55, (byte)(0xC0 + fanaddress), groupAddress, datas)
|
||||
{
|
||||
Name = "Set Device Address";
|
||||
DeviceAddress = fanaddress;
|
||||
GroupAddress = groupAddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询机地址
|
||||
/// </summary>
|
||||
public class FfuAAFGetAddressHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFGetAddressHandler(FfuAAF device, byte fanaddress, byte groupAddress, byte[] values)
|
||||
: base(device, 0x35, (byte)(0xE0 + fanaddress), groupAddress, values)
|
||||
{
|
||||
Name = "Query Device Address";
|
||||
DeviceAddress = fanaddress;
|
||||
GroupAddress = groupAddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
Device.DeviceAddress = result.Data1;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 查询最高转速 1
|
||||
/// </summary>
|
||||
public class FfuAAFQueryMaxSpeed1Handler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFQueryMaxSpeed1Handler(FfuAAF device, byte fanaddress, byte groupAddress, byte[] values)
|
||||
: base(device, 0x35, (byte)(0xE0 + fanaddress), groupAddress, values)
|
||||
{
|
||||
Name = "Query Max Speed1";
|
||||
DeviceAddress = fanaddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
Device.MaxSpeed1 = result.Data1;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询最高转速 2
|
||||
/// </summary>
|
||||
public class FfuAAFQueryMaxSpeed2Handler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFQueryMaxSpeed2Handler(FfuAAF device, byte fanaddress, byte groupAddress, byte[] values)
|
||||
: base(device, 0x35, (byte)(0xE0 + fanaddress), groupAddress, values)
|
||||
{
|
||||
Name = "Query Max Speed2";
|
||||
DeviceAddress = fanaddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
Device.MaxSpeed2 = result.Data1;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询最高转速 3
|
||||
/// </summary>
|
||||
public class FfuAAFQueryMaxSpeed3Handler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFQueryMaxSpeed3Handler(FfuAAF device, byte fanaddress, byte groupAddress, byte[] values)
|
||||
: base(device, 0x35, (byte)(0xE0 + fanaddress), groupAddress, values)
|
||||
{
|
||||
Name = "Query Max Speed3";
|
||||
DeviceAddress = fanaddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
Device.MaxSpeed3 = result.Data1;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class FfuAAFQuerySpeedFactorHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFQuerySpeedFactorHandler(FfuAAF device, byte fanaddress, byte groupAddress, byte[] values)
|
||||
: base(device, 0x35, (byte)(0xE0 + fanaddress), groupAddress, values)
|
||||
{
|
||||
Name = "Query Speed Factor";
|
||||
DeviceAddress = fanaddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
Device.Speedfactor = result.Data1;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class FfuAAFSetSpeedFactorHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFSetSpeedFactorHandler(FfuAAF device, byte fanaddress, byte groupAddress, byte[] datas)
|
||||
: base(device, 0x55, (byte)(0xC0 + fanaddress), groupAddress, datas)
|
||||
{
|
||||
Name = "Set Speed";
|
||||
DeviceAddress = fanaddress;
|
||||
GroupAddress = groupAddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class FfuAAFQuerySpeedHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFQuerySpeedHandler(FfuAAF device, byte fanaddress, byte groupAddress)
|
||||
: base(device, 0x15, (byte)(0x20 + fanaddress), groupAddress, null)
|
||||
{
|
||||
Name = "Query Speed";
|
||||
DeviceAddress = fanaddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
Device.Speed = result.Data1 * Device.MaxSpeed2 / 250;//Device.NMaxSpeed
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class FfuAAFSetSpeedHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFSetSpeedHandler(FfuAAF device, byte fanaddress, byte groupAddress, byte[] datas)
|
||||
: base(device, 0x35, (byte)(0x40 + fanaddress), groupAddress, datas)
|
||||
{
|
||||
Name = "Set Speed";
|
||||
DeviceAddress = fanaddress;
|
||||
GroupAddress = groupAddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class FfuAAFResetDeviceHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFResetDeviceHandler(FfuAAF device, byte fanaddress, byte groupAddress)
|
||||
: base(device, 0x15, (byte)(0x60 + fanaddress), groupAddress, null)
|
||||
{
|
||||
Name = "Reset Device";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class FfuAAFQueryStatusHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFQueryStatusHandler(FfuAAF device, byte fanaddress, byte groupAddress)
|
||||
: base(device, 0x35, fanaddress, groupAddress, new byte[] { 0x00 })
|
||||
{
|
||||
Name = "Query Status";
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
//if (result.IsError)
|
||||
//{
|
||||
// Device.SetError(result.Data);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// if (int.TryParse(result.Data, out int code))
|
||||
// {
|
||||
// Device.SetErrorCode(code);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Device.SetError(result.Data + "format error");
|
||||
// }
|
||||
//}
|
||||
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,631 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
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 MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.Aire
|
||||
{
|
||||
public class FfuAAF : BaseDevice, IConnection, IDevice
|
||||
{
|
||||
private FfuAAFConnection _connection;
|
||||
|
||||
private string _deviceAddress = "001";
|
||||
|
||||
public byte DeviceAddress { get; set; }
|
||||
|
||||
|
||||
public byte GroupAddress { get; set; }
|
||||
|
||||
public byte Speedfactor { get; set; }
|
||||
|
||||
public int MaxSpeed1 { get; set; }
|
||||
|
||||
public int MaxSpeed2 { get; set; }
|
||||
|
||||
public int MaxSpeed3 { get; set; }
|
||||
|
||||
public int Status { get; set; }
|
||||
|
||||
|
||||
private bool _isOn;
|
||||
|
||||
private bool _activeMonitorStatus;
|
||||
|
||||
private int _nMaxSpeed = 0;
|
||||
public int NMaxSpeed { get => _nMaxSpeed; }
|
||||
|
||||
private int _errorCode;
|
||||
|
||||
private RD_TRIG _trigPumpOnOff = new RD_TRIG();
|
||||
private R_TRIG _trigError = new R_TRIG();
|
||||
private R_TRIG _trigOverTemp = new R_TRIG();
|
||||
|
||||
private R_TRIG _trigWarningMessage = new R_TRIG();
|
||||
private string _lastError = string.Empty;
|
||||
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 object _locker = new object();
|
||||
|
||||
private bool _enableLog = true;
|
||||
|
||||
private string _scRoot;
|
||||
|
||||
private AsyncSerial serialPort;
|
||||
|
||||
private int _ffuSpeed;
|
||||
|
||||
private string[] StatusStringList = new string[] { "normal", "running fault", "stop fault" };
|
||||
|
||||
public int Speed
|
||||
{
|
||||
get { return _ffuSpeed; }
|
||||
set { _ffuSpeed = 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 FfuAAF(string scRoot, string module, string name, string port) : base(module, name, name, name)
|
||||
{
|
||||
|
||||
_scRoot = scRoot;
|
||||
_activeMonitorStatus = true;
|
||||
//serialPort = new AsyncSerial(port, 9600, 8);
|
||||
//serialPort.OnDataChanged += (OnDataChanged);
|
||||
//serialPort.OnErrorHappened += (OnErrorHandler);
|
||||
}
|
||||
|
||||
private void OnErrorHandler(ErrorEventArgs args)
|
||||
{
|
||||
}
|
||||
|
||||
private void OnDataChanged(string message)
|
||||
{
|
||||
}
|
||||
|
||||
public void SetGroupAddress(byte newGroupAddress)
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFSetGroupAddressHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x00, newGroupAddress }));
|
||||
}
|
||||
|
||||
public void GetGroupAddress()
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFGetGroupAddressHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x00 }));
|
||||
}
|
||||
|
||||
public void SetDeviceAddress(byte newDeviceAddress)
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFSetAddressHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x01, newDeviceAddress }));
|
||||
}
|
||||
|
||||
public void GetDeviceAddress()
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFGetAddressHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x01 }));
|
||||
}
|
||||
|
||||
public void QueryMaxSpeed1()
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFQueryMaxSpeed1Handler(this, DeviceAddress, GroupAddress, new byte[] { 0x08 }));
|
||||
}
|
||||
|
||||
public void QueryMaxSpeed2()
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFQueryMaxSpeed2Handler(this, DeviceAddress, GroupAddress, new byte[] { 0x09 }));
|
||||
}
|
||||
|
||||
public void QueryMaxSpeed3()
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFQueryMaxSpeed3Handler(this, DeviceAddress, GroupAddress, new byte[] { 0x0a }));
|
||||
}
|
||||
/// <summary>
|
||||
/// 查转速因子
|
||||
/// </summary>
|
||||
public void QuerySpeedFactor()
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFQuerySpeedFactorHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x11 }));
|
||||
}
|
||||
|
||||
public void SetSpeedFactor(byte newSpeedFactor)
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFSetSpeedFactorHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x11, newSpeedFactor }));
|
||||
}
|
||||
|
||||
public void QuerySpeed()
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFQuerySpeedHandler(this, DeviceAddress, GroupAddress));
|
||||
}
|
||||
|
||||
public void SetSpeed(int speed)
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFSetSpeedHandler(this, DeviceAddress, GroupAddress, new byte[] { (byte)(speed * 250.0 / MaxSpeed2) }));
|
||||
}
|
||||
|
||||
public void ResetDevice()
|
||||
{
|
||||
_lstHandler.AddLast(new FfuAAFResetDeviceHandler(this, DeviceAddress, GroupAddress));
|
||||
}
|
||||
|
||||
public void QueryError()
|
||||
{
|
||||
|
||||
_lstHandler.AddLast(new FfuAAFQueryStatusHandler(this, DeviceAddress, GroupAddress));
|
||||
|
||||
EV.PostInfoLog(Module, "Query error");
|
||||
}
|
||||
|
||||
private int ChangedStatus(int value)
|
||||
{
|
||||
int returnValue = 0;
|
||||
switch (value)
|
||||
{
|
||||
case 0:
|
||||
returnValue = 0;
|
||||
break;
|
||||
case 0x80:
|
||||
returnValue = 1;
|
||||
break;
|
||||
case 0x10:
|
||||
returnValue = 2;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
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}.{Module}.{Name}.Address");
|
||||
Address = portName;
|
||||
|
||||
int address = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.DeviceAddress");
|
||||
|
||||
DeviceAddress = (byte)address;
|
||||
DATA.Subscribe($"{Name}.DeviceAddress", () => DeviceAddress);
|
||||
|
||||
GroupAddress = (byte)SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.GroupAddress");
|
||||
DATA.Subscribe($"{Name}.GroupAddress", () => GroupAddress);
|
||||
|
||||
Status = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.Status");
|
||||
DATA.Subscribe($"{Name}.Status", () => StatusStringList[ChangedStatus(Status)]);
|
||||
|
||||
_deviceAddress = address.ToString("D3");
|
||||
_enableLog = SC.GetValue<bool>($"{_scRoot}.{Module}.{Name}.EnableLogMessage");
|
||||
|
||||
Speedfactor = (byte)SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.Speedfactor");
|
||||
DATA.Subscribe($"{Name}.Speedfactor", () => Speedfactor);
|
||||
|
||||
// _nMaxSpeed = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.MaxSpeed1");
|
||||
MaxSpeed1 = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.MaxSpeed1");
|
||||
DATA.Subscribe($"{Name}.MaxSpeed1", () => MaxSpeed1);
|
||||
|
||||
MaxSpeed2 = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.MaxSpeed2");
|
||||
DATA.Subscribe($"{Name}.MaxSpeed2", () => MaxSpeed2);
|
||||
|
||||
MaxSpeed3 = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.MaxSpeed3");
|
||||
DATA.Subscribe($"{Name}.MaxSpeed3", () => MaxSpeed3);
|
||||
|
||||
_connection = new FfuAAFConnection(portName);
|
||||
_connection.EnableLog(_enableLog);
|
||||
|
||||
if (_connection.Connect())
|
||||
{
|
||||
EV.PostInfoLog(Module, $"{Module}.{Name} connected");
|
||||
}
|
||||
|
||||
_thread = new PeriodicJob(1000, OnTimer, $"{Name} MonitorHandler", true);
|
||||
|
||||
_ffuSpeed = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.Speed");
|
||||
|
||||
DATA.Subscribe($"{Name}.Speed", () => _ffuSpeed);
|
||||
|
||||
|
||||
OP.Subscribe($"{Name}SetFFUGroupAddress", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Set FFU Group Address.");
|
||||
return false;
|
||||
}
|
||||
int.TryParse((string)param[2], out var newgroupAddress);
|
||||
GroupAddress = (byte)newgroupAddress;
|
||||
SetGroupAddress((byte)newgroupAddress);
|
||||
EV.PostInfoLog(Module, $"{Name}Set FFU Group Address {newgroupAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}GetFFUGroupAddress", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Get FFU Group Address.");
|
||||
return false;
|
||||
}
|
||||
GetGroupAddress();
|
||||
EV.PostInfoLog(Module, $"{Name}Get FFU Group Address {groupAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}SetFFUDeviceAddress", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Set FFU Device Address.");
|
||||
return false;
|
||||
}
|
||||
int.TryParse((string)param[2], out var newAddress);
|
||||
SetDeviceAddress((byte)newAddress);
|
||||
EV.PostInfoLog(Module, $"{Name}Set FFU Device Address {newAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}GetFFUDeviceAddress", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Get FFU Device Address.");
|
||||
return false;
|
||||
}
|
||||
GetDeviceAddress();
|
||||
EV.PostInfoLog(Module, $"{Name}Get FFU Device Address {groupAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}SetFFUSpeedfactor", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Set FFU speed factor.");
|
||||
return false;
|
||||
}
|
||||
int.TryParse((string)param[2], out var newSpeedFactor);
|
||||
SetSpeedFactor((byte)newSpeedFactor);
|
||||
EV.PostInfoLog(Module, $"{Name}Set FFU speed factor {newSpeedFactor}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}GetFFUSpeedfactor", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Get FFU speed factor.");
|
||||
return false;
|
||||
}
|
||||
QuerySpeedFactor();
|
||||
EV.PostInfoLog(Module, $"{Name}Get FFU speed factor {groupAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}GetFFUStatus", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Get FFU Status.");
|
||||
return false;
|
||||
}
|
||||
QueryError();
|
||||
EV.PostInfoLog(Module, $"{Name}Get FFU Status {groupAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
|
||||
OP.Subscribe($"{Name}SetFFUSpeed", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "invalid speed.");
|
||||
return false;
|
||||
}
|
||||
int.TryParse((string)param[2], out var speed);
|
||||
//SetSpeed((int)(speed * 250 * 1.0 / 1440));
|
||||
SetSpeed(speed);
|
||||
EV.PostInfoLog(Module, $"{Name} speed set to {speed}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}GetFFUSpeed", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "FFU Query speed.");
|
||||
return false;
|
||||
}
|
||||
QuerySpeed();
|
||||
EV.PostInfoLog(Module, $"{Name}FFU Query Speed {groupAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}GetFFUMaxSpeed1", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Get FFU GetFFUMaxSpeed1.");
|
||||
return false;
|
||||
}
|
||||
QueryMaxSpeed1();
|
||||
EV.PostInfoLog(Module, $"{Name}Get FFU GetFFUMaxSpeed1 {groupAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}GetFFUMaxSpeed2", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Get FFU GetFFUMaxSpeed2.");
|
||||
return false;
|
||||
}
|
||||
QueryMaxSpeed2();
|
||||
EV.PostInfoLog(Module, $"{Name}Get FFU GetFFUMaxSpeed2 {groupAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}GetFFUMaxSpeed3", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Get FFU GetFFUMaxSpeed3.");
|
||||
return false;
|
||||
}
|
||||
QueryMaxSpeed3();
|
||||
EV.PostInfoLog(Module, $"{Name}Get FFU GetFFUMaxSpeed3 {groupAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
|
||||
OP.Subscribe($"{Name}SetFFUReset", (cmd, param) =>
|
||||
{
|
||||
if (int.TryParse((string)param[0], out var groupAddress))
|
||||
{
|
||||
EV.PostWarningLog(Module, "Set FFU Reset.");
|
||||
return false;
|
||||
}
|
||||
ResetDevice();
|
||||
EV.PostInfoLog(Module, $"{Name}Set FFU Reset {groupAddress}");
|
||||
return true;
|
||||
});
|
||||
|
||||
OP.Subscribe($"{Name}.SetFFUSpeedBySC", (cmd, param) =>
|
||||
{
|
||||
int speed = SC.GetValue<int>("System.FFUFanSpeedSetPoint");
|
||||
SetSpeed(speed);
|
||||
EV.PostInfoLog(Module, $"Set {Name} speed");
|
||||
return true;
|
||||
});
|
||||
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}");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
HandlerBase handler = null;
|
||||
if (!_connection.IsBusy)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
if (_lstHandler.Count == 0 && _activeMonitorStatus)
|
||||
{
|
||||
QuerySpeed();
|
||||
}
|
||||
|
||||
if (_lstHandler.Count > 0)
|
||||
{
|
||||
handler = _lstHandler.First.Value;
|
||||
|
||||
_lstHandler.RemoveFirst();
|
||||
}
|
||||
}
|
||||
|
||||
if (handler != null)
|
||||
{
|
||||
_connection.Execute(handler);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Write(ex);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
internal void NoteError()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Monitor()
|
||||
{
|
||||
try
|
||||
{
|
||||
_connection.EnableLog(_enableLog);
|
||||
|
||||
_trigPumpOnOff.CLK = _isOn;
|
||||
if (_trigPumpOnOff.R)
|
||||
{
|
||||
EV.PostInfoLog(Module, $"{Module}.{Name} is on");
|
||||
}
|
||||
|
||||
if (_trigPumpOnOff.T)
|
||||
{
|
||||
EV.PostInfoLog(Module, $"{Module}.{Name} is off");
|
||||
}
|
||||
|
||||
|
||||
_trigCommunicationError.CLK = _connection.IsCommunicationError;
|
||||
if (_trigCommunicationError.Q)
|
||||
{
|
||||
EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Write(ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void Terminate()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_connection != null)
|
||||
{
|
||||
_connection.Disconnect();
|
||||
_connection = null;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Write(ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_trigError.RST = true;
|
||||
_trigOverTemp.RST = true;
|
||||
_trigWarningMessage.RST = true;
|
||||
|
||||
_connection.SetCommunicationError(false, "");
|
||||
_trigCommunicationError.RST = true;
|
||||
|
||||
_enableLog = SC.GetValue<bool>($"System.{Module}.{Name}.EnableLogMessage");
|
||||
|
||||
_trigRetryConnect.RST = true;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void SetPumpOnOff(bool isOn)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void SetActiveMonitor(bool active)
|
||||
{
|
||||
_activeMonitorStatus = active;
|
||||
}
|
||||
|
||||
|
||||
internal void NoteOnOff(bool isOn)
|
||||
{
|
||||
_isOn = isOn;
|
||||
}
|
||||
|
||||
|
||||
public void SetSpeed(float speed)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
_lstHandler.Clear();
|
||||
if (_connection.IsBusy) _connection.ForceClear();
|
||||
|
||||
List<byte> cmddata = new List<byte>() { 0x35, 0x40, 0x0, (byte)(speed * 250.0 / _nMaxSpeed) };
|
||||
byte checksum = ModRTU_CRC(cmddata.ToArray());
|
||||
cmddata.Add(checksum);
|
||||
_connection.SendMessage(cmddata.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void SetErrorCode(int errorCode)
|
||||
{
|
||||
_errorCode = errorCode;
|
||||
}
|
||||
|
||||
|
||||
public void SetError(string reason)
|
||||
{
|
||||
_trigWarningMessage.CLK = true;
|
||||
if (_trigWarningMessage.Q)
|
||||
{
|
||||
EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
|
||||
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MECF.Framework.Common.Communications;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.Aire
|
||||
{
|
||||
public class FfuAAFMessage : BinaryMessage
|
||||
{
|
||||
public byte Preamble { get; set; }
|
||||
public byte Command { get; set; }
|
||||
public byte GroupAddress { get; set; }
|
||||
public byte Data1 { get; set; }
|
||||
public byte Data2 { get; set; }
|
||||
}
|
||||
|
||||
public class FfuAAFConnection : SerialPortConnectionBase
|
||||
{
|
||||
private List<byte> _lstCacheBuffer = new List<byte>();
|
||||
public FfuAAFConnection(string portName) : base(portName,9600,8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One,"\r",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();
|
||||
|
||||
FfuAAFMessage msg = new FfuAAFMessage();
|
||||
msg.IsResponse = false;
|
||||
msg.IsAck = false;
|
||||
msg.IsComplete = false;
|
||||
msg.RawMessage = _lstCacheBuffer.ToArray();
|
||||
|
||||
if (temps.Length < 4) return msg;
|
||||
|
||||
if (temps.LastOrDefault() != ModRTU_CRC(temps.Take(temps.Length - 1).ToArray()))
|
||||
return msg;
|
||||
|
||||
msg.Preamble = temps[0];
|
||||
msg.Command = temps[1];
|
||||
msg.GroupAddress = temps[2];
|
||||
if (temps.Length > 4) msg.Data1 = temps[3];
|
||||
if (temps.Length > 5) msg.Data2 = temps[4];
|
||||
|
||||
|
||||
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;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+370
@@ -0,0 +1,370 @@
|
||||
using System.Collections.Generic;
|
||||
using MECF.Framework.Common.Communications;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.Aire
|
||||
{
|
||||
public abstract class FfuAAFHandler : HandlerBase
|
||||
{
|
||||
public FfuAAF Device { get; }
|
||||
public byte DeviceAddress;
|
||||
public byte GroupAddress;
|
||||
|
||||
public bool IsNeedFeedback;
|
||||
|
||||
protected FfuAAFHandler(FfuAAF device, byte prefix, byte command, byte groupAddress, byte[] datas)
|
||||
: base(BuildMessage(prefix, command, groupAddress, datas))
|
||||
{
|
||||
Device = device;
|
||||
IsNeedFeedback = true;
|
||||
|
||||
}
|
||||
protected FfuAAFHandler(FfuAAF device, byte prefix, byte command, byte groupAddress, byte[] datas, bool needfeedback)
|
||||
: base(BuildMessage(prefix, command, groupAddress, datas))
|
||||
{
|
||||
Device = device;
|
||||
IsNeedFeedback = needfeedback;
|
||||
}
|
||||
|
||||
private static byte[] BuildMessage(byte prefix, byte command, byte groupAddress, byte[] datas)
|
||||
{
|
||||
List<byte> result = new List<byte>() { prefix, command, groupAddress };
|
||||
if (datas != null)
|
||||
{
|
||||
foreach (byte data in datas)
|
||||
result.Add(data);
|
||||
}
|
||||
result.Add(ModRTU_CRC(result.ToArray()));
|
||||
return result.ToArray();
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
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;
|
||||
byte temp = buffer[0];
|
||||
|
||||
for (int i = 1; i < buffer.Length; i++)
|
||||
{
|
||||
temp = (byte)(temp ^ buffer[i]);
|
||||
}
|
||||
return (byte)~temp;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置组地址
|
||||
/// </summary>
|
||||
public class FfuAAFSetGroupAddressHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFSetGroupAddressHandler(FfuAAF device, byte fanaddress, byte groupAddress, byte[] datas)
|
||||
: base(device, 0x55, (byte)(0xC0 + fanaddress), groupAddress, datas)
|
||||
{
|
||||
Name = "Set Group Address";
|
||||
DeviceAddress = fanaddress;
|
||||
GroupAddress = groupAddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 查询组地址
|
||||
/// </summary>
|
||||
public class FfuAAFGetGroupAddressHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFGetGroupAddressHandler(FfuAAF device, byte fanaddress, byte groupAddress, byte[] values)
|
||||
: base(device, 0x35, (byte)(0xE0 + fanaddress), groupAddress, values)
|
||||
{
|
||||
Name = "Query Group Address";
|
||||
DeviceAddress = fanaddress;
|
||||
GroupAddress = groupAddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
result.GroupAddress = result.Data1;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置机地址
|
||||
/// </summary>
|
||||
public class FfuAAFSetAddressHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFSetAddressHandler(FfuAAF device, byte fanaddress, byte groupAddress, byte[] datas)
|
||||
: base(device, 0x55, (byte)(0xC0 + fanaddress), groupAddress, datas)
|
||||
{
|
||||
Name = "Set Device Address";
|
||||
DeviceAddress = fanaddress;
|
||||
GroupAddress = groupAddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询机地址
|
||||
/// </summary>
|
||||
public class FfuAAFGetAddressHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFGetAddressHandler(FfuAAF device, byte fanaddress, byte groupAddress, byte[] values)
|
||||
: base(device, 0x35, (byte)(0xE0 + fanaddress), groupAddress, values)
|
||||
{
|
||||
Name = "Query Device Address";
|
||||
DeviceAddress = fanaddress;
|
||||
GroupAddress = groupAddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
Device.DeviceAddress = result.Data1;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 查询最高转速 1
|
||||
/// </summary>
|
||||
public class FfuAAFQueryMaxSpeed1Handler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFQueryMaxSpeed1Handler(FfuAAF device, byte fanaddress, byte groupAddress, byte[] values)
|
||||
: base(device, 0x35, (byte)(0xE0 + fanaddress), groupAddress, values)
|
||||
{
|
||||
Name = "Query Max Speed1";
|
||||
DeviceAddress = fanaddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
Device.MaxSpeed1 = result.Data1;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询最高转速 2
|
||||
/// </summary>
|
||||
public class FfuAAFQueryMaxSpeed2Handler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFQueryMaxSpeed2Handler(FfuAAF device, byte fanaddress, byte groupAddress, byte[] values)
|
||||
: base(device, 0x35, (byte)(0xE0 + fanaddress), groupAddress, values)
|
||||
{
|
||||
Name = "Query Max Speed2";
|
||||
DeviceAddress = fanaddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
Device.MaxSpeed2 = result.Data1;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询最高转速 3
|
||||
/// </summary>
|
||||
public class FfuAAFQueryMaxSpeed3Handler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFQueryMaxSpeed3Handler(FfuAAF device, byte fanaddress, byte groupAddress, byte[] values)
|
||||
: base(device, 0x35, (byte)(0xE0 + fanaddress), groupAddress, values)
|
||||
{
|
||||
Name = "Query Max Speed3";
|
||||
DeviceAddress = fanaddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
Device.MaxSpeed3 = result.Data1;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class FfuAAFQuerySpeedFactorHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFQuerySpeedFactorHandler(FfuAAF device, byte fanaddress, byte groupAddress, byte[] values)
|
||||
: base(device, 0x35, (byte)(0xE0 + fanaddress), groupAddress, values)
|
||||
{
|
||||
Name = "Query Speed Factor";
|
||||
DeviceAddress = fanaddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
Device.Speedfactor = result.Data1;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class FfuAAFSetSpeedFactorHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFSetSpeedFactorHandler(FfuAAF device, byte fanaddress, byte groupAddress, byte[] datas)
|
||||
: base(device, 0x55, (byte)(0xC0 + fanaddress), groupAddress, datas)
|
||||
{
|
||||
Name = "Set Speed";
|
||||
DeviceAddress = fanaddress;
|
||||
GroupAddress = groupAddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class FfuAAFQuerySpeedHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFQuerySpeedHandler(FfuAAF device, byte fanaddress, byte groupAddress)
|
||||
: base(device, 0x15, (byte)(0x20 + fanaddress), groupAddress, null)
|
||||
{
|
||||
Name = "Query Speed";
|
||||
DeviceAddress = fanaddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
Device.Speed = result.Data1 * 1440 / 250;//Device.NMaxSpeed
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class FfuAAFSetSpeedHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFSetSpeedHandler(FfuAAF device, byte fanaddress, byte groupAddress, byte[] datas)
|
||||
: base(device, 0x35, (byte)(0x40 + fanaddress), groupAddress, datas)
|
||||
{
|
||||
Name = "Set Speed";
|
||||
DeviceAddress = fanaddress;
|
||||
GroupAddress = groupAddress;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class FfuAAFResetDeviceHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFResetDeviceHandler(FfuAAF device, byte fanaddress, byte groupAddress)
|
||||
: base(device, 0x15, (byte)(0x60 + fanaddress), groupAddress, null)
|
||||
{
|
||||
Name = "Reset Device";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class FfuAAFQueryStatusHandler : FfuAAFHandler
|
||||
{
|
||||
public FfuAAFQueryStatusHandler(FfuAAF device, byte fanaddress, byte groupAddress)
|
||||
: base(device, 0x35, fanaddress, groupAddress, new byte[] { 0x00 })
|
||||
{
|
||||
Name = "Query Status";
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuAAFMessage;
|
||||
//if (result.IsError)
|
||||
//{
|
||||
// Device.SetError(result.Data);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// if (int.TryParse(result.Data, out int code))
|
||||
// {
|
||||
// Device.SetErrorCode(code);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Device.SetError(result.Data + "format error");
|
||||
// }
|
||||
//}
|
||||
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+173
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
+301
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
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.MayAir
|
||||
{
|
||||
public class FfuMayAir : BaseDevice, IConnection
|
||||
{
|
||||
private FfuMayAirConnection _connection;
|
||||
|
||||
private string _deviceAddress = "001";
|
||||
|
||||
private bool _isOn;
|
||||
|
||||
private bool _activeMonitorStatus;
|
||||
|
||||
private int _errorCode;
|
||||
|
||||
private RD_TRIG _trigPumpOnOff = new RD_TRIG();
|
||||
private R_TRIG _trigError = new R_TRIG();
|
||||
private R_TRIG _trigOverTemp = new R_TRIG();
|
||||
|
||||
private R_TRIG _trigWarningMessage = new R_TRIG();
|
||||
private string _lastError = string.Empty;
|
||||
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 object _locker = new object();
|
||||
|
||||
private bool _enableLog = true;
|
||||
|
||||
private string _scRoot;
|
||||
|
||||
public string Address
|
||||
{
|
||||
get
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsConnected
|
||||
{
|
||||
get
|
||||
{
|
||||
return _connection!=null && _connection.IsConnected;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public bool Connect()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Disconnect()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public FfuMayAir(string module, string name, string scRoot) : base(module, name, name, name)
|
||||
{
|
||||
_scRoot = scRoot;
|
||||
|
||||
_activeMonitorStatus = true;
|
||||
}
|
||||
|
||||
public void QuerySpeed()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void ResetDevice()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void QueryError()
|
||||
{
|
||||
_lstHandler.AddLast(new FfuMayAirQueryStatusHandler(this, 1, 0));
|
||||
|
||||
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}.{Module}.{Name}.Address");
|
||||
|
||||
int address = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.DeviceAddress");
|
||||
|
||||
_deviceAddress = address.ToString("D3");
|
||||
_enableLog = SC.GetValue<bool>($"{_scRoot}.{Module}.{Name}.EnableLogMessage");
|
||||
|
||||
_connection = new FfuMayAirConnection(portName);
|
||||
_connection.EnableLog(_enableLog);
|
||||
|
||||
if (_connection.Connect())
|
||||
{
|
||||
EV.PostInfoLog(Module, $"{Module}.{Name} connected");
|
||||
}
|
||||
|
||||
_thread = new PeriodicJob(100, OnTimer, $"{Module}.{Name} MonitorHandler", 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($"{Module}.{Name}.Address"));
|
||||
if (!_connection.Connect())
|
||||
{
|
||||
EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
HandlerBase handler = null;
|
||||
if (!_connection.IsBusy)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
if (_lstHandler.Count == 0 && _activeMonitorStatus)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
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 NoteError()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Monitor()
|
||||
{
|
||||
try
|
||||
{
|
||||
_connection.EnableLog(_enableLog);
|
||||
|
||||
_trigPumpOnOff.CLK = _isOn;
|
||||
if (_trigPumpOnOff.R)
|
||||
{
|
||||
EV.PostInfoLog(Module, $"{Module}.{Name} is on");
|
||||
}
|
||||
|
||||
if (_trigPumpOnOff.T)
|
||||
{
|
||||
EV.PostInfoLog(Module, $"{Module}.{Name} is off");
|
||||
}
|
||||
|
||||
|
||||
_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()
|
||||
{
|
||||
_trigError.RST = true;
|
||||
_trigOverTemp.RST = true;
|
||||
_trigWarningMessage.RST = true;
|
||||
|
||||
_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;
|
||||
}
|
||||
|
||||
|
||||
internal void NoteOnOff(bool isOn)
|
||||
{
|
||||
_isOn = isOn;
|
||||
}
|
||||
|
||||
|
||||
public void SetSpeed(float speed)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void SetErrorCode(int errorCode)
|
||||
{
|
||||
_errorCode = errorCode;
|
||||
}
|
||||
|
||||
|
||||
public void SetError(string reason)
|
||||
{
|
||||
_trigWarningMessage.CLK = true;
|
||||
if (_trigWarningMessage.Q)
|
||||
{
|
||||
EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
using MECF.Framework.Common.Communications;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.MayAir
|
||||
{
|
||||
public class FfuMayAirMessage : BinaryMessage
|
||||
{
|
||||
public byte Prefix { get; set; }
|
||||
public byte Action { get; set; }
|
||||
public byte Command { get; set; }
|
||||
public byte DeviceAddress { get; set; }
|
||||
public byte GroupAddress { get; set; }
|
||||
public byte Data1 { get; set; }
|
||||
public byte Data2 { get; set; }
|
||||
}
|
||||
|
||||
public class FfuMayAirConnection : SerialPortConnectionBase
|
||||
{
|
||||
public FfuMayAirConnection(string portName) : base(portName)
|
||||
{
|
||||
|
||||
}
|
||||
protected override MessageBase ParseResponse(byte[] rawMessage)
|
||||
{
|
||||
FfuMayAirMessage msg = new FfuMayAirMessage();
|
||||
//msg.RawMessage = rawText;
|
||||
|
||||
//if (rawText.Length <= 14)
|
||||
//{
|
||||
// LOG.Error($"text length check failed, " + rawText);
|
||||
// msg.IsFormatError = true;
|
||||
// return msg;
|
||||
//}
|
||||
|
||||
//msg.MessagePart = new string[6];
|
||||
|
||||
//msg.MessagePart[0] = rawText.Substring(0, 3); //device address
|
||||
//msg.MessagePart[1] = rawText.Substring(3, 2); // action
|
||||
//msg.MessagePart[2] = rawText.Substring(5, 3); //parameter
|
||||
//msg.MessagePart[3] = rawText.Substring(8, 2); //length
|
||||
|
||||
//if (!int.TryParse(msg.MessagePart[3], out int length))
|
||||
//{
|
||||
// LOG.Error($"text {msg.MessagePart[3]} not valid, " + rawText);
|
||||
// msg.IsFormatError = true;
|
||||
// return msg;
|
||||
//}
|
||||
|
||||
//msg.MessagePart[4] = rawText.Substring(10, length); //data
|
||||
|
||||
//msg.MessagePart[5] = rawText.Substring(10 + length, 3);
|
||||
|
||||
//if (!int.TryParse(msg.MessagePart[5], out int sum))
|
||||
//{
|
||||
// LOG.Error($"text {msg.MessagePart[5]} not valid, " + rawText);
|
||||
// msg.IsFormatError = true;
|
||||
// return msg;
|
||||
//}
|
||||
|
||||
//int checkSum = 0;
|
||||
//string checkText = rawText.Substring(0, rawText.Length - 3 - 1);
|
||||
|
||||
// foreach (var item in checkText)
|
||||
// {
|
||||
// checkSum += (int)item;
|
||||
// }
|
||||
|
||||
//if (checkSum % 256 != sum)
|
||||
//{
|
||||
// LOG.Error($"check sum failed, " + rawText);
|
||||
// msg.IsFormatError = true;
|
||||
// return msg;
|
||||
//}
|
||||
|
||||
//msg.DeviceAddress = msg.MessagePart[0];
|
||||
//msg.Action = msg.MessagePart[1];
|
||||
//msg.Parameter = msg.MessagePart[2];
|
||||
//msg.DataLength = length;
|
||||
//msg.Data = msg.MessagePart[4];
|
||||
|
||||
//if (msg.Data == "NO_DEF" || msg.Data=="_RANGE" || msg.Data=="_LOGIC")
|
||||
//{
|
||||
// msg.IsError = true;
|
||||
//}
|
||||
|
||||
msg.IsResponse = true;
|
||||
msg.IsAck = true;
|
||||
msg.IsComplete = true;
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
using System.Collections.Generic;
|
||||
using MECF.Framework.Common.Communications;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.MayAir
|
||||
{
|
||||
public abstract class FfuMayAirHandler : HandlerBase
|
||||
{
|
||||
public FfuMayAir Device { get; }
|
||||
|
||||
protected FfuMayAirHandler(FfuMayAir device, byte prefix, byte command, byte deviceAddress, byte groupAddress, byte data1, byte data2)
|
||||
: base(BuildMessage(prefix, command, deviceAddress, groupAddress, data1, data2))
|
||||
{
|
||||
Device = device;
|
||||
}
|
||||
|
||||
protected FfuMayAirHandler(FfuMayAir device, byte prefix, byte command, byte deviceAddress, byte groupAddress, byte data1)
|
||||
: base(BuildMessage(prefix, command, deviceAddress, groupAddress, data1))
|
||||
{
|
||||
Device = device;
|
||||
}
|
||||
|
||||
protected FfuMayAirHandler(FfuMayAir device, byte prefix, byte command, byte deviceAddress, byte groupAddress)
|
||||
: base(BuildMessage(prefix, command, deviceAddress, groupAddress))
|
||||
{
|
||||
Device = device;
|
||||
}
|
||||
|
||||
private static byte[] BuildMessage(byte prefix, byte command, byte deviceAddress, byte groupAddress)
|
||||
{
|
||||
List<byte> result = new List<byte>() { prefix, (byte)(command | deviceAddress), groupAddress };
|
||||
result.Add(CalcSum(result));
|
||||
return result.ToArray();
|
||||
}
|
||||
|
||||
private static byte[] BuildMessage(byte prefix, byte command, byte deviceAddress, byte groupAddress, byte data1)
|
||||
{
|
||||
List<byte> result = new List<byte>(){prefix, (byte)(command | deviceAddress), groupAddress, data1};
|
||||
result.Add(CalcSum(result));
|
||||
return result.ToArray();
|
||||
}
|
||||
|
||||
private static byte[] BuildMessage(byte prefix, byte command, byte deviceAddress, byte groupAddress, byte data1, byte data2)
|
||||
{
|
||||
List<byte> result = new List<byte>() { prefix, (byte)(command | deviceAddress), groupAddress, data1, data2 };
|
||||
result.Add(CalcSum(result));
|
||||
return result.ToArray();
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static byte CalcSum(List<byte> value)
|
||||
{
|
||||
int sum = 0;
|
||||
foreach (var item in value)
|
||||
{
|
||||
sum += (int)item;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public class FfuMayAirQuerySpeedHandler : FfuMayAirHandler
|
||||
{
|
||||
public FfuMayAirQuerySpeedHandler(FfuMayAir device, byte deviceAddress, byte groupAddress)
|
||||
: base(device, 0x15, 0x20, deviceAddress, groupAddress)
|
||||
{
|
||||
Name = "Query Speed";
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuMayAirMessage;
|
||||
|
||||
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class FfuMayAirQueryStatusHandler : FfuMayAirHandler
|
||||
{
|
||||
public FfuMayAirQueryStatusHandler(FfuMayAir device, byte deviceAddress, byte groupAddress)
|
||||
: base(device, 0x35, 0x00, deviceAddress, groupAddress, 0x00)
|
||||
{
|
||||
Name = "Query Status";
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as FfuMayAirMessage;
|
||||
//if (result.IsError)
|
||||
//{
|
||||
// Device.SetError(result.Data);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// if (int.TryParse(result.Data, out int code))
|
||||
// {
|
||||
// Device.SetErrorCode(code);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Device.SetError(result.Data + "format error");
|
||||
// }
|
||||
//}
|
||||
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user