using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MECF.Framework.Common.Communications; namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FlowMeters { public abstract class Usf500NHandler : HandlerBase { public Usf500N Device { get; } public byte Address; public byte Channel; public byte FunctionCode; protected Usf500NHandler(Usf500N device, byte address, byte functionCode, byte[] datas) : base(BuildMessage(address, functionCode, datas)) { Device = device; Address = address; FunctionCode = functionCode; } private static byte[] BuildMessage(byte address, byte command,byte[] datas) { List result = new List() { address, command}; if (datas != null) { foreach (byte data in datas) result.Add(data); } result.AddRange(Calculate_CRC(result.ToArray())); return result.ToArray(); } public override bool HandleMessage(MessageBase msg, out bool handled) { ResponseMessage = msg; handled = true; return true; } private static byte[] Calculate_CRC(byte[] buffer) { ushort X = 0xFFFF; for (int i = 0; i < buffer.Length; i++) { int timeNo = 0; while (timeNo < 9) { timeNo++; X = (ushort)(X ^ buffer[i]); ushort tempValue = (ushort)(X & 1); X = (ushort)(X >> 1); if (tempValue == 1) { X = (ushort)(X ^ 0xA001); } } } return new byte[] { (byte)(X >> 8), (byte)(X & 0xFF) }; } } /// /// 设置组地址 /// /// 设置机地址 /// /// 查询最高转速 1 /// public class Usf500NQueryFlowRateHandler : Usf500NHandler { private int _channel; public Usf500NQueryFlowRateHandler(Usf500N device, byte address, int channel) : base(device, address,4,BuildData(channel)) { Name = "QueryFlowRate"; _channel = channel; } public override bool HandleMessage(MessageBase msg, out bool handled) { var result = msg as Usf500NMessage; handled = false; if (!result.IsResponse) return true; Device.ParseFlowRate(Address, (byte)_channel, result.ReadOutData); handled = true; return true; } private static byte[] BuildData(int channel) { ushort regAdd = (ushort)(channel * 100 + 1); byte[] ret = new byte[] { (byte)(regAdd >> 8), (byte)(regAdd & 0xFF),0,1 }; return ret; } } public class Usf500NQueryFlowVolumeHandler : Usf500NHandler { private int _channel; public Usf500NQueryFlowVolumeHandler(Usf500N device, byte address, int channel) : base(device, address, 4, BuildData(channel)) { Name = "QueryFlowRate"; _channel = channel; } public override bool HandleMessage(MessageBase msg, out bool handled) { var result = msg as Usf500NMessage; handled = false; if (!result.IsResponse) return true; Device.ParseFlowVolume(Address, (byte)_channel, result.ReadOutData); handled = true; return true; } private static byte[] BuildData(int channel) { ushort regAdd = (ushort)(channel * 100 + 2); byte[] ret = new byte[] { (byte)(regAdd >> 8), (byte)(regAdd & 0xFF), 0, 1 }; return ret; } } public class Usf500NResetFlowVolumeHandler : Usf500NHandler { private int _channel; public Usf500NResetFlowVolumeHandler(Usf500N device, byte address, int channel) : base(device, address, 6, BuildData(channel)) { Name = "QueryFlowRate"; _channel = channel; } public override bool HandleMessage(MessageBase msg, out bool handled) { var result = msg as Usf500NMessage; handled = false; if (!result.IsResponse) return true; //Device.ParseFlowRate(Address, (byte)_channel, result.ReadOutData); handled = true; return true; } private static byte[] BuildData(int channel) { ushort regAdd = (ushort)(channel * 100 + 3); byte[] ret = new byte[] { (byte)(regAdd >> 8), (byte)(regAdd & 0xFF), 0, 2 }; return ret; } } }