123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- using System;
- using System.Text.RegularExpressions;
- using System.Threading;
- using Aitex.Core.RT.IOCore;
- using MECF.Framework.Common.Utilities;
- using MECF.Framework.Simulator.Core.Driver;
- namespace EfemDualSimulator.Devices
- {
- class SMCChillerMockPMB : SerialPortDeviceSimulator
- {
- Random _rd = new Random();
- private object _locker = new object();
- public bool IsNoResponse { get; internal set; }
- public bool IsRemote { get; internal set; }
- public bool IsRun { get; internal set; }
- public bool IsFault { get; internal set; }
- public string TempSetPoint { get; internal set; }
- public float TempFeedback { get; internal set; }
- public string Data_Alram1 { get; internal set; }
- public string Data_Alram2 { get; internal set; }
- public string Data_Alram3 { get; internal set; }
- public enum SMCChillerStatus
- {
- Open,
- Close,
- }
- public static SMCChillerStatus _simPumpStatus;
- private const string EOF = "\r\n";
- private const char MSG_DELIMITER = ' ';
- private const string MOCKUP_PORT = "COM43";
- public SMCChillerMockPMB(string com) : base(com, -1, EOF, MSG_DELIMITER)
- {
- _simPumpStatus = SMCChillerStatus.Close;
- }
- class Constant
- {
- public const string Start = ":";
- public const string End = "\r\n";
- public const string SlaveAddress = "01";
- public const string Function_R_M = "03";
- public const string Function_W_S = "06";
- public const string Function_W_M = "10";
- public const string Function_RW_M = "17";
- public const string Data_Address_GetTemp = "0000";
- public const string Data_Address_GetPress = "0002";
- public const string Data_Address_GetElec = "0003";
- public const string Data_Address_Status1 = "0004";
- public const string Data_Address_Alarm1 = "0005";
- public const string Data_Address_Alarm2 = "0006";
- public const string Data_Address_Alarm3 = "0007";
- public const string Data_Address_Status2 = "0009";
- public const string Data_Address_SetTemp = "000B";
- public const string Data_Address_SetRun = "000C";
- public const string SET_ON = SlaveAddress + Function_W_S + Data_Address_SetRun + "0001";
- public const string SET_OFF = SlaveAddress + Function_W_S + Data_Address_SetRun + "0000";
- public const string SET_TEMP = SlaveAddress + Function_W_S + Data_Address_SetTemp;
- public const string GET_ALL = SlaveAddress + Function_R_M + Data_Address_GetTemp + "000B";
- }
- protected override void ProcessUnsplitMessage(string UnsplitMessage)
- {
- //if (IsFault)
- //{
- // //OnWriteMessage($"{Constant.SlaveAddress}97");
- // //return;
- //}
- //读,默认7个全返回
- //if (message.Substring(9, 4) != "0000")
- //{
- // string data = $"{Constant.SlaveAddress}{ Constant.Function_R_M}0E";
- // for (int i = 0; i < 7; i++)
- // {
- // //data += $"0000";
- // //data += i.ToString().PadLeft(4,'0');
- // data += (i+256).ToString().PadLeft(4,'0');
- // }
- // OnWriteMessage(GetResponseText(data));
- //}
- foreach (string message in Regex.Split(UnsplitMessage, EOF, RegexOptions.None))
- {
- if (string.IsNullOrWhiteSpace(message))
- continue;
- if (message.Substring(3, 10) == "030000000B")
- {
- if (PortName == "COM148")
- {
- if (IsRun && IO.AI[$"PMB.AI_1_CoolantOutletTemp"].FloatValue > TempFeedback)
- {
- IO.AI[$"PMB.AI_1_CoolantOutletTemp"].FloatValue -= _rd.Next(1, 2);
- }
- else if (IsRun && IO.AI[$"PMB.AI_1_CoolantOutletTemp"].FloatValue < TempFeedback)
- {
- IO.AI[$"PMB.AI_1_CoolantOutletTemp"].FloatValue += _rd.Next(1, 2);
- }
- }
- if (PortName == "COM149")
- {
- if (IsRun && IO.AI[$"PMB.AI_2_CoolantOutletTemp"].FloatValue > TempFeedback)
- {
- IO.AI[$"PMB.AI_2_CoolantOutletTemp"].FloatValue -= _rd.Next(1, 2);
- }
- else if (IsRun && IO.AI[$"PMB.AI_2_CoolantOutletTemp"].FloatValue < TempFeedback)
- {
- IO.AI[$"PMB.AI_2_CoolantOutletTemp"].FloatValue += _rd.Next(1, 2);
- }
- }
- string data = $"{Constant.SlaveAddress}{Constant.Function_R_M}16";
- for (int i = 0; i < 13; i++)
- {
- //data += $"0000";
- //data += i.ToString().PadLeft(4,'0');
- //data += (i + 256).ToString().PadLeft(4, '0');
- if (i == 0)
- {
- int temp = (int)(TempFeedback * 10);
- data += temp.ToString("X4");
- }
- else if (i == 4)
- {
- data += IsRun ? "1".PadLeft(4, '0') : "0".PadLeft(4, '0');
- }
- else if (i == 5)
- {
- data += string.IsNullOrEmpty(Data_Alram1) ? "0000" : Data_Alram1;
- }
- else if (i == 6)
- {
- data += string.IsNullOrEmpty(Data_Alram2) ? "0000" : Data_Alram2;
- }
- else if (i == 7)
- {
- data += string.IsNullOrEmpty(Data_Alram3) ? "0000" : Data_Alram3;
- }
- else if (i == 11)
- {
- if (TempSetPoint != null)
- {
- data += TempSetPoint;
- }
- else
- {
- data += "0000";
- }
- }
- else
- {
- data += "0000";
- }
- }
- OnWriteMessage(GetResponseText(data));
- }
- if (message.Substring(3, 6) == "06000B")
- {
- TempSetPoint = message.Substring(9, 4);
- SetTemp();
- string data = $"{Constant.SlaveAddress}{Constant.Function_W_S}{message.Substring(5, 8)}";
- OnWriteMessage(GetResponseText(data));
- }
- if (message.Substring(3, 6) == "06000C")
- {
- IsRun = message.Substring(9, 4) == "0001";
- string data = $"{Constant.SlaveAddress}{Constant.Function_W_M}{message.Substring(5, 8)}";
- OnWriteMessage(GetResponseText(data));
- }
- }
- //写
- }
- public string GetResponseText(string str)
- {
- return ":" + str + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(str)).ToString("X2") + "\r\n";
- }
- internal void SetTemp()
- {
- int iBuf = 0;
- if (!string.IsNullOrEmpty(TempSetPoint))
- {
- for (int i = 3; i > -1; i--)
- {
- iBuf += Convert.ToInt32(TempSetPoint.Substring(i, 1), 16) * (int)Math.Pow(16, 3 - i);
- }
- if (iBuf > 1500)
- {
- iBuf = (~iBuf ^ 0xFFFF) / 10;
- }
- else
- {
- iBuf = iBuf / 10;
- }
- TempFeedback = iBuf;
- }
- }
- internal void SetAlarm(bool isAlarm)
- {
- if (isAlarm)
- {
- Data_Alram1 = "FFFF";
- Data_Alram2 = "FFFF";
- Data_Alram3 = "FFFF";
- }
- else
- {
- Data_Alram1 = "0000";
- Data_Alram2 = "0000";
- Data_Alram3 = "0000";
- }
- }
- }
- }
|