PortExecute.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using SHJX.Service.Control.Extends;
  2. using SHJX.Service.Control.Common.Assets;
  3. namespace SHJX.Service.Control.Execute
  4. {
  5. public static class PortExecute
  6. {
  7. private static readonly ILogger logger = LogFactory.BuildLogger(typeof(PortExecute));
  8. //public static bool AxisZdownOrUp = false;//z轴上下状态
  9. private static readonly object obj_locker = new();
  10. /// <summary>
  11. /// 电机返回原点
  12. /// </summary>
  13. /// <param name="typeName"></param>
  14. /// <returns></returns>
  15. public static bool MotorGoBack(this string typeName)
  16. {
  17. int register = ConfigInstance.GetPortRegister(typeName);
  18. bool res;
  19. res = WriteGobackData();
  20. double executedTime = 0;
  21. do
  22. {
  23. /* Mark:
  24. * 暂时只针对Z轴使用
  25. */
  26. if (typeName.Equals(EquipmentNames.AxisZ) && executedTime > 15)
  27. {
  28. logger.LogInformation($"{EquipmentNames.AxisZ}在{executedTime}秒后还未返回原点,重新写入返回原点数据!");
  29. WriteGobackData();
  30. executedTime = 0;
  31. }
  32. char[] judgeRes = typeName.MotorRead();
  33. if (judgeRes is null or { Length: 0 })
  34. {
  35. continue;
  36. }
  37. res = judgeRes[register -1].Equals('0');
  38. Thread.Sleep(200);
  39. executedTime += 0.2;
  40. } while (!res);
  41. return res;
  42. bool WriteGobackData()
  43. {
  44. bool gobackRes;
  45. int defeatCount = 0;
  46. do
  47. {
  48. gobackRes = (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(typeName).SetType(ArgType.GoBack).SetValue(register).Execute();
  49. if (!gobackRes)
  50. {
  51. defeatCount++;
  52. logger.LogInformation($"第{defeatCount}失败,等待1s后重试!");
  53. Thread.Sleep(1000);
  54. }
  55. } while (!gobackRes);
  56. return gobackRes;
  57. }
  58. }
  59. /// <summary>
  60. /// 温度读取
  61. /// </summary>
  62. /// <param name="typeName"></param>
  63. /// <returns></returns>
  64. public static Dictionary<string, double> HeatRead(this string typeName)
  65. {
  66. return PortProducerFactory.Factory(WriteWay.Read).SetName(typeName).SetType(ArgType.HeatRead).Execute() as Dictionary<string, double>;
  67. }
  68. /// <summary>
  69. /// 电机读取
  70. /// </summary>
  71. /// <param name="typeName"></param>
  72. /// <param name="register"></param>
  73. /// <returns></returns>
  74. public static char[] MotorRead(this string typeName)
  75. {
  76. lock (obj_locker)
  77. {
  78. return PortProducerFactory.Factory(WriteWay.Read).SetName(typeName).SetType(ArgType.Read).Execute() as char[];
  79. }
  80. }
  81. /// <summary>
  82. /// 液体电机移动
  83. /// </summary>
  84. /// <param name="name"></param>
  85. /// <param name="value"></param>
  86. /// <returns></returns>
  87. public static bool LiquidMotorMove(this string name, object value)
  88. {
  89. bool res;
  90. int defeatCount = 0;
  91. do
  92. {
  93. res = (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Move).SetValue(value).Execute();
  94. if (res)
  95. {
  96. Thread.Sleep(Math.Abs(Convert.ToInt32(value) / 20000 * SpeedSleep.Calculation(name)));
  97. }
  98. else
  99. {
  100. defeatCount++;
  101. logger.LogInformation($"第{defeatCount}失败,等待3s后重试!");
  102. Thread.Sleep(1000);
  103. }
  104. } while (!res);
  105. return res;
  106. }
  107. /// <summary>
  108. /// 运动电机的移动
  109. /// </summary>
  110. /// <param name="name"></param>
  111. /// <param name="value"></param>
  112. /// <returns></returns>
  113. public static bool MotorMove(this string name, object value)
  114. {
  115. bool res;
  116. int defeatCount = 0;
  117. do
  118. {
  119. res = (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Move).SetValue(value).Execute();
  120. if (res)
  121. {
  122. break;
  123. }
  124. else
  125. {
  126. defeatCount++;
  127. logger.LogInformation($"第{defeatCount}失败,等待3s后重试!");
  128. Thread.Sleep(3000);
  129. }
  130. } while (!res);
  131. return res;
  132. }
  133. /// <summary>
  134. /// 打开寄存器
  135. /// </summary>
  136. /// <param name="name"></param>
  137. /// <returns></returns>
  138. public static bool RegisterOpen(this string registerName)
  139. {
  140. lock (obj_locker)
  141. {
  142. int register = ConfigInstance.GetPortRegister(registerName);
  143. int defeatCount = 0;
  144. bool res;
  145. do
  146. {
  147. res = (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(registerName).SetType(ArgType.Open).SetValue(register).Execute();
  148. logger.LogDebug($"写寄存器{registerName}" + "打开****************" + register.ToString());
  149. if (!res)
  150. {
  151. defeatCount++;
  152. logger.LogDebug($"第{defeatCount}失败,等待1s后重试!");
  153. Thread.Sleep(1000);
  154. }
  155. } while (!res);
  156. return res;
  157. }
  158. }
  159. /// <summary>
  160. /// 关闭寄存器
  161. /// </summary>
  162. /// <param name="name"></param>
  163. /// <returns></returns>
  164. public static bool RegisterClose(this string name)
  165. {
  166. lock (obj_locker)
  167. {
  168. int register = ConfigInstance.GetPortRegister(name);
  169. bool res;
  170. int defeatCount = 0;
  171. do
  172. {
  173. res = (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Close).SetValue(register).Execute();
  174. logger.LogDebug($"写寄存器{name}" + "关闭*****************" + register.ToString());
  175. if (!res)
  176. {
  177. defeatCount++;
  178. logger.LogDebug($"第{defeatCount}失败,等待1s后重试!");
  179. Thread.Sleep(1000);
  180. }
  181. } while (!res);
  182. return res;
  183. }
  184. }
  185. /// <summary>
  186. /// 电机停止
  187. /// </summary>
  188. /// <param name="name"></param>
  189. /// <returns></returns>
  190. public static bool MotorStop(this string name)
  191. {
  192. return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Stop).Execute();
  193. }
  194. /// <summary>
  195. /// 速度写入
  196. /// </summary>
  197. /// <param name="name"></param>
  198. /// <param name="value"></param>
  199. /// <returns></returns>
  200. public static bool WriteSpeed(this string name, object value)
  201. {
  202. return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Speed).SetValue(value).Execute();
  203. }
  204. /// <summary>
  205. /// 写入加速度
  206. /// </summary>
  207. /// <param name="name"></param>
  208. /// <param name="value"></param>
  209. /// <returns></returns>
  210. public static bool WriteAcSpeed(this string name, object value)
  211. {
  212. return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.AcSpeed).SetValue(value).Execute();
  213. }
  214. /// <summary>
  215. /// 写入减速度
  216. /// </summary>
  217. /// <param name="name"></param>
  218. /// <param name="value"></param>
  219. /// <returns></returns>
  220. public static bool WriteDeSpeed(this string name, object value)
  221. {
  222. return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.DeSpeed).SetValue(value).Execute();
  223. }
  224. /// <summary>
  225. /// 写入温度
  226. /// </summary>
  227. /// <param name="name"></param>
  228. /// <param name="value"></param>
  229. /// <returns></returns>
  230. public static bool WriteTemperature(this string name, object value)
  231. {
  232. return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Temp).SetValue(value).Execute();
  233. }
  234. /// <summary>
  235. /// 写入比例
  236. /// </summary>
  237. /// <param name="name"></param>
  238. /// <param name="value"></param>
  239. /// <returns></returns>
  240. public static bool WriteRatio(this string name, object value)
  241. {
  242. return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Ratio).SetValue(value).Execute();
  243. }
  244. /// <summary>
  245. /// 写入积分
  246. /// </summary>
  247. /// <param name="name"></param>
  248. /// <param name="value"></param>
  249. /// <returns></returns>
  250. public static bool WriteIntegral(this string name, object value)
  251. {
  252. return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Integral).SetValue(value).Execute();
  253. }
  254. /// <summary>
  255. /// 写入微分
  256. /// </summary>
  257. /// <param name="name"></param>
  258. /// <param name="value"></param>
  259. /// <returns></returns>
  260. public static bool WriteDifferential(this string name, object value)
  261. {
  262. return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Differential).SetValue(value).Execute();
  263. }
  264. /// <summary>
  265. /// 写入积分默认值
  266. /// </summary>
  267. /// <param name="name"></param>
  268. /// <param name="value"></param>
  269. /// <returns></returns>
  270. public static bool WriteIntegralDeafult(this string name, object value)
  271. {
  272. return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.IntegralDeafult).SetValue(value).Execute();
  273. }
  274. /// <summary>
  275. /// 写入Pid
  276. /// </summary>
  277. /// <param name="name"></param>
  278. /// <returns></returns>
  279. public static bool WritePid(this string name)
  280. {
  281. PID pid = DataManagerInstance.GetPid();
  282. return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Pid).SetValue(pid).Execute();
  283. }
  284. public static ushort[] HumitureRead()
  285. {
  286. return (ushort[])PortProducerFactory.Factory(WriteWay.Read).SetType(ArgType.Humiture).Execute();
  287. }
  288. }
  289. }