SMCChillerMockPMA.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Threading;
  4. using Aitex.Core.RT.IOCore;
  5. using MECF.Framework.Common.Utilities;
  6. using MECF.Framework.Simulator.Core.Driver;
  7. namespace EfemDualSimulator.Devices
  8. {
  9. class SMCChillerMockPMA : SerialPortDeviceSimulator
  10. {
  11. Random _rd = new Random();
  12. private object _locker = new object();
  13. public bool IsNoResponse { get; internal set; }
  14. public bool IsRemote { get; internal set; }
  15. public bool IsRun { get; internal set; }
  16. public bool IsFault { get; internal set; }
  17. public string TempSetPoint { get; internal set; }
  18. public float TempFeedback { get; internal set; }
  19. public string Data_Alram1 { get; internal set; }
  20. public string Data_Alram2 { get; internal set; }
  21. public string Data_Alram3 { get; internal set; }
  22. public enum SMCChillerStatus
  23. {
  24. Open,
  25. Close,
  26. }
  27. public static SMCChillerStatus _simPumpStatus;
  28. private const string EOF = "\r\n";
  29. private const char MSG_DELIMITER = ' ';
  30. private const string MOCKUP_PORT = "COM43";
  31. public SMCChillerMockPMA(string com) : base(com, -1, EOF, MSG_DELIMITER)
  32. {
  33. _simPumpStatus = SMCChillerStatus.Close;
  34. }
  35. class Constant
  36. {
  37. public const string Start = ":";
  38. public const string End = "\r\n";
  39. public const string SlaveAddress = "01";
  40. public const string Function_R_M = "03";
  41. public const string Function_W_S = "06";
  42. public const string Function_W_M = "10";
  43. public const string Function_RW_M = "17";
  44. public const string Data_Address_GetTemp = "0000";
  45. public const string Data_Address_GetPress = "0002";
  46. public const string Data_Address_GetElec = "0003";
  47. public const string Data_Address_Status1 = "0004";
  48. public const string Data_Address_Alarm1 = "0005";
  49. public const string Data_Address_Alarm2 = "0006";
  50. public const string Data_Address_Alarm3 = "0007";
  51. public const string Data_Address_Status2 = "0009";
  52. public const string Data_Address_SetTemp = "000B";
  53. public const string Data_Address_SetRun = "000C";
  54. public const string SET_ON = SlaveAddress + Function_W_S + Data_Address_SetRun + "0001";
  55. public const string SET_OFF = SlaveAddress + Function_W_S + Data_Address_SetRun + "0000";
  56. public const string SET_TEMP = SlaveAddress + Function_W_S + Data_Address_SetTemp;
  57. public const string GET_ALL = SlaveAddress + Function_R_M + Data_Address_GetTemp + "000B";
  58. }
  59. protected override void ProcessUnsplitMessage(string UnsplitMessage)
  60. {
  61. //if (IsFault)
  62. //{
  63. // //OnWriteMessage($"{Constant.SlaveAddress}97");
  64. // //return;
  65. //}
  66. //读,默认7个全返回
  67. //if (message.Substring(9, 4) != "0000")
  68. //{
  69. // string data = $"{Constant.SlaveAddress}{ Constant.Function_R_M}0E";
  70. // for (int i = 0; i < 7; i++)
  71. // {
  72. // //data += $"0000";
  73. // //data += i.ToString().PadLeft(4,'0');
  74. // data += (i+256).ToString().PadLeft(4,'0');
  75. // }
  76. // OnWriteMessage(GetResponseText(data));
  77. //}
  78. foreach (string message in Regex.Split(UnsplitMessage, EOF, RegexOptions.None))
  79. {
  80. if(string.IsNullOrWhiteSpace(message))
  81. continue;
  82. if (message.Substring(3, 10) == "030000000B")
  83. {
  84. if (PortName == "COM128")
  85. {
  86. if (IsRun && IO.AI[$"PMA.AI_1_CoolantOutletTemp"].FloatValue > TempFeedback)
  87. {
  88. IO.AI[$"PMA.AI_1_CoolantOutletTemp"].FloatValue -= _rd.Next(1, 2);
  89. }
  90. else if (IsRun && IO.AI[$"PMA.AI_1_CoolantOutletTemp"].FloatValue < TempFeedback)
  91. {
  92. IO.AI[$"PMA.AI_1_CoolantOutletTemp"].FloatValue += _rd.Next(1, 2);
  93. }
  94. }
  95. if (PortName == "COM129")
  96. {
  97. if (IsRun && IO.AI[$"PMA.AI_2_CoolantOutletTemp"].FloatValue > TempFeedback)
  98. {
  99. IO.AI[$"PMA.AI_2_CoolantOutletTemp"].FloatValue -= _rd.Next(1, 2);
  100. }
  101. else if (IsRun && IO.AI[$"PMA.AI_2_CoolantOutletTemp"].FloatValue < TempFeedback)
  102. {
  103. IO.AI[$"PMA.AI_2_CoolantOutletTemp"].FloatValue += _rd.Next(1, 2);
  104. }
  105. }
  106. string data = $"{Constant.SlaveAddress}{Constant.Function_R_M}16";
  107. for (int i = 0; i < 13; i++)
  108. {
  109. //data += $"0000";
  110. //data += i.ToString().PadLeft(4,'0');
  111. //data += (i + 256).ToString().PadLeft(4, '0');
  112. if (i == 0)
  113. {
  114. int temp = (int)(TempFeedback * 10);
  115. data += temp.ToString("X4");
  116. }
  117. else if (i == 4)
  118. {
  119. data += IsRun ? "1".PadLeft(4, '0') : "0".PadLeft(4, '0');
  120. }
  121. else if (i == 5)
  122. {
  123. data += string.IsNullOrEmpty(Data_Alram1) ? "0000" : Data_Alram1;
  124. }
  125. else if (i == 6)
  126. {
  127. data += string.IsNullOrEmpty(Data_Alram2) ? "0000" : Data_Alram2;
  128. }
  129. else if (i == 7)
  130. {
  131. data += string.IsNullOrEmpty(Data_Alram3) ? "0000" : Data_Alram3;
  132. }
  133. else if (i == 11)
  134. {
  135. if (TempSetPoint != null)
  136. {
  137. data += TempSetPoint;
  138. }
  139. else
  140. {
  141. data += "0000";
  142. }
  143. }
  144. else
  145. {
  146. data += "0000";
  147. }
  148. }
  149. OnWriteMessage(GetResponseText(data));
  150. }
  151. if (message.Substring(3, 6) == "06000B")
  152. {
  153. TempSetPoint = message.Substring(9, 4);
  154. SetTemp();
  155. string data = $"{Constant.SlaveAddress}{Constant.Function_W_S}{message.Substring(5, 8)}";
  156. OnWriteMessage(GetResponseText(data));
  157. }
  158. if (message.Substring(3, 6) == "06000C")
  159. {
  160. IsRun = message.Substring(9, 4) == "0001";
  161. string data = $"{Constant.SlaveAddress}{Constant.Function_W_M}{message.Substring(5, 8)}";
  162. OnWriteMessage(GetResponseText(data));
  163. }
  164. }
  165. //写
  166. }
  167. public string GetResponseText(string str)
  168. {
  169. return ":" + str + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(str)).ToString("X2") + "\r\n";
  170. }
  171. internal void SetTemp()
  172. {
  173. int iBuf = 0;
  174. if (!string.IsNullOrEmpty(TempSetPoint))
  175. {
  176. for (int i = 3; i > -1; i--)
  177. {
  178. iBuf += Convert.ToInt32(TempSetPoint.Substring(i, 1), 16) * (int)Math.Pow(16, 3 - i);
  179. }
  180. if (iBuf > 1500)
  181. {
  182. iBuf = (~iBuf ^ 0xFFFF) / 10;
  183. }
  184. else
  185. {
  186. iBuf = iBuf / 10;
  187. }
  188. TempFeedback = iBuf;
  189. }
  190. }
  191. internal void SetAlarm(bool isAlarm)
  192. {
  193. if (isAlarm)
  194. {
  195. Data_Alram1 = "FFFF";
  196. Data_Alram2 = "FFFF";
  197. Data_Alram3 = "FFFF";
  198. }
  199. else
  200. {
  201. Data_Alram1 = "0000";
  202. Data_Alram2 = "0000";
  203. Data_Alram3 = "0000";
  204. }
  205. }
  206. }
  207. }