FestoModbusDevice.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using Aitex.Core.RT.Log;
  2. using Aitex.Core.Util;
  3. using DocumentFormat.OpenXml.Wordprocessing;
  4. using MECF.Framework.Common.Device.PowerSupplier;
  5. using MECF.Framework.Common.Net;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace MECF.Framework.Common.Device.Festo
  13. {
  14. public class FestoModbusDevice : JetMessageTcpClient<FestoMessage, FestoCommand>
  15. {
  16. #region 内部变量
  17. /// <summary>
  18. /// 名称
  19. /// </summary>
  20. private string _name;
  21. /// <summary>
  22. /// 上一次错误信息
  23. /// </summary>
  24. private string _lastErrorMsg = "";
  25. /// <summary>
  26. /// do的数量
  27. /// </summary>
  28. private ushort _addressCount = 6;
  29. /// <summary>
  30. /// di开始地址
  31. /// </summary>
  32. private ushort _diStartAddress = 45395;
  33. /// <summary>
  34. /// 通道
  35. /// </summary>
  36. private byte _channel = 1;
  37. /// <summary>
  38. /// IP地址
  39. /// </summary>
  40. private string _ip = "";
  41. /// <summary>
  42. /// 端口号
  43. /// </summary>
  44. private int _port = 502;
  45. /// <summary>
  46. /// 定时器
  47. /// </summary>
  48. private PeriodicJob _periodicJob;
  49. /// <summary>
  50. /// 共享锁
  51. /// </summary>
  52. private object _locker = new object();
  53. /// <summary>
  54. /// 共享锁时长
  55. /// </summary>
  56. private int _lockerTime = 2000;
  57. #endregion
  58. /// <summary>
  59. /// 构造函数
  60. /// </summary>
  61. /// <param name="ip"></param>
  62. /// <param name="port"></param>
  63. public FestoModbusDevice(string name, string ip, int port,ushort diStartAddress,byte channel) : base(ip, port)
  64. {
  65. ReceiveTimeout = 1000;
  66. SendTimeout = 1000;
  67. ConnectTimeout = 1000;
  68. _name = name;
  69. _diStartAddress=diStartAddress;
  70. _ip = ip;
  71. _port = port;
  72. _periodicJob = new PeriodicJob(200, OnTimer, $"festo {name} timer", false,true);
  73. }
  74. /// <summary>
  75. /// 定时器执行
  76. /// </summary>
  77. /// <returns></returns>
  78. private bool OnTimer()
  79. {
  80. if(Monitor.TryEnter(_locker, _lockerTime))
  81. {
  82. ApplyAllDatas();
  83. Monitor.Exit(_locker);
  84. }
  85. return true;
  86. }
  87. /// <summary>
  88. /// 更新地址数量
  89. /// </summary>
  90. /// <param name="addressCount"></param>
  91. public void InitializeAddressCount(ushort addressCount)
  92. {
  93. _addressCount = addressCount;
  94. NetResult result = Connect();
  95. if (result.IsSuccess)
  96. {
  97. ApplyAllDatas();
  98. LOG.WriteLog(eEvent.INFO_FESTO, _name, $"connect {_ip}:{_port} success");
  99. }
  100. else
  101. {
  102. LOG.WriteLog(eEvent.INFO_FESTO, _name, $"connect {_ip}:{_port} failed");
  103. }
  104. _periodicJob.Start();
  105. }
  106. /// <summary>
  107. /// 设置Festo数值
  108. /// </summary>
  109. /// <param name="address"></param>
  110. /// <param name="value"></param>
  111. /// <returns></returns>
  112. public bool SetFestoValue(ushort address,byte value)
  113. {
  114. FestoCommand command = new FestoCommand();
  115. command.Channel = _channel;
  116. command.Address=address;
  117. command.CommandCode = 0x06;
  118. command.Datas = new byte[1] { value };
  119. if (Monitor.TryEnter(_locker, _lockerTime))
  120. {
  121. bool result= SetOperation(command);
  122. Monitor.Exit(_locker);
  123. return result;
  124. }
  125. else
  126. {
  127. LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"Write apply locker over {_lockerTime}");
  128. return false;
  129. }
  130. }
  131. /// <summary>
  132. /// 设置操作
  133. /// </summary>
  134. /// <param name="command"></param>
  135. /// <returns></returns>
  136. private bool SetOperation(FestoCommand command)
  137. {
  138. if (Connected)
  139. {
  140. NetResult netResult = SetData(command);
  141. if (!netResult.IsSuccess)
  142. {
  143. WriteErrMsg($"write write {command.Address.ToString("X2")} value {command.Datas[0]} failed,{netResult.Message}");
  144. return false;
  145. }
  146. return true;
  147. }
  148. else
  149. {
  150. NetResult netResult = Connect();
  151. if (netResult.IsSuccess)
  152. {
  153. netResult = SetData(command);
  154. if (!netResult.IsSuccess)
  155. {
  156. WriteErrMsg($"write {command.Address.ToString("X2")} value {command.Datas[0]} failed,{netResult.Message}");
  157. return false;
  158. }
  159. return true;
  160. }
  161. else
  162. {
  163. WriteErrMsg("connect failed");
  164. return false;
  165. }
  166. }
  167. }
  168. /// <summary>
  169. /// 申请所有数据
  170. /// </summary>
  171. public void ApplyAllDatas()
  172. {
  173. FestoCommand command = new FestoCommand();
  174. command.Address = _diStartAddress;
  175. command.RegisterCount = (ushort)(_addressCount*2);
  176. command.CommandCode = 0x03;
  177. command.Channel = _channel;
  178. ApplyDataOperation(command);
  179. }
  180. /// <summary>
  181. /// 申请数据操作
  182. /// </summary>
  183. /// <param name="command"></param>
  184. private void ApplyDataOperation(FestoCommand command)
  185. {
  186. if (!Connected)
  187. {
  188. NetResult connectResult = Connect();
  189. if (!connectResult.IsSuccess)
  190. {
  191. WriteErrMsg("connect failed");
  192. return;
  193. }
  194. }
  195. NetResult<FestoCommand> netResult = ApplyData(command);
  196. if (!netResult.IsSuccess)
  197. {
  198. WriteErrMsg($"apply {command.Address.ToString("X2")} failed,{netResult.Message}");
  199. return;
  200. }
  201. if (netResult.Data.Datas != null)
  202. {
  203. //bit0--高8位,bit1-低8位,bit2-下一地址高8位,bit3--下一地址低8位,只取bit1
  204. byte[] bytes = new byte[_addressCount];
  205. for(int i = 0; i < netResult.Data.Datas.Length; i += 4)
  206. {
  207. if (i + 1 < netResult.Data.Datas.Length&&i/4<_addressCount)
  208. {
  209. byte data = netResult.Data.Datas[i + 1];
  210. bytes[i / 4] = data;
  211. }
  212. }
  213. FestoControllerCfgManager.Instance.UpdateFestoData(_name, bytes);
  214. }
  215. }
  216. /// <summary>
  217. /// 写错误日志
  218. /// </summary>
  219. /// <param name="msg"></param>
  220. private void WriteErrMsg(string msg)
  221. {
  222. if (msg != _lastErrorMsg)
  223. {
  224. _lastErrorMsg = msg;
  225. LOG.WriteLog(eEvent.ERR_FESTO, _name, msg);
  226. }
  227. }
  228. }
  229. }