SimChillerSmcHRS.cs 8.1 KB

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