ISerialPort.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using SHJX.Service.Common.ReadXML;
  5. using SHJX.Service.ServerClient.RS485;
  6. using SHJX.Service.Common.Logging;
  7. using Microsoft.Extensions.Logging;
  8. namespace SHJX.Service.ServerClient.TempController
  9. {
  10. public abstract class SerialPortImp
  11. {
  12. private readonly ReadConfigUtil _config;
  13. private static readonly ILogger logger = LogFactory.BuildLogger(typeof(SerialPortImp));
  14. protected SerialPortImp( ReadConfigUtil config)
  15. {
  16. _config = config;
  17. }
  18. /// <summary>
  19. /// 数据下发
  20. /// </summary>
  21. /// <param name="value"></param>
  22. /// <param name="port"></param>
  23. /// <returns></returns>
  24. protected virtual bool SendFormat(string value, object port)
  25. {
  26. if (!Rs485Client.IsValid) return false;
  27. var res = Rs485Client.SendData(value, '\r');
  28. return string.Concat(port, '%', '\r').Equals(res);
  29. }
  30. /// <summary>
  31. /// 电机停止
  32. /// </summary>
  33. /// <param name="nodeId"></param>
  34. /// <returns></returns>
  35. public bool MotorStop(byte nodeId)
  36. {
  37. var value = $"{nodeId}ST\r";
  38. return SendFormat(value, nodeId);
  39. }
  40. /// <summary>
  41. /// 电机移动
  42. /// </summary>
  43. /// <param name="args"></param>
  44. /// <returns></returns>
  45. protected bool MotorMove(params object[] args)
  46. {
  47. string value = string.Format("{0}FL{1}\r", args);
  48. var res = SendFormat(value, args[0]);
  49. logger.LogInformation($"写入电机移动,端口:{args[0]},数据:{value},写入:{(res ? "成功" : "失败")}");
  50. return res;
  51. }
  52. /// <summary>
  53. /// 寄存器操作
  54. /// </summary>
  55. public bool MotorStorage(byte nodeId, int address, string way)
  56. {
  57. var value = $"{nodeId}SO{address}{way}\r";
  58. var res = SendFormat(value, nodeId);
  59. logger.LogInformation($"写入寄存器数据,端口:{nodeId},地址:{address},数据:{value},写入:{(res ? "成功" : "失败")}");
  60. return res;
  61. }
  62. /// <summary>
  63. /// 检查寄存器
  64. /// </summary>
  65. /// <param name="nodeId"></param>
  66. /// <returns></returns>
  67. public char[] JudgeStorage(byte nodeId)
  68. {
  69. var value = $"{nodeId}IS\r";
  70. var res = Rs485Client.SendData(value, '\r');
  71. var chars = res.Replace($"{nodeId}IS=", string.Empty).Replace("\r", string.Empty).Reverse().ToArray();
  72. return chars;
  73. }
  74. public string JudgeXYZMove(byte nodeId)
  75. {
  76. var value = $"{nodeId}SC\r";
  77. if (!Rs485Client.IsValid) return "";
  78. var res = Rs485Client.SendData(value, '\r');
  79. return res.Split('=')[1];
  80. }
  81. /// <summary>
  82. /// 返回原点
  83. /// </summary>
  84. /// <param name="nodeId"></param>
  85. /// <param name="type"></param>
  86. /// <returns></returns>
  87. public bool MotorGoBack(byte nodeId, int type)
  88. {
  89. var value = $"{nodeId}FS{type}L\r";
  90. var res = SendFormat(value, nodeId);
  91. logger.LogInformation($"写入返回原点数据,端口:{nodeId},数据:{value},写入:{(res ? "成功" : "失败")}");
  92. if (!res) return res;
  93. do
  94. {
  95. var judgeRes = JudgeStorage(nodeId);
  96. if (judgeRes is null || judgeRes.Length < 1)
  97. {
  98. res = false;
  99. continue;
  100. }
  101. res = judgeRes[type - 1].Equals('0');
  102. } while (!res);
  103. Thread.Sleep(500);
  104. return res;
  105. }
  106. public bool MotorGoBackDissolvePort(byte nodeId, int type) //消解出来的时和液体硫酸亚铁胺单独写回原点,消解位因为不支持FS 回原点命令,出来只能用SH回原点,进去 的话可以用FS回原点
  107. {
  108. var value = $"{nodeId}SH{type}L\r";
  109. var res = SendFormat(value, nodeId);
  110. logger.LogInformation($"写入返回原点数据,端口:{nodeId},数据:{value},写入:{(res ? "成功" : "失败")}");
  111. if (!res) return res;
  112. do
  113. {
  114. var judgeRes = JudgeStorage(nodeId);
  115. if (judgeRes is null || judgeRes.Length < 1)
  116. {
  117. res = false;
  118. continue;
  119. }
  120. res = judgeRes[type - 1].Equals('0');
  121. } while (!res);
  122. Thread.Sleep(500);
  123. return res;
  124. }
  125. /// <summary>
  126. /// 设置速度
  127. /// </summary>
  128. /// <param name="nodeId"></param>
  129. /// <param name="speed"></param>
  130. /// <returns></returns>
  131. protected bool MotorSpeed(byte nodeId, double speed)
  132. {
  133. var value = $"{nodeId}VE{speed}\r";
  134. return SendFormat(value, nodeId);
  135. }
  136. /// <summary>
  137. /// 设置加速度
  138. /// </summary>
  139. /// <param name="nodeId"></param>
  140. /// <param name="speed"></param>
  141. /// <returns></returns>
  142. protected bool MotorAcSpeed(byte nodeId, double speed)
  143. {
  144. var value = $"{nodeId}AC{speed}\r";
  145. return SendFormat(value, nodeId);
  146. }
  147. /// <summary>
  148. /// 设置减速度
  149. /// </summary>
  150. /// <param name="nodeId"></param>
  151. /// <param name="speed"></param>
  152. /// <returns></returns>
  153. protected bool MotorDeSpeed(byte nodeId, double speed)
  154. {
  155. var value = $"{nodeId}DE{speed}\r";
  156. return SendFormat(value, nodeId);
  157. }
  158. /// <summary>
  159. /// 移动
  160. /// </summary>
  161. /// <param name="nodeId"></param>
  162. /// <param name="distance"></param>
  163. /// <returns></returns>
  164. public bool Move(byte nodeId, long distance, int interval, Double speed)
  165. {
  166. bool res = false;
  167. res = MotorMove(nodeId, distance);
  168. return res;
  169. }
  170. }
  171. }