OmronFinsNetHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MECF.Framework.RT.Core.IoProviders.Common;
  6. using MECF.Framework.RT.Core.IoProviders.Common.Transfer;
  7. namespace MECF.Framework.RT.Core.IoProviders.Omron
  8. {
  9. /// <summary>
  10. /// Omron PLC的FINS协议相关的辅助类
  11. /// </summary>
  12. public class OmronFinsNetHelper
  13. {
  14. #region Static Method Helper
  15. /// <summary>
  16. /// 解析数据地址,Omron手册第188页
  17. /// </summary>
  18. /// <param name="address">数据地址</param>
  19. /// <param name="isBit">是否是位地址</param>
  20. /// <returns>解析后的结果地址对象</returns>
  21. public static OperateResult<OmronFinsDataType, byte[]> AnalysisAddress( string address, bool isBit )
  22. {
  23. var result = new OperateResult<OmronFinsDataType, byte[]>( );
  24. try
  25. {
  26. switch (address[0])
  27. {
  28. case 'D':
  29. case 'd':
  30. {
  31. // DM区数据
  32. result.Content1 = OmronFinsDataType.DM;
  33. break;
  34. }
  35. case 'C':
  36. case 'c':
  37. {
  38. // CIO区数据
  39. result.Content1 = OmronFinsDataType.CIO;
  40. break;
  41. }
  42. case 'W':
  43. case 'w':
  44. {
  45. // WR区
  46. result.Content1 = OmronFinsDataType.WR;
  47. break;
  48. }
  49. case 'H':
  50. case 'h':
  51. {
  52. // HR区
  53. result.Content1 = OmronFinsDataType.HR;
  54. break;
  55. }
  56. case 'A':
  57. case 'a':
  58. {
  59. // AR区
  60. result.Content1 = OmronFinsDataType.AR;
  61. break;
  62. }
  63. default: throw new Exception( StringResources.Language.NotSupportedDataType );
  64. }
  65. if (isBit)
  66. {
  67. // 位操作
  68. string[] splits = address.Substring( 1 ).Split( new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries );
  69. ushort addr = ushort.Parse( splits[0] );
  70. result.Content2 = new byte[3];
  71. result.Content2[0] = BitConverter.GetBytes( addr )[1];
  72. result.Content2[1] = BitConverter.GetBytes( addr )[0];
  73. if (splits.Length > 1)
  74. {
  75. result.Content2[2] = byte.Parse( splits[1] );
  76. if (result.Content2[2] > 15)
  77. {
  78. throw new Exception( StringResources.Language.OmronAddressMustBeZeroToFiveteen );
  79. }
  80. }
  81. }
  82. else
  83. {
  84. // 字操作
  85. ushort addr = ushort.Parse( address.Substring( 1 ) );
  86. result.Content2 = new byte[3];
  87. result.Content2[0] = BitConverter.GetBytes( addr )[1];
  88. result.Content2[1] = BitConverter.GetBytes( addr )[0];
  89. }
  90. }
  91. catch (Exception ex)
  92. {
  93. result.Message = ex.Message;
  94. return result;
  95. }
  96. result.IsSuccess = true;
  97. return result;
  98. }
  99. /// <summary>
  100. /// 根据读取的地址,长度,是否位读取创建Fins协议的核心报文
  101. /// </summary>
  102. /// <param name="address">地址,具体格式请参照示例说明</param>
  103. /// <param name="length">读取的数据长度</param>
  104. /// <param name="isBit">是否使用位读取</param>
  105. /// <returns>带有成功标识的Fins核心报文</returns>
  106. public static OperateResult<byte[]> BuildReadCommand( string address, ushort length, bool isBit )
  107. {
  108. var analysis = OmronFinsNetHelper.AnalysisAddress( address, isBit );
  109. if (!analysis.IsSuccess) return OperateResult.CreateFailedResult<byte[]>( analysis );
  110. byte[] _PLCCommand = new byte[8];
  111. _PLCCommand[0] = 0x01; // 读取存储区数据
  112. _PLCCommand[1] = 0x01;
  113. if (isBit)
  114. {
  115. _PLCCommand[2] = analysis.Content1.BitCode;
  116. }
  117. else
  118. {
  119. _PLCCommand[2] = analysis.Content1.WordCode;
  120. }
  121. analysis.Content2.CopyTo( _PLCCommand, 3 );
  122. _PLCCommand[6] = (byte)(length / 256); // 长度
  123. _PLCCommand[7] = (byte)(length % 256);
  124. return OperateResult.CreateSuccessResult( _PLCCommand );
  125. }
  126. /// <summary>
  127. /// 根据写入的地址,数据,是否位写入生成Fins协议的核心报文
  128. /// </summary>
  129. /// <param name="address">地址内容,具体格式请参照示例说明</param>
  130. /// <param name="value">实际的数据</param>
  131. /// <param name="isBit">是否位数据</param>
  132. /// <returns>带有成功标识的Fins核心报文</returns>
  133. public static OperateResult<byte[]> BuildWriteWordCommand( string address, byte[] value, bool isBit )
  134. {
  135. var analysis = OmronFinsNetHelper.AnalysisAddress( address, isBit );
  136. if (!analysis.IsSuccess) return OperateResult.CreateFailedResult<byte[]>( analysis );
  137. byte[] _PLCCommand = new byte[8 + value.Length];
  138. _PLCCommand[0] = 0x01;
  139. _PLCCommand[1] = 0x02;
  140. if (isBit)
  141. {
  142. _PLCCommand[2] = analysis.Content1.BitCode;
  143. }
  144. else
  145. {
  146. _PLCCommand[2] = analysis.Content1.WordCode;
  147. }
  148. analysis.Content2.CopyTo( _PLCCommand, 3 );
  149. if (isBit)
  150. {
  151. _PLCCommand[6] = (byte)(value.Length / 256);
  152. _PLCCommand[7] = (byte)(value.Length % 256);
  153. }
  154. else
  155. {
  156. _PLCCommand[6] = (byte)(value.Length / 2 / 256);
  157. _PLCCommand[7] = (byte)(value.Length / 2 % 256);
  158. }
  159. value.CopyTo( _PLCCommand, 8 );
  160. return OperateResult.CreateSuccessResult( _PLCCommand );
  161. }
  162. /// <summary>
  163. /// 验证欧姆龙的Fins-TCP返回的数据是否正确的数据,如果正确的话,并返回所有的数据内容
  164. /// </summary>
  165. /// <param name="response">来自欧姆龙返回的数据内容</param>
  166. /// <param name="isRead">是否读取</param>
  167. /// <returns>带有是否成功的结果对象</returns>
  168. public static OperateResult<byte[]> ResponseValidAnalysis( byte[] response, bool isRead )
  169. {
  170. if (response.Length >= 16)
  171. {
  172. // 提取错误码 -> Extracting error Codes
  173. byte[] buffer = new byte[4];
  174. buffer[0] = response[15];
  175. buffer[1] = response[14];
  176. buffer[2] = response[13];
  177. buffer[3] = response[12];
  178. int err = BitConverter.ToInt32( buffer, 0 );
  179. if (err > 0) return new OperateResult<byte[]>( err, GetStatusDescription( err ) );
  180. byte[] result = new byte[response.Length - 16];
  181. Array.Copy( response, 16, result, 0, result.Length );
  182. return UdpResponseValidAnalysis( result, isRead );
  183. //if (response.Length >= 30)
  184. //{
  185. // err = response[28] * 256 + response[29];
  186. // // if (err > 0) return new OperateResult<byte[]>( err, StringResources.Language.OmronReceiveDataError );
  187. // if (!isRead)
  188. // {
  189. // OperateResult<byte[]> success = OperateResult.CreateSuccessResult( new byte[0] );
  190. // success.ErrorCode = err;
  191. // success.Message = GetStatusDescription( err );
  192. // return success;
  193. // }
  194. // else
  195. // {
  196. // // 读取操作 -> read operate
  197. // byte[] content = new byte[response.Length - 30];
  198. // if (content.Length > 0) Array.Copy( response, 30, content, 0, content.Length );
  199. // OperateResult<byte[]> success = OperateResult.CreateSuccessResult( content );
  200. // if (content.Length == 0) success.IsSuccess = false;
  201. // success.ErrorCode = err;
  202. // success.Message = GetStatusDescription( err );
  203. // return success;
  204. // }
  205. //}
  206. }
  207. return new OperateResult<byte[]>( StringResources.Language.OmronReceiveDataError );
  208. }
  209. /// <summary>
  210. /// 验证欧姆龙的Fins-Udp返回的数据是否正确的数据,如果正确的话,并返回所有的数据内容
  211. /// </summary>
  212. /// <param name="response">来自欧姆龙返回的数据内容</param>
  213. /// <param name="isRead">是否读取</param>
  214. /// <returns>带有是否成功的结果对象</returns>
  215. public static OperateResult<byte[]> UdpResponseValidAnalysis( byte[] response, bool isRead )
  216. {
  217. if (response.Length >= 14)
  218. {
  219. int err = response[12] * 256 + response[13];
  220. // if (err > 0) return new OperateResult<byte[]>( err, StringResources.Language.OmronReceiveDataError );
  221. if (!isRead)
  222. {
  223. OperateResult<byte[]> success = OperateResult.CreateSuccessResult( new byte[0] );
  224. success.ErrorCode = err;
  225. success.Message = GetStatusDescription( err ) + " Received:" + SoftBasic.ByteToHexString( response, ' ' );
  226. return success;
  227. }
  228. else
  229. {
  230. // 读取操作 -> read operate
  231. byte[] content = new byte[response.Length - 14];
  232. if (content.Length > 0) Array.Copy( response, 14, content, 0, content.Length );
  233. OperateResult<byte[]> success = OperateResult.CreateSuccessResult( content );
  234. if (content.Length == 0) success.IsSuccess = false;
  235. success.ErrorCode = err;
  236. success.Message = GetStatusDescription( err ) + " Received:" + SoftBasic.ByteToHexString( response, ' ' );
  237. return success;
  238. }
  239. }
  240. return new OperateResult<byte[]>( StringResources.Language.OmronReceiveDataError );
  241. }
  242. /// <summary>
  243. /// 获取错误信息的字符串描述文本
  244. /// </summary>
  245. /// <param name="err">错误码</param>
  246. /// <returns>文本描述</returns>
  247. public static string GetStatusDescription( int err )
  248. {
  249. switch (err)
  250. {
  251. case 0: return StringResources.Language.OmronStatus0;
  252. case 1: return StringResources.Language.OmronStatus1;
  253. case 2: return StringResources.Language.OmronStatus2;
  254. case 3: return StringResources.Language.OmronStatus3;
  255. case 20: return StringResources.Language.OmronStatus20;
  256. case 21: return StringResources.Language.OmronStatus21;
  257. case 22: return StringResources.Language.OmronStatus22;
  258. case 23: return StringResources.Language.OmronStatus23;
  259. case 24: return StringResources.Language.OmronStatus24;
  260. case 25: return StringResources.Language.OmronStatus25;
  261. default: return StringResources.Language.UnknownError;
  262. }
  263. }
  264. #endregion
  265. }
  266. }