FestoModbusDevice.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. _channel = channel;
  73. _periodicJob = new PeriodicJob(200, OnTimer, $"festo {name} timer", false,true);
  74. }
  75. /// <summary>
  76. /// 定时器执行
  77. /// </summary>
  78. /// <returns></returns>
  79. private bool OnTimer()
  80. {
  81. if(Monitor.TryEnter(_locker, _lockerTime))
  82. {
  83. ApplyAllDatas();
  84. Monitor.Exit(_locker);
  85. }
  86. return true;
  87. }
  88. /// <summary>
  89. /// 更新地址数量
  90. /// </summary>
  91. /// <param name="addressCount"></param>
  92. public void InitializeAddressCount(ushort addressCount)
  93. {
  94. _addressCount = addressCount;
  95. NetResult result = Connect();
  96. if (result.IsSuccess)
  97. {
  98. ApplyAllDatas();
  99. LOG.WriteLog(eEvent.INFO_FESTO, _name, $"connect {_ip}:{_port} success");
  100. }
  101. else
  102. {
  103. LOG.WriteLog(eEvent.INFO_FESTO, _name, $"connect {_ip}:{_port} failed");
  104. }
  105. _periodicJob.Start();
  106. }
  107. /// <summary>
  108. /// 设置Festo数值
  109. /// </summary>
  110. /// <param name="address"></param>
  111. /// <param name="value"></param>
  112. /// <returns></returns>
  113. public bool SetFestoValue(ushort address,byte value)
  114. {
  115. FestoCommand command = new FestoCommand();
  116. command.Channel = _channel;
  117. command.Address=address;
  118. command.CommandCode = 0x06;
  119. command.Datas = new byte[1] { value };
  120. if (Monitor.TryEnter(_locker, _lockerTime))
  121. {
  122. bool result= SetOperation(command);
  123. Monitor.Exit(_locker);
  124. return result;
  125. }
  126. else
  127. {
  128. WriteErrMsg($"Write apply locker over {_lockerTime}");
  129. return false;
  130. }
  131. }
  132. /// <summary>
  133. /// 设置操作
  134. /// </summary>
  135. /// <param name="command"></param>
  136. /// <returns></returns>
  137. private bool SetOperation(FestoCommand command)
  138. {
  139. if (Connected)
  140. {
  141. NetResult netResult = SetData(command);
  142. if (!netResult.IsSuccess)
  143. {
  144. WriteErrMsg($"write {command.Address.ToString("X2")} value {command.Datas[0]} failed,{netResult.Message}");
  145. return false;
  146. }
  147. else
  148. {
  149. LOG.WriteLog(eEvent.INFO_FESTO, _name, $"write {command.Address.ToString("X2")} value {command.Datas[0]} success");
  150. }
  151. return true;
  152. }
  153. else
  154. {
  155. NetResult netResult = Connect();
  156. if (netResult.IsSuccess)
  157. {
  158. netResult = SetData(command);
  159. if (!netResult.IsSuccess)
  160. {
  161. WriteErrMsg($"write {command.Address.ToString("X2")} value {command.Datas[0]} failed,{netResult.Message}");
  162. return false;
  163. }
  164. return true;
  165. }
  166. else
  167. {
  168. WriteErrMsg("connect failed");
  169. return false;
  170. }
  171. }
  172. }
  173. /// <summary>
  174. /// 申请所有数据
  175. /// </summary>
  176. public void ApplyAllDatas()
  177. {
  178. FestoCommand command = new FestoCommand();
  179. command.Address = _diStartAddress;
  180. command.RegisterCount = (ushort)(_addressCount*2);
  181. command.CommandCode = 0x03;
  182. command.Channel = _channel;
  183. ApplyDataOperation(command);
  184. }
  185. /// <summary>
  186. /// 申请数据操作
  187. /// </summary>
  188. /// <param name="command"></param>
  189. private void ApplyDataOperation(FestoCommand command)
  190. {
  191. if (!Connected)
  192. {
  193. NetResult connectResult = Connect();
  194. if (!connectResult.IsSuccess)
  195. {
  196. WriteErrMsg("connect failed");
  197. return;
  198. }
  199. }
  200. NetResult<FestoCommand> netResult = ApplyData(command);
  201. if (!netResult.IsSuccess)
  202. {
  203. WriteErrMsg($"apply {command.Address.ToString("X2")} failed,{netResult.Message}");
  204. return;
  205. }
  206. if (netResult.Data.Datas != null)
  207. {
  208. //bit0--高8位,bit1-低8位,bit2-下一地址高8位,bit3--下一地址低8位,只取bit1
  209. byte[] bytes = new byte[_addressCount];
  210. for(int i = 0; i < netResult.Data.Datas.Length; i += 4)
  211. {
  212. if (i + 1 < netResult.Data.Datas.Length&&i/4<_addressCount)
  213. {
  214. byte data = netResult.Data.Datas[i + 1];
  215. bytes[i / 4] = data;
  216. }
  217. }
  218. FestoControllerCfgManager.Instance.UpdateFestoData(_name, bytes);
  219. }
  220. }
  221. /// <summary>
  222. /// 写错误日志
  223. /// </summary>
  224. /// <param name="msg"></param>
  225. private void WriteErrMsg(string msg)
  226. {
  227. if (msg != _lastErrorMsg)
  228. {
  229. _lastErrorMsg = msg;
  230. LOG.WriteLog(eEvent.ERR_FESTO, _name, msg);
  231. }
  232. }
  233. }
  234. }