MelsecMCBin.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace MECF.Framework.RT.Core.IoProviders.Mitsubishis
  8. {
  9. public class MelsecMCBin
  10. {
  11. public MelsecMCBin()
  12. {
  13. m_nNetNo = 0x00;//本站
  14. m_nPLCNo = 0xFF;
  15. m_nIONo = 0xFF03;
  16. m_nStationNo = 0x00;
  17. m_nTimeOut = 0x1000;
  18. m_bytelist = new List<byte>();
  19. m_nDataLen = 12;//不包括软元件数据
  20. }
  21. /// <summary>
  22. /// 检查起始地址和读取长度
  23. /// </summary>
  24. /// <returns></returns>
  25. private bool check()
  26. {
  27. if (m_nElementDataLen < 0 || m_nElementDataLen > m_melsecElement.m_nLen)
  28. {
  29. return false;
  30. }
  31. if (m_nElementStartAddr < m_melsecElement.m_nStartAddr || m_nElementStartAddr > m_melsecElement.m_nEndAddr)
  32. {
  33. return false;
  34. }
  35. return true;
  36. }
  37. /*************读协议内容******************************
  38. * 副标题(2)50 00|网络编号(1)00|PLC编号(1)FF
  39. * IO编号(2)FF 03|站编号(1)00|请求数据长度(2)_12
  40. * 应答超时(2)1000|命令(2)_|子命令(2)_|起始地址(3)_
  41. * 请求软元件代码(1)|请求点数长度(2)
  42. * ***************************************************/
  43. /// <summary>
  44. /// 构建读取字节
  45. /// </summary>
  46. public byte[] getReadBytes()
  47. {
  48. m_bytelist.Clear();
  49. if (check())
  50. {
  51. m_enumSubTitle = EnumsubTitle.Request;
  52. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_enumSubTitle, 2));
  53. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nNetNo, 1));
  54. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nPLCNo, 1));
  55. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nIONo, 2));
  56. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nStationNo, 1));
  57. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nDataLen, 2, false));
  58. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nTimeOut, 2));
  59. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_enumCmd, 2, false));
  60. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_enumSubCmd, 2, false));
  61. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nElementStartAddr, 3, false));
  62. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_melsecElement.m_nBinCode, 1));
  63. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nElementDataLen, 2, false));
  64. }
  65. return m_bytelist.ToArray();
  66. }
  67. /****************写协议内容**********************************
  68. * 副标题(2)50 00|网络编号(1)00|PLC编号(1)FF
  69. * IO编号(2)FF 03|站编号(1)00|请求数据长度(2)_12+写入数据长度
  70. * 应答超时(2)1000|命令(2)_|子命令(2)_|起始地址(3)_
  71. * 请求软元件代码(1)|请求点数长度(2)|写入数据(分按位和按字)
  72. * ***********************************************************/
  73. /// <summary>
  74. /// 构建写字节
  75. /// </summary>
  76. /// <param name="nLen">写入数据长度</param>
  77. public byte[] getWriteBytes(int nLen)
  78. {
  79. m_bytelist.Clear();
  80. if (check())
  81. {
  82. m_enumSubTitle = EnumsubTitle.Request;
  83. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_enumSubTitle, 2));
  84. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nNetNo, 1));
  85. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nPLCNo, 1));
  86. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nIONo, 2));
  87. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nStationNo, 1));
  88. m_nDataLen += nLen;
  89. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nDataLen, 2, false));
  90. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nTimeOut, 2));
  91. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_enumCmd, 2, false));
  92. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_enumSubCmd, 2, false));
  93. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nElementStartAddr, 3, false));
  94. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_melsecElement.m_nBinCode, 1));
  95. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nElementDataLen, 2, false));
  96. m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nElementData, nLen));
  97. }
  98. return m_bytelist.ToArray();
  99. }
  100. //MelsecMCBinSend: 500000FFFF03000E001000011400000000009D0100000F 按字写
  101. //MelsecMCBinRece: D00000FFFF030002000000
  102. /****************读应答正常协议内容**********************************
  103. * 副标题(2)D0 00|网络编号(1)00|PLC编号(1)FF
  104. * IO编号(2)FF 03|站编号(1)00|应答数据长度(2)_
  105. * 结束代码(2)00 00|应答数据部分
  106. * ***********************************************************/
  107. public byte[] getReadRespond(byte[] byteRespond)
  108. {
  109. if (byteRespond.Length > 11)
  110. {
  111. int nLen = StringHelper.ConverterToInt(byteRespond[7], byteRespond[8]) - 2;
  112. if (nLen == byteRespond.Length - 11)
  113. {
  114. for (int n = 0; n < nLen; n++)
  115. {
  116. byteRespond[n] = byteRespond[byteRespond.Length - n - 1];
  117. }
  118. Array.Resize(ref byteRespond, nLen);
  119. }
  120. return byteRespond;
  121. }
  122. return null;
  123. }
  124. /****************写应答正常协议内容**********************************
  125. * 副标题(2)D0 00|网络编号(1)00|PLC编号(1)FF
  126. * IO编号(2)FF 03|站编号(1)00|应答数据长度(2)02 00
  127. * 结束代码(2)00 00
  128. * D0 00 |00 |FF |FF 03| 00| 02 00 |00 00
  129. * ***********************************************************/
  130. public bool getWriteRespond(byte[] byteRespond)
  131. {
  132. string strRespond = StringHelper.GetHexString(byteRespond, byteRespond.Length);
  133. string strTemp = "D00000FFFF030002000000";
  134. return string.Equals(strTemp, strRespond);
  135. }
  136. /****************应答异常协议内容**********************************
  137. * 副标题(2)D0 00|网络编号(1)00|PLC编号(1)FF
  138. * IO编号(2)FF 03|站编号(1)00|应答数据长度(2) 0B 00
  139. * 结束代码(2)51 C0
  140. *
  141. * 网络编号(1)00|PLC编号(1)FF
  142. * IO编号(2)FF 03|站编号(1)00|命令(2)|子命令(2)
  143. * ***********************************************************/
  144. public int getRespondCode(byte[] byteRespond)
  145. {
  146. if (byteRespond.Length > 10)
  147. {
  148. return StringHelper.ConverterToInt(byteRespond[9], byteRespond[10]);
  149. }
  150. return -1;
  151. }
  152. /*副标题*/
  153. public EnumsubTitle m_enumSubTitle { get; set; }
  154. /*网络编号*/
  155. public int m_nNetNo { get; set; }
  156. /*PLC编号*/
  157. public int m_nPLCNo { get; set; }
  158. /*IO编号*/
  159. public int m_nIONo { get; set; }
  160. /*站编号*/
  161. public int m_nStationNo { get; set; }
  162. /*数据长度*/
  163. public int m_nDataLen { get; set; }
  164. /*CPU监视定时器,命令输出到接收应答文件时间*/
  165. public int m_nTimeOut { get; set; }
  166. /*命令*/
  167. public EnumCmd m_enumCmd { get; set; }
  168. /*子命令*/
  169. public EnumSubCmd m_enumSubCmd { get; set; }
  170. /*软元件*/
  171. public MelsecElement m_melsecElement { get; set; }
  172. /*起始软元件地址*/
  173. public int m_nElementStartAddr { get; set; }
  174. /*软元件数据长度*/
  175. public int m_nElementDataLen { get; set; }
  176. /*软元件数据*/
  177. public int m_nElementData { get; set; }
  178. /*结束代码*/
  179. public EnumEndCode m_nEndCode { get; set; }
  180. /*临时存放字节数组*/
  181. private List<byte> m_bytelist;
  182. public enum EnumsubTitle
  183. {
  184. Request = 0x5000,//请求
  185. Respond = 0xD000,//应答
  186. }
  187. public enum EnumEndCode
  188. {
  189. Ok = 0x0000,//正常应答
  190. Err = 0x51C0,//异常应答
  191. }
  192. public enum EnumCmd
  193. {
  194. ReadBatch = 0x0401,//成批读
  195. WriteBatch = 0x1401,//成批写
  196. }
  197. /// <summary>
  198. /// 返回byte数组
  199. /// </summary>
  200. public enum EnumSubCmd
  201. {
  202. /*有存储扩展模块b7=0,b6=0:随机读出,监视数据注册用外*/
  203. Bit = 0x0001,//按位读写
  204. Word = 0x0000,//按字读写
  205. /*有存储扩展模块b7=1,b6=0:随机读出,监视数据注册用外*/
  206. BitEx = 0x0081,//按位读写
  207. WordEx = 0x0080,//按字读写
  208. }
  209. }
  210. public static class StringHelper
  211. {
  212. public static byte[] ConverterToBytes(object value, int length,bool reverse=true)
  213. {
  214. byte[] src = new byte[length];
  215. for (int i = 0; i < length; i++)
  216. {
  217. src[i] = (byte)(((byte)value >> (i*8)) & 0xFF);
  218. }
  219. return src;
  220. }
  221. public static int ConverterToInt(byte value1, byte value2)
  222. {
  223. int result = value1 << 8 + value2;
  224. return result;
  225. }
  226. public static string GetHexString(byte[] byteRespond, int length)
  227. {
  228. StringBuilder result = new StringBuilder();
  229. foreach (byte bytes in byteRespond)
  230. {
  231. result.AppendFormat("{0:x2}", bytes);
  232. }
  233. return result.ToString();
  234. }
  235. }
  236. }