123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- using System;
- using System.Threading;
- using MECF.Framework.Simulator.Core.Driver;
- namespace EfemDualSimulator.Devices
- {
- class MicrowaveGeneratorMockPMA : SerialPortDeviceSimulator
- {
- public enum GeneratorStatus
- {
- Unknown,
- OFF,
- ON,
- ERROR
- }
- public GeneratorStatus _simGeneratorStatus;
- private const string EOF = "\r";
- private const char MSG_DELIMITER = '_';
- private const string MOCKUP_PORT = "COM37";
- private ushort SetPower = 0;
- public MicrowaveGeneratorMockPMA(string com) : base(com, -1, EOF, MSG_DELIMITER, false)
- {
- _simGeneratorStatus = GeneratorStatus.Unknown;
- }
- protected override void ProcessUnsplitMessage(byte[] message)
- {
- if(message == null || message.Length == 0)
- throw new ArgumentException("Microwave hardware command message is invalid");
- var byte0 = message[0];
- var byte1 = message[1];
- if(byte1 == 0x03)
- {
- var commandBytes = new byte[] { message[3], message[2] };
- var address = BitConverter.ToUInt16(commandBytes, 0);
- var lenBytes = new byte[] { message[5], message[4] };
- var len = BitConverter.ToUInt16(lenBytes, 0);
- var response = new byte[5 + len * 2];
- response[0] = byte0;
- response[1] = byte1;
- response[2] = Convert.ToByte(len);
- ushort data = 0;
- if (address == MicrowareRFCommand.PowerOnOrOff)
- {
- data = (ushort)(_simGeneratorStatus == GeneratorStatus.ON ? 1 : 0);
- }
- else if (address == MicrowareRFCommand.PowerSetPoint ||
- address == MicrowareRFCommand.ForwardPower ||
- address == MicrowareRFCommand.ReflectedPower)
- {
- data = SetPower;
- }
- response[3] = BitConverter.GetBytes(data)[1];
- response[4] = BitConverter.GetBytes(data)[0];
- var crc = CRC16(response);
- response[5] = crc[0];
- response[6] = crc[1];
- OnWriteMessage(response);
- }
- else if(byte1 == 0x06)
- {
- var commandBytes = new byte[] { message[3], message[2] };
- var address = BitConverter.ToUInt16(commandBytes, 0);
- var dataBytes = new byte[] { message[5], message[4] };
- var data = BitConverter.ToUInt16(dataBytes, 0);
- if (address == MicrowareRFCommand.PowerOnOrOff)
- {
- _simGeneratorStatus = data == 1 ? GeneratorStatus.ON : GeneratorStatus.OFF;
- }
- else if(address == MicrowareRFCommand.PowerSetPoint)
- {
- SetPower = data;
- }
- OnWriteMessage(message);
- }
- //if (string.IsNullOrEmpty(message))
- // throw new ArgumentException("Hardware command message is invalid");
- //string sRes = string.Empty;
- //if (message.Contains(EOF))
- //{
- // message = message.Remove(message.Length - 1);
- //}
- //switch (message)
- //{
- // case "Q":
- // if (_simGeneratorStatus == GeneratorStatus.ON)
- // {
- // sRes = $"2010000 12345 0{SetPower} 00100 45678\r";
- // }
- // else if (_simGeneratorStatus == GeneratorStatus.OFF)
- // {
- // sRes = "2000000 12345 00000 00000 45678\r";
- // }
- // break;
- // case "G":
- // _simGeneratorStatus = GeneratorStatus.ON;
- // sRes = "\r";
- // break;
- // case "S":
- // _simGeneratorStatus = GeneratorStatus.OFF;
- // sRes = "\r";
- // break;
- // default:
- // if (message.EndsWith("W"))
- // {
- // SetPower = message.TrimEnd('W').Trim();
- // }
- // break;
- //}
- ////string[] strs = message.Split(MSG_DELIMITER);
- ////Thread.Sleep(2 * 1000);
- //sRes += "\r";
-
- }
- public static byte[] CRC16(byte[] data)
- {
- ushort crc = 0xFFFF;
- for (int i = 0; i < data.Length; i++)
- {
- crc = (ushort)(crc ^ (data[i]));
- for (int j = 0; j < 8; j++)
- {
- crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ 0xA001) : (ushort)(crc >> 1);
- }
- }
- byte hi = (byte)((crc & 0xFF00) >> 8); //高位置
- byte lo = (byte)(crc & 0x00FF); //低位置
- return new byte[] { hi, lo };
- }
- }
- static class MicrowareRFCommand
- {
- public const ushort PowerOnOrOff = 0x0081; //1:开 0:关 RW
- public const ushort PowerSetPoint = 0x0082; //功率设定值(W) RW
- public const ushort State = 0x008C; //设备故障 R
- public const ushort Reset = 0x009F; //清除异常 RW
- public const ushort Warning = 0x00A0; //整机预警 R
- public const ushort ForwardPower = 0x00A1; //整机输出功率 R
- public const ushort ReflectedPower = 0x00A2; //整机反射功率 R
- }
- }
|