MelsecHelper.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace TFT_MelsecMcNet
  6. {
  7. /// <summary>
  8. /// 所有三菱通讯类的通用辅助工具类,包含了一些通用的静态方法,可以使用本类来获取一些原始的报文信息。详细的操作参见例子
  9. /// </summary>
  10. public class MelsecHelper
  11. {
  12. #region Melsec Mc
  13. /// <summary>
  14. /// 解析A1E协议数据地址
  15. /// </summary>
  16. /// <param name="address">数据地址</param>
  17. /// <returns></returns>
  18. public static OperateResult<MelsecA1EDataType, ushort> McA1EAnalysisAddress(string address)
  19. {
  20. var result = new OperateResult<MelsecA1EDataType, ushort>();
  21. try
  22. {
  23. switch (address[0])
  24. {
  25. case 'X':
  26. case 'x':
  27. {
  28. result.Content1 = MelsecA1EDataType.X;
  29. result.Content2 = Convert.ToUInt16(address.Substring(1), MelsecA1EDataType.X.FromBase);
  30. break;
  31. }
  32. case 'Y':
  33. case 'y':
  34. {
  35. result.Content1 = MelsecA1EDataType.Y;
  36. result.Content2 = Convert.ToUInt16(address.Substring(1), MelsecA1EDataType.Y.FromBase);
  37. break;
  38. }
  39. case 'M':
  40. case 'm':
  41. {
  42. result.Content1 = MelsecA1EDataType.M;
  43. result.Content2 = Convert.ToUInt16(address.Substring(1), MelsecA1EDataType.M.FromBase);
  44. break;
  45. }
  46. case 'S':
  47. case 's':
  48. {
  49. result.Content1 = MelsecA1EDataType.S;
  50. result.Content2 = Convert.ToUInt16(address.Substring(1), MelsecA1EDataType.S.FromBase);
  51. break;
  52. }
  53. case 'D':
  54. case 'd':
  55. {
  56. result.Content1 = MelsecA1EDataType.D;
  57. result.Content2 = Convert.ToUInt16(address.Substring(1), MelsecA1EDataType.D.FromBase);
  58. break;
  59. }
  60. case 'R':
  61. case 'r':
  62. {
  63. result.Content1 = MelsecA1EDataType.R;
  64. result.Content2 = Convert.ToUInt16(address.Substring(1), MelsecA1EDataType.R.FromBase);
  65. break;
  66. }
  67. default: throw new Exception(StringResources.Language.NotSupportedDataType);
  68. }
  69. }
  70. catch (Exception ex)
  71. {
  72. result.Message = ex.Message;
  73. return result;
  74. }
  75. result.IsSuccess = true;
  76. return result;
  77. }
  78. /// <summary>
  79. /// 从三菱地址,是否位读取进行创建读取的MC的核心报文
  80. /// </summary>
  81. /// <param name="isBit">是否进行了位读取操作</param>
  82. /// <param name="addressData">三菱Mc协议的数据地址</param>
  83. /// <returns>带有成功标识的报文对象</returns>
  84. public static byte[] BuildReadMcCoreCommand(McAddressData addressData, bool isBit)
  85. {
  86. byte[] command = new byte[10];
  87. command[0] = 0x01; // 批量读取数据命令
  88. command[1] = 0x04;
  89. command[2] = isBit ? (byte)0x01 : (byte)0x00; // 以点为单位还是字为单位成批读取
  90. command[3] = 0x00;
  91. command[4] = BitConverter.GetBytes(addressData.AddressStart)[0]; // 起始地址的地位
  92. command[5] = BitConverter.GetBytes(addressData.AddressStart)[1];
  93. command[6] = BitConverter.GetBytes(addressData.AddressStart)[2];
  94. command[7] = addressData.McDataType.DataCode; // 指明读取的数据
  95. command[8] = (byte)(addressData.Length % 256); // 软元件的长度
  96. command[9] = (byte)(addressData.Length / 256);
  97. return command;
  98. }
  99. /// <summary>
  100. /// 从三菱地址,是否位读取进行创建读取Ascii格式的MC的核心报文
  101. /// </summary>
  102. /// <param name="addressData">三菱Mc协议的数据地址</param>
  103. /// <param name="isBit">是否进行了位读取操作</param>
  104. /// <returns>带有成功标识的报文对象</returns>
  105. public static byte[] BuildAsciiReadMcCoreCommand(McAddressData addressData, bool isBit)
  106. {
  107. byte[] command = new byte[20];
  108. command[0] = 0x30; // 批量读取数据命令
  109. command[1] = 0x34;
  110. command[2] = 0x30;
  111. command[3] = 0x31;
  112. command[4] = 0x30; // 以点为单位还是字为单位成批读取
  113. command[5] = 0x30;
  114. command[6] = 0x30;
  115. command[7] = isBit ? (byte)0x31 : (byte)0x30;
  116. command[8] = Encoding.ASCII.GetBytes(addressData.McDataType.AsciiCode)[0]; // 软元件类型
  117. command[9] = Encoding.ASCII.GetBytes(addressData.McDataType.AsciiCode)[1];
  118. command[10] = BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[0]; // 起始地址的地位
  119. command[11] = BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[1];
  120. command[12] = BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[2];
  121. command[13] = BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[3];
  122. command[14] = BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[4];
  123. command[15] = BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[5];
  124. command[16] = SoftBasic.BuildAsciiBytesFrom(addressData.Length)[0]; // 软元件点数
  125. command[17] = SoftBasic.BuildAsciiBytesFrom(addressData.Length)[1];
  126. command[18] = SoftBasic.BuildAsciiBytesFrom(addressData.Length)[2];
  127. command[19] = SoftBasic.BuildAsciiBytesFrom(addressData.Length)[3];
  128. return command;
  129. }
  130. /// <summary>
  131. /// 以字为单位,创建数据写入的核心报文
  132. /// </summary>
  133. /// <param name="addressData">三菱Mc协议的数据地址</param>
  134. /// <param name="value">实际的原始数据信息</param>
  135. /// <returns>带有成功标识的报文对象</returns>
  136. public static byte[] BuildWriteWordCoreCommand(McAddressData addressData, byte[] value)
  137. {
  138. if (value == null) value = new byte[0];
  139. byte[] command = new byte[10 + value.Length];
  140. command[0] = 0x01; // 批量写入数据命令
  141. command[1] = 0x14;
  142. command[2] = 0x00; // 以字为单位成批读取
  143. command[3] = 0x00;
  144. command[4] = BitConverter.GetBytes(addressData.AddressStart)[0]; // 起始地址的地位
  145. command[5] = BitConverter.GetBytes(addressData.AddressStart)[1];
  146. command[6] = BitConverter.GetBytes(addressData.AddressStart)[2];
  147. command[7] = addressData.McDataType.DataCode; // 指明写入的数据
  148. command[8] = (byte)(value.Length / 2 % 256); // 软元件长度的地位
  149. command[9] = (byte)(value.Length / 2 / 256);
  150. value.CopyTo(command, 10);
  151. return command;
  152. }
  153. /// <summary>
  154. /// 以字为单位,创建ASCII数据写入的核心报文
  155. /// </summary>
  156. /// <param name="addressData">三菱Mc协议的数据地址</param>
  157. /// <param name="value">实际的原始数据信息</param>
  158. /// <returns>带有成功标识的报文对象</returns>
  159. public static byte[] BuildAsciiWriteWordCoreCommand(McAddressData addressData, byte[] value)
  160. {
  161. if (value == null) value = new byte[0];
  162. byte[] buffer = new byte[value.Length * 2];
  163. for (int i = 0; i < value.Length / 2; i++)
  164. {
  165. SoftBasic.BuildAsciiBytesFrom(BitConverter.ToUInt16(value, i * 2)).CopyTo(buffer, 4 * i);
  166. }
  167. value = buffer;
  168. byte[] command = new byte[20 + value.Length];
  169. command[0] = 0x31; // 批量写入的命令
  170. command[1] = 0x34;
  171. command[2] = 0x30;
  172. command[3] = 0x31;
  173. command[4] = 0x30; // 子命令
  174. command[5] = 0x30;
  175. command[6] = 0x30;
  176. command[7] = 0x30;
  177. command[8] = Encoding.ASCII.GetBytes(addressData.McDataType.AsciiCode)[0]; // 软元件类型
  178. command[9] = Encoding.ASCII.GetBytes(addressData.McDataType.AsciiCode)[1];
  179. command[10] = BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[0]; // 起始地址的地位
  180. command[11] = BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[1];
  181. command[12] = BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[2];
  182. command[13] = BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[3];
  183. command[14] = BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[4];
  184. command[15] = BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[5];
  185. command[16] = SoftBasic.BuildAsciiBytesFrom((ushort)(value.Length / 4))[0]; // 软元件点数
  186. command[17] = SoftBasic.BuildAsciiBytesFrom((ushort)(value.Length / 4))[1];
  187. command[18] = SoftBasic.BuildAsciiBytesFrom((ushort)(value.Length / 4))[2];
  188. command[19] = SoftBasic.BuildAsciiBytesFrom((ushort)(value.Length / 4))[3];
  189. value.CopyTo(command, 20);
  190. return command;
  191. }
  192. /// <summary>
  193. /// 以位为单位,创建数据写入的核心报文
  194. /// </summary>
  195. /// <param name="addressData">三菱Mc协议的数据地址</param>
  196. /// <param name="value">原始的bool数组数据</param>
  197. /// <returns>带有成功标识的报文对象</returns>
  198. public static byte[] BuildWriteBitCoreCommand(McAddressData addressData, bool[] value)
  199. {
  200. if (value == null) value = new bool[0];
  201. byte[] buffer = TransBoolArrayToByteData(value);
  202. byte[] command = new byte[10 + buffer.Length];
  203. command[0] = 0x01; // 批量写入数据命令
  204. command[1] = 0x14;
  205. command[2] = 0x01; // 以位为单位成批写入
  206. command[3] = 0x00;
  207. command[4] = BitConverter.GetBytes(addressData.AddressStart)[0]; // 起始地址的地位
  208. command[5] = BitConverter.GetBytes(addressData.AddressStart)[1];
  209. command[6] = BitConverter.GetBytes(addressData.AddressStart)[2];
  210. command[7] = addressData.McDataType.DataCode; // 指明写入的数据
  211. command[8] = (byte)(value.Length % 256); // 软元件长度的地位
  212. command[9] = (byte)(value.Length / 256);
  213. buffer.CopyTo(command, 10);
  214. return command;
  215. }
  216. /// <summary>
  217. /// 以位为单位,创建ASCII数据写入的核心报文
  218. /// </summary>
  219. /// <param name="addressData">三菱Mc协议的数据地址</param>
  220. /// <param name="value">原始的bool数组数据</param>
  221. /// <returns>带有成功标识的报文对象</returns>
  222. public static byte[] BuildAsciiWriteBitCoreCommand(McAddressData addressData, bool[] value)
  223. {
  224. if (value == null) value = new bool[0];
  225. byte[] buffer = value.Select(m => m ? (byte)0x31 : (byte)0x30).ToArray();
  226. byte[] command = new byte[20 + buffer.Length];
  227. command[0] = 0x31; // 批量写入的命令
  228. command[1] = 0x34;
  229. command[2] = 0x30;
  230. command[3] = 0x31;
  231. command[4] = 0x30; // 子命令
  232. command[5] = 0x30;
  233. command[6] = 0x30;
  234. command[7] = 0x31;
  235. command[8] = Encoding.ASCII.GetBytes(addressData.McDataType.AsciiCode)[0]; // 软元件类型
  236. command[9] = Encoding.ASCII.GetBytes(addressData.McDataType.AsciiCode)[1];
  237. command[10] = BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[0]; // 起始地址的地位
  238. command[11] = BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[1];
  239. command[12] = BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[2];
  240. command[13] = BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[3];
  241. command[14] = BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[4];
  242. command[15] = BuildBytesFromAddress(addressData.AddressStart, addressData.McDataType)[5];
  243. command[16] = SoftBasic.BuildAsciiBytesFrom((ushort)(value.Length))[0]; // 软元件点数
  244. command[17] = SoftBasic.BuildAsciiBytesFrom((ushort)(value.Length))[1];
  245. command[18] = SoftBasic.BuildAsciiBytesFrom((ushort)(value.Length))[2];
  246. command[19] = SoftBasic.BuildAsciiBytesFrom((ushort)(value.Length))[3];
  247. buffer.CopyTo(command, 20);
  248. return command;
  249. }
  250. #endregion
  251. #region Common Logic
  252. /// <summary>
  253. /// 从三菱的地址中构建MC协议的6字节的ASCII格式的地址
  254. /// </summary>
  255. /// <param name="address">三菱地址</param>
  256. /// <param name="type">三菱的数据类型</param>
  257. /// <returns>6字节的ASCII格式的地址</returns>
  258. internal static byte[] BuildBytesFromAddress(int address, MelsecMcDataType type)
  259. {
  260. return Encoding.ASCII.GetBytes(address.ToString(type.FromBase == 10 ? "D6" : "X6"));
  261. }
  262. /// <summary>
  263. /// 将0,1,0,1的字节数组压缩成三菱格式的字节数组来表示开关量的
  264. /// </summary>
  265. /// <param name="value">原始的数据字节</param>
  266. /// <returns>压缩过后的数据字节</returns>
  267. internal static byte[] TransBoolArrayToByteData(byte[] value)
  268. {
  269. int length = (value.Length + 1) / 2;
  270. byte[] buffer = new byte[length];
  271. for (int i = 0; i < length; i++)
  272. {
  273. if (value[i * 2 + 0] != 0x00) buffer[i] += 0x10;
  274. if ((i * 2 + 1) < value.Length)
  275. {
  276. if (value[i * 2 + 1] != 0x00) buffer[i] += 0x01;
  277. }
  278. }
  279. return buffer;
  280. }
  281. /// <summary>
  282. /// 将bool的组压缩成三菱格式的字节数组来表示开关量的
  283. /// </summary>
  284. /// <param name="value">原始的数据字节</param>
  285. /// <returns>压缩过后的数据字节</returns>
  286. internal static byte[] TransBoolArrayToByteData(bool[] value)
  287. {
  288. int length = (value.Length + 1) / 2;
  289. byte[] buffer = new byte[length];
  290. for (int i = 0; i < length; i++)
  291. {
  292. if (value[i * 2 + 0]) buffer[i] += 0x10;
  293. if ((i * 2 + 1) < value.Length)
  294. {
  295. if (value[i * 2 + 1]) buffer[i] += 0x01;
  296. }
  297. }
  298. return buffer;
  299. }
  300. #endregion
  301. #region CRC Check
  302. /// <summary>
  303. /// 计算Fx协议指令的和校验信息
  304. /// </summary>
  305. /// <param name="data">字节数据</param>
  306. /// <returns>校验之后的数据</returns>
  307. internal static byte[] FxCalculateCRC(byte[] data)
  308. {
  309. int sum = 0;
  310. for (int i = 1; i < data.Length - 2; i++)
  311. {
  312. sum += data[i];
  313. }
  314. return SoftBasic.BuildAsciiBytesFrom((byte)sum);
  315. }
  316. /// <summary>
  317. /// 检查指定的和校验是否是正确的
  318. /// </summary>
  319. /// <param name="data">字节数据</param>
  320. /// <returns>是否成功</returns>
  321. internal static bool CheckCRC(byte[] data)
  322. {
  323. byte[] crc = FxCalculateCRC(data);
  324. if (crc[0] != data[data.Length - 2]) return false;
  325. if (crc[1] != data[^1]) return false;
  326. return true;
  327. }
  328. #endregion
  329. }
  330. }