ModBusSerial.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using Aitex.Core.RT.Event;
  2. using Aitex.Core.RT.IOCore;
  3. using Aitex.Core.RT.Log;
  4. using HslCommunication;
  5. using HslCommunication.ModBus;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Xml;
  12. namespace MECF.Framework.RT.Core.IoProviders
  13. {
  14. public class ModbusSerial : IoProvider
  15. {
  16. //private string address;
  17. private string portName;
  18. private byte station;
  19. public int diStartAddress;
  20. public int doStartAddress;
  21. public int aiStartAddress;
  22. public int aoStartAddress;
  23. public string dataformat;
  24. public bool addressstartwithzero;
  25. //public bool stringReverse;
  26. private ModbusRtu _readerClient = null;
  27. public bool IsCommunicationError = false;
  28. private bool _isConnected;
  29. private object _locker = new object();
  30. private bool _enableLog
  31. {
  32. get;set;
  33. }
  34. protected override void SetParameter(XmlElement nodeParameter)
  35. {
  36. //address = nodeParameter.GetAttribute("address");
  37. portName = nodeParameter.GetAttribute("PortName");
  38. station = (byte)Convert.ToInt32(nodeParameter.GetAttribute("station"));
  39. //stringReverse = Convert.ToBoolean(nodeParameter.GetAttribute("stringReverse"));
  40. diStartAddress = int.Parse(nodeParameter.GetAttribute("diStartAddress"));
  41. aiStartAddress = int.Parse(nodeParameter.GetAttribute("aiStartAddress"));
  42. doStartAddress = int.Parse(nodeParameter.GetAttribute("doStartAddress"));
  43. aoStartAddress = int.Parse(nodeParameter.GetAttribute("aoStartAddress"));
  44. dataformat = nodeParameter.GetAttribute("dataformat");
  45. //OP.Subscribe("System.ModbusPLC", InvokeReset);
  46. _readerClient = new ModbusRtu();
  47. _readerClient.SerialPortInni(portName);
  48. _readerClient.Open();
  49. }
  50. public int _connecteTimes { get; set; }
  51. protected override bool OnTimer()
  52. {
  53. if (!_isConnected) return true;
  54. lock (_locker)
  55. {
  56. try
  57. {
  58. foreach (var bufferSection in _blockSections)
  59. {
  60. if (bufferSection.Type == IoType.DI)
  61. {
  62. bool[] diBuffer = ReadDi(bufferSection.Offset, bufferSection.Size);
  63. if (diBuffer != null)
  64. {
  65. _buffer.SetDiBuffer(_source, bufferSection.Offset, diBuffer);
  66. //TraceArray(diBuffer);
  67. }
  68. }
  69. else if (bufferSection.Type == IoType.DO)
  70. {
  71. bool[] doBuffer = ReadDi(bufferSection.Offset, bufferSection.Size);
  72. if (doBuffer != null)
  73. {
  74. _buffer.SetDoBuffer(_source, bufferSection.Offset, doBuffer);
  75. }
  76. }
  77. else if (bufferSection.Type == IoType.AI)
  78. {
  79. short[] aiBuffer = ReadAi(bufferSection.Offset, bufferSection.Size);
  80. if (aiBuffer != null)
  81. {
  82. _buffer.SetAiBuffer(_source, bufferSection.Offset, aiBuffer);
  83. }
  84. }
  85. else if (bufferSection.Type == IoType.AO)
  86. {
  87. short[] aoBuffer = ReadAi(bufferSection.Offset, bufferSection.Size);
  88. if (aoBuffer != null)
  89. {
  90. _buffer.SetAoBuffer(_source, bufferSection.Offset, aoBuffer);
  91. }
  92. }
  93. }
  94. }
  95. catch (Exception ex)
  96. {
  97. LOG.Write(ex);
  98. Close();
  99. }
  100. }
  101. return true;
  102. }
  103. //public void Reset()
  104. //{
  105. //}
  106. protected override bool[] ReadDi(int offset, int size)
  107. {
  108. int intSize = size % 16 == 0 ? size / 16 : (size / 16 + 1);
  109. int address = offset + diStartAddress;
  110. OperateResult<short[]> ret;
  111. ret = _readerClient.ReadInt16(address.ToString(), (ushort)intSize);
  112. if (!ret.IsSuccess) return null;
  113. List<bool> retList = new List<bool>();
  114. foreach (var value in ret.Content)
  115. {
  116. for (int i = 0; i < 16; i++)
  117. {
  118. int comValue = 1 << i;
  119. retList.Add((value & comValue) == comValue);
  120. }
  121. }
  122. return retList.Take(size).ToArray();
  123. }
  124. protected override float[] ReadAiFloat(int offset, int size)
  125. {
  126. return null;
  127. }
  128. protected override short[] ReadAi(int offset, int size)
  129. {
  130. int address = offset + aiStartAddress;
  131. OperateResult<short[]> ret;
  132. ret = _readerClient.ReadInt16(address.ToString(), (ushort)size);
  133. if (!ret.IsSuccess) return null;
  134. return ret.Content;
  135. }
  136. public override bool SetValue(AOAccessor aoItem, short value)
  137. {
  138. //if (!_trigConnected.M)
  139. // return false;
  140. int address = aoItem.Index + aoStartAddress;
  141. lock (_locker)
  142. {
  143. OperateResult ret = null;
  144. if (aoItem.Name.Contains("Inclination") || aoItem.Name.Contains("Position")
  145. || aoItem.Name.Contains("SoftUpLimit") || aoItem.Name.Contains("AfterReturnZero") || aoItem.Name.Contains("MoveDistance"))
  146. ret = _readerClient.Write(address.ToString(), Convert.ToInt16(value));
  147. else
  148. ret = _readerClient.Write(address.ToString(), Convert.ToUInt16(value));
  149. if (_enableLog)
  150. LOG.Write($"MPLC Write AO address:{ address} to { value},result:{ret.IsSuccess},message:{ret.Message}");
  151. if (!ret.IsSuccess)
  152. LOG.Write($"MPLC failed to write AO address:{ address} to { value},result:{ret.IsSuccess},message:{ret.Message}");
  153. return ret.IsSuccess;
  154. }
  155. }
  156. public override bool SetValue(DOAccessor doItem, bool value)
  157. {
  158. //if (!_trigConnected.M)
  159. // return false;
  160. lock (_locker)
  161. {
  162. int address = doItem.Index / 16 + doStartAddress;
  163. var ret = _readerClient.ReadInt16(address.ToString());
  164. if (!ret.IsSuccess) return false;
  165. int valueSet = ret.Content;
  166. var val = 1 << (doItem.Index % 16);
  167. if ((ret.Content & val) == val)
  168. {
  169. if (!value)
  170. valueSet -= val;
  171. }
  172. else
  173. {
  174. if (value)
  175. valueSet += val;
  176. }
  177. var ret2 = _readerClient.Write(address.ToString(), Convert.ToUInt16(valueSet));
  178. if (_enableLog)
  179. LOG.Write($"MPLC Write AO address:{ address} to { valueSet},result:{ret.IsSuccess},message:{ret.Message}");
  180. return ret2.IsSuccess;
  181. }
  182. }
  183. protected override void Close()
  184. {
  185. try
  186. {
  187. _readerClient.Close();
  188. _readerClient.Dispose();
  189. }
  190. catch (Exception ex)
  191. {
  192. LOG.Write(ex.Message);
  193. }
  194. }
  195. protected override void Open()
  196. {
  197. _readerClient.Open();
  198. try
  199. {
  200. _isConnected = _readerClient.IsOpen();
  201. }
  202. catch (Exception ex)
  203. {
  204. IsCommunicationError = true;
  205. LOG.Write(ex.Message);
  206. }
  207. }
  208. public byte[] ReadResultRender<T>(OperateResult<T> result, string address)
  209. {
  210. byte[] buffer = new byte[32];
  211. if (result.IsSuccess)
  212. {
  213. _connecteTimes = 0;
  214. IsCommunicationError = false;
  215. var dat2 = (Convert.ToUInt32(result.Content.ToString()) & 0xFFFF);
  216. var dat1 = (Convert.ToUInt32(result.Content.ToString()) >> 16 & 0xFFFF);
  217. uint[] data = new uint[] { dat1, dat2 };
  218. for (int j = 0; j < 2; j++)
  219. {
  220. for (int i = 0; i < 16; i++)
  221. {
  222. buffer[j * 16 + i] = Convert.ToByte((data[j] >> i) & 0x01);
  223. }
  224. }
  225. string str = string.Join("", Array.ConvertAll(buffer, x => x.ToString()));
  226. if (_enableLog)
  227. LOG.Info($"Read Success,Data:{ str} ({ result.Content.ToString()}:{dat1}、{dat2})");
  228. return buffer;
  229. }
  230. else
  231. {
  232. IsCommunicationError = true;
  233. EV.PostAlarmLog("System.PLC", $"[{address}] Read Failed {Environment.NewLine}Reason:{result.ToMessageShowString()}");
  234. //MessageBox.Show(DateTime.Now.ToString("[HH:mm:ss] ") + $"[{address}] Read Failed {Environment.NewLine}Reason:{result.ToMessageShowString()}");
  235. }
  236. return buffer;
  237. }
  238. public void WriteResultRender(OperateResult result, string address, int data, string str)
  239. {
  240. if (result.IsSuccess)
  241. {
  242. _connecteTimes = 0;
  243. IsCommunicationError = false;
  244. if (_enableLog)
  245. LOG.Info("Write Success,Data" + str + "(" + data.ToString() + ")");
  246. }
  247. else
  248. {
  249. IsCommunicationError = true;
  250. EV.PostAlarmLog("System.PLC", $"[{address}] Write Failed {Environment.NewLine}Reason:{result.ToMessageShowString()}");
  251. }
  252. }
  253. public bool InvokeReset(string arg1, object[] arg2)
  254. {
  255. _connecteTimes = 0;
  256. if (!_isConnected || IsCommunicationError)
  257. Open();
  258. IsCommunicationError = false;
  259. return true;
  260. }
  261. protected override void WriteDo(int offset, bool[] buffer)
  262. {
  263. }
  264. protected override void WriteAo(int offset, short[] buffer)
  265. {
  266. }
  267. }
  268. }