| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 | 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<byte> result = new List<byte>() { 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) };        }    }    /// <summary>    /// 设置组地址    /// <summary>    /// 设置机地址    /// <summary>    /// 查询最高转速 1    /// </summary>    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;        }    }}
 |