味魔方demo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
5.7 KiB

  1. using BPA.Communication;
  2. using BPA.Helper;
  3. using Microsoft.AspNetCore.Http;
  4. namespace CommcationLibray
  5. {
  6. /// <summary>
  7. /// modbusTcp连接基类
  8. /// </summary>
  9. public class ModbusTCP
  10. {
  11. ICommModbus commHelper;
  12. /// <summary>
  13. /// 连接modbusTcp
  14. /// </summary>
  15. /// <param name="ip">IP</param>
  16. /// <param name="Port">端口</param>
  17. /// <returns></returns>
  18. public async Task<Result> ConnectModbusTcp(string ip, int Port)
  19. {
  20. Result results = new Result();
  21. await Task.Run( new Action(() =>
  22. {
  23. CommHelper.CreateModbusTcp(ip, Port).OnSuccess(s =>
  24. {
  25. results.State = s.IsSuccess;
  26. results.Exception = s.Message;
  27. commHelper = s.Content;
  28. }).OnFailure(s =>
  29. {
  30. results.State = s.IsSuccess;
  31. results.Exception = s.Message;
  32. });
  33. }));
  34. return results;
  35. }
  36. /// <summary>
  37. /// 获取订单号
  38. /// </summary>
  39. /// <param name="address">读取保持寄存器 的地址</param>
  40. /// <typeparam name="T">需要返回的数据类型</typeparam>
  41. /// <param name="length">数据长度</param>
  42. /// <param name="format">数据格式</param>
  43. /// <returns></returns>
  44. public Result GetOrders<T>(string address, ushort length = 1, BPADataFormat format = BPADataFormat.ABCD)
  45. {
  46. Result results = new Result();
  47. try
  48. {
  49. if (commHelper != null)
  50. {
  51. var GetOrdersData = commHelper.ReadHoldRegister<T>(address, length,format);
  52. results.State = GetOrdersData.IsSuccess;
  53. results.Exception = GetOrdersData.Message;
  54. results.Datas = GetOrdersData.Content;
  55. }
  56. else
  57. {
  58. results.State = false;
  59. results.Exception = "ICommModbus 对象为空!!!";
  60. }
  61. }
  62. catch (Exception ex)
  63. {
  64. results.State = false;
  65. results.Exception = ex.Message;
  66. }
  67. return results;
  68. }
  69. /// <summary>
  70. /// 写入配方编号
  71. /// </summary>
  72. /// <typeparam name="T">写入的数据类型</typeparam>
  73. /// <param name="address">寄存器地址</param>
  74. /// <param name="value">写入的值</param>
  75. /// <param name="len">地址长度</param>
  76. /// <param name="format">数据格式</param>
  77. /// <returns></returns>
  78. public Result WriteRecipeNumber<T>(string address, T value, int len = 0, BPADataFormat format = BPADataFormat.ABCD)
  79. {
  80. Result results = new Result();
  81. try
  82. {
  83. if (commHelper != null)
  84. {
  85. var WriteState = commHelper.WriteHoldRegister<T>(address, value, len, format);
  86. results.State = WriteState.IsSuccess;
  87. results.Exception = WriteState.Message;
  88. }
  89. else
  90. {
  91. results.State = false;
  92. results.Exception = "ICommModbus 对象为空!!!";
  93. }
  94. }
  95. catch (Exception ex)
  96. {
  97. results.State = false;
  98. results.Exception = ex.Message;
  99. }
  100. return results;
  101. }
  102. /// <summary>
  103. /// 确定写入编号
  104. /// </summary>
  105. /// <param name="address">线圈地址</param>
  106. /// <param name="value">写入的值</param>
  107. /// <returns></returns>
  108. public Result DetermineWriteNumberCommand(string address, bool value)
  109. {
  110. return WriteCoilData(address, value);
  111. }
  112. /// <summary>
  113. /// 写入取消配料的点位
  114. /// </summary>
  115. /// <param name="address">线圈地址</param>
  116. /// <param name="value">写入线圈的值</param>
  117. /// <returns></returns>
  118. public Result WriteCancelIngredients(string address, bool value)
  119. {
  120. return WriteCoilData(address,value);
  121. }
  122. /// <summary>
  123. /// 读取取消配料的点位
  124. /// </summary>
  125. /// <param name="address">读取线圈的地址</param>
  126. /// <returns></returns>
  127. public Result ReadCancelIngredients(string address)
  128. {
  129. return ReadCoilData(address);
  130. }
  131. /// <summary>
  132. /// 获取设备状态
  133. /// </summary>
  134. /// <param name="address">读取线圈的地址</param>
  135. /// <returns></returns>
  136. public Result ReadDeviceStatus(string address)
  137. {
  138. return ReadCoilData(address);
  139. }
  140. /// <summary>
  141. /// 读取线圈数据
  142. /// </summary>
  143. /// <param name="address">读取线圈的地址</param>
  144. /// <returns></returns>
  145. private Result ReadCoilData(string address)
  146. {
  147. Result results = new Result();
  148. try
  149. {
  150. if (commHelper != null)
  151. {
  152. var readState = commHelper.ReadCoil(address);
  153. results.State = readState.IsSuccess;
  154. results.Datas = readState.Content;
  155. results.Exception = readState.Message;
  156. }
  157. else
  158. {
  159. results.State = false;
  160. results.Exception = "ICommModbus 对象为空!!!";
  161. }
  162. }
  163. catch (Exception ex)
  164. {
  165. results.State = false;
  166. results.Exception = ex.Message;
  167. }
  168. return results;
  169. }
  170. /// <summary>
  171. /// 写入线圈数据
  172. /// </summary>
  173. /// <param name="address"></param>
  174. /// <param name="value">写入线圈的值</param>
  175. /// <returns></returns>
  176. private Result WriteCoilData(string address,bool value)
  177. {
  178. Result results = new Result();
  179. try
  180. {
  181. if (commHelper != null)
  182. {
  183. var WriteState = commHelper.WriteCoil(address, value);
  184. results.State = WriteState.IsSuccess;
  185. results.Exception = WriteState.Message;
  186. }
  187. else
  188. {
  189. results.State = false;
  190. results.Exception = "ICommModbus 对象为空!!!";
  191. }
  192. }
  193. catch (Exception ex)
  194. {
  195. results.State = false;
  196. results.Exception = ex.Message;
  197. }
  198. return results;
  199. }
  200. }
  201. }