123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Aitex.Core.Common.DeviceData;
- using Aitex.Core.RT.Event;
- using MECF.Framework.Common.Communications;
- using static MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFs.AdTecTxLow.LowFrequencyRF;
- namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFs.AdTecTxLow
- {
- public abstract class LowFrequencyRFHandler : HandlerBase
- {
- public LowFrequencyRF Device { get; }
- private byte[] _functionCode;
- private byte[] _executionCode;
- private byte[] _parameter;
- protected LowFrequencyRFHandler(LowFrequencyRF device, byte[] functionCode, byte[] executionCode, byte[] parameter)
- : base(BuildMessage(functionCode, executionCode, parameter))
- {
- Device = device;
- _functionCode = functionCode;
- _executionCode = executionCode;
- _parameter = parameter;
- }
- private static byte _header = 0x40;
- private static byte _command = 0x55;
- private static byte _stopCR = 0x0D;
- private static byte[] BuildMessage(byte[] functionCode, byte[] executionCode, byte[] parameter)
- {
- List<byte> buffer = new List<byte>();
- buffer.Add(_header);
- buffer.Add(_command);
- if (functionCode != null && functionCode.Length > 0)
- {
- buffer.AddRange(functionCode);
- }
- if (executionCode != null && executionCode.Length > 0)
- {
- buffer.AddRange(executionCode);
- }
- if (parameter != null && parameter.Length > 0)
- {
- buffer.AddRange(parameter);
- }
- buffer.Add(_stopCR);
- return buffer.ToArray();
- }
- public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
- {
- LowFrequencyRFMessage response = msg as LowFrequencyRFMessage;
- ResponseMessage = msg;
- SetState(EnumHandlerState.Acked);
- if (response.IsResponse)
- {
- if (response.DataLength >= 1)
- {
- ParseData(response);
- }
- //SendAck();
- SetState(EnumHandlerState.Completed);
- transactionComplete = true;
- return true;
- }
- transactionComplete = false;
- return false;
- }
- protected static byte[] ToByteArray(string parameter)
- {
- if (parameter == null)
- return new byte[] { };
- return parameter.Split(',').Select(para => Convert.ToByte(para, 16)).ToArray();
- }
- protected virtual void ParseData(LowFrequencyRFMessage msg)
- {
- if (msg.Data[0] != 0)
- {
- var reason = TranslateCsrCode(msg.Data[0]);
- //Device.NoteError(reason);
- }
- }
- //public void SendAck()
- //{
- // Device.Connection.SendMessage(new byte[] { 0x06 });
- //}
- private static byte CalcSum(List<byte> data, int length)
- {
- byte ret = 0x00;
- for (var i = 0; i < length; i++)
- {
- ret ^= data[i];
- }
- return ret;
- }
- protected string TranslateCsrCode(int csrCode)
- {
- string ret = csrCode.ToString();
- switch (csrCode)
- {
- case 0:
- ret = null;//"Command accepted";
- break;
- case 1:
- ret = "Control Code Is Incorrect";
- break;
- case 2:
- ret = "Output Is On(Change Not Allowed)";
- break;
- case 4:
- ret = "Data Is Out Of Range";
- break;
- case 7:
- ret = "Active Fault(s) Exist";
- break;
- case 9:
- ret = "Data Byte Count Is Incorrect";
- break;
- case 19:
- ret = "Recipe Is Active(Change Not Allowed)";
- break;
- case 50:
- ret = "The Frequency Is Out Of Range";
- break;
- case 51:
- ret = "The Duty Cycle Is Out Of Range";
- break;
- case 53:
- ret = "The Device Controlled By The Command Is Not Detected";
- break;
- case 99:
- ret = "Command Not Accepted(There Is No Such Command)";
- break;
- default:
- break;
- }
- return ret;
- }
- }
- //00 ControlMode
- public class LowFrequencyRFSetControlModeHandler : LowFrequencyRFHandler
- {
- public LowFrequencyRFSetControlModeHandler(LowFrequencyRF device, byte mode)
- : base(device, BuildPara('0', '0'), BuildPara('0', '0'), BuildData(mode))
- {
- Name = "Set control mode";
- }
- private static byte[] BuildPara(char a, char b)
- {
- return new byte[] { (byte)a, (byte)b };
- }
- private static byte[] BuildData(byte mode)
- {
- return mode == 0x01 ? BuildPara('0', '1') : BuildPara('0', '2');
- }
- }
- public class LowFrequencyRFGetControlModeHandler : LowFrequencyRFHandler
- {
- public LowFrequencyRFGetControlModeHandler(LowFrequencyRF device)
- : base(device, BuildPara('0', '0'), BuildPara('0', '1'), null)
- {
- Name = "Get control power";
- }
- private static byte[] BuildPara(char a, char b)
- {
- return new byte[] { (byte)a, (byte)b };
- }
- protected override void ParseData(LowFrequencyRFMessage response)
- {
- EnumLowRfPowerCommunicationMode mode = EnumLowRfPowerCommunicationMode.Manual;
- if (response.Data[6] == (byte)'0' && response.Data[7] == (byte)'1')
- {
- mode = EnumLowRfPowerCommunicationMode.Manual;
- }
- else if (response.Data[6] == (byte)'0' && response.Data[7] == (byte)'2')
- {
- mode = EnumLowRfPowerCommunicationMode.RS232C;
- }
- Device.NoteCommMode(mode);
- }
- }
- //01 SetPoint Limit
- public class LowFrequencyRFSetPointLimitHandler : LowFrequencyRFHandler
- {
- public LowFrequencyRFSetPointLimitHandler(LowFrequencyRF device, int point)
- : base(device, BuildPara('0', '1'), BuildPara('0', '0'), BuildData(point))
- {
- Name = "SetPoint Limit";
- }
- private static byte[] BuildPara(char a, char b)
- {
- return new byte[] { (byte)a, (byte)b };
- }
- private static byte[] BuildData(int point)
- {
- string str = point.ToString("X4");
- return new byte[4] { (byte)str[0], (byte)str[1], (byte)str[2], (byte)str[3] };
- }
- }
- //02 Setpoint
- public class LowFrequencyRFSetPointHandler : LowFrequencyRFHandler
- {
- public LowFrequencyRFSetPointHandler(LowFrequencyRF device, int point)
- : base(device, BuildPara('0', '2'), BuildPara('0', '0'), BuildData(point))
- {
- Name = "Set Point";
- }
- private static byte[] BuildPara(char a, char b)
- {
- return new byte[] { (byte)a, (byte)b };
- }
- private static byte[] BuildData(int point)
- {
- string str = point.ToString("X4");
- return new byte[4] { (byte)str[0], (byte)str[1], (byte)str[2], (byte)str[3] };
- }
- }
- public class LowFrequencyRFGetSetPointHandler : LowFrequencyRFHandler
- {
- public LowFrequencyRFGetSetPointHandler(LowFrequencyRF device)
- : base(device, BuildPara('0', '2'), BuildPara('0', '1'), null)
- {
- Name = "Get SetPoint";
- }
- private static byte[] BuildPara(char a, char b)
- {
- return new byte[] { (byte)a, (byte)b };
- }
- protected override void ParseData(LowFrequencyRFMessage response)
- {
- int value = GetData(response.Data[6], response.Data[7], response.Data[8], response.Data[9]);
- Device.NotePowerSetPoint(value);
- }
- private static int GetData(byte b1, byte b2, byte b3, byte b4)
- {
- byte[] byteData = new byte[4] { b1, b2, b3, b4 };
- string str = Encoding.ASCII.GetString(byteData);
- int value = Convert.ToInt32(str, 16);
- return value;
- }
- }
- //03 00 off, AA on
- public class LowFrequencyRFSwitchOnOffHandler : LowFrequencyRFHandler
- {
- public LowFrequencyRFSwitchOnOffHandler(LowFrequencyRF device, bool isOn)
- : base(device, BuildPara('0', '3'), BuildPara('0', '0'), BuildData(isOn))
- {
- Name = "Switch " + (isOn ? "On" : "Off");
- }
- private static byte[] BuildPara(char a, char b)
- {
- return new byte[] { (byte)a, (byte)b };
- }
- private static byte[] BuildData(bool isOn)
- {
- return isOn ? BuildPara('A', 'A') : BuildPara('0', '0');
- }
- }
- public class LowFrequencyRFGetSwitchOnOffHandler : LowFrequencyRFHandler
- {
- public LowFrequencyRFGetSwitchOnOffHandler(LowFrequencyRF device)
- : base(device, BuildPara('0', '3'), BuildPara('0', '1'), null)
- {
- Name = "Get Switch Status";
- }
- private static byte[] BuildPara(char a, char b)
- {
- return new byte[] { (byte)a, (byte)b };
- }
- protected override void ParseData(LowFrequencyRFMessage response)
- {
- bool isOn = false;
- if (response.Data[6] == (byte)'0' && response.Data[7] == (byte)'0')
- {
- isOn = false;
- }
- else if (response.Data[6] == (byte)'A' && response.Data[7] == (byte)'A')
- {
- isOn = true;
- }
- Device.NoteStatus(isOn);
- }
- }
- //80 AlarmReset
- public class LowFrequencyRFAlarmResetHandler : LowFrequencyRFHandler
- {
- public LowFrequencyRFAlarmResetHandler(LowFrequencyRF device)
- : base(device, BuildPara('8', '0'), BuildPara('0', '0'), BuildPara('A', 'A'))
- {
- Name = "reset alarm" ;
- }
- private static byte[] BuildPara(char a, char b)
- {
- return new byte[] { (byte)a, (byte)b };
- }
- }
- public class LowFrequencyRFGetAlarmStatusHandler : LowFrequencyRFHandler
- {
- public LowFrequencyRFGetAlarmStatusHandler(LowFrequencyRF device)
- : base(device, BuildPara('8', '0'), BuildPara('0', '1'), null)
- {
- Name = "get alarm status";
- }
- private static byte[] BuildPara(char a, char b)
- {
- return new byte[] { (byte)a, (byte)b };
- }
- protected override void ParseData(LowFrequencyRFMessage response)
- {
- byte[] byteData = new byte[8] {response.Data[6], response.Data[7], response.Data[8], response.Data[9],
- response.Data[10], response.Data[11], response.Data[12], response.Data[13] };
- string errorCode = Encoding.ASCII.GetString(byteData);
- bool isError = false;
- if (errorCode != "00000000")
- isError = true;
- Device.NoteErrorStatus(isError, errorCode);
- }
- }
- //81 Waring Status
- public class LowFrequencyRFWaringStatusHandler : LowFrequencyRFHandler
- {
- public LowFrequencyRFWaringStatusHandler(LowFrequencyRF device)
- : base(device, BuildPara('8', '1'), BuildPara('0', '1'), null)
- {
- Name = "Waring Status";
- }
- private static byte[] BuildPara(char a, char b)
- {
- return new byte[] { (byte)a, (byte)b };
- }
- }
- //90 Actual Forward and Refected
- public class LowFrequencyGetForwardAndRefectedfHandler : LowFrequencyRFHandler
- {
- public LowFrequencyGetForwardAndRefectedfHandler(LowFrequencyRF device)
- : base(device, BuildPara('9', '0'), BuildPara('0', '3'), null)
- {
- Name = "Get Forward And Refectedf";
- }
- private static byte[] BuildPara(char a, char b)
- {
- return new byte[] { (byte)a, (byte)b };
- }
- protected override void ParseData(LowFrequencyRFMessage response)
- {
- Device.NoteForwardPowerAndReflectPower(response.Data);
- }
- }
- }
|