SiemensPPI.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MECF.Framework.RT.Core.IoProviders.Siemens.Serial;
  6. using MECF.Framework.RT.Core.IoProviders.Siemens.Transfer;
  7. #if Net45
  8. using System.Threading.Tasks;
  9. #endif
  10. namespace MECF.Framework.RT.Core.IoProviders.Siemens
  11. {
  12. /// <summary>
  13. /// 西门子的PPI协议,适用于s7-200plc,注意,本类库有个致命的风险需要注意,由于本类库的每次通讯分成2次操作,故而不支持多线程同时读写,当发生线程竞争的时候,会导致数据异常,
  14. /// 想要解决的话,需要您在每次数据交互时添加同步锁。
  15. /// </summary>
  16. /// <remarks>
  17. /// 适用于西门子200的通信,非常感谢 合肥-加劲 的测试,让本类库圆满完成。
  18. ///
  19. /// 注意:M地址范围有限 0-31地址
  20. /// </remarks>
  21. public class SiemensPPI : SerialDeviceBase<ReverseBytesTransform>
  22. {
  23. #region Constructor
  24. /// <summary>
  25. /// 实例化一个西门子的PPI协议对象
  26. /// </summary>
  27. public SiemensPPI( )
  28. {
  29. WordLength = 2;
  30. }
  31. #endregion
  32. /// <summary>
  33. /// 西门子PLC的站号信息
  34. /// </summary>
  35. public byte Station { get => station;
  36. set {
  37. station = value;
  38. executeConfirm[1] = value;
  39. int count = 0;
  40. for (int i = 1; i < 4; i++)
  41. {
  42. count += executeConfirm[i];
  43. }
  44. executeConfirm[4] = (byte)count;
  45. }
  46. }
  47. /// <summary>
  48. /// 从西门子的PLC中读取数据信息,地址为"M100","AI100","I0","Q0","V100","S100"等,详细请参照API文档
  49. /// </summary>
  50. /// <param name="address">西门子的地址数据信息</param>
  51. /// <param name="length">数据长度</param>
  52. /// <returns>带返回结果的结果对象</returns>
  53. public override OperateResult<byte[]> Read( string address, ushort length )
  54. {
  55. // 解析指令
  56. OperateResult<byte[]> command = BuildReadCommand( station, address, length, false );
  57. if (!command.IsSuccess) return command;
  58. // 第一次数据交互
  59. OperateResult<byte[]> read1 = ReadBase( command.Content );
  60. if (!read1.IsSuccess) return read1;
  61. // 验证
  62. if (read1.Content[0] != 0xE5) return new OperateResult<byte[]>( "PLC Receive Check Failed:" + SoftBasic.ByteToHexString( read1.Content, ' ' ) );
  63. // 第二次数据交互
  64. OperateResult<byte[]> read2 = ReadBase( executeConfirm );
  65. if (!read2.IsSuccess) return read2;
  66. // 错误码判断
  67. if (read2.Content.Length < 21) return new OperateResult<byte[]>( read2.ErrorCode, "Failed: " + SoftBasic.ByteToHexString( read2.Content, ' ' ) );
  68. if (read2.Content[17] != 0x00 || read2.Content[18] != 0x00) return new OperateResult<byte[]>( read2.Content[19], GetMsgFromStatus( read2.Content[18], read2.Content[19] ) );
  69. if (read2.Content[21] != 0xFF) return new OperateResult<byte[]>( read2.Content[21], GetMsgFromStatus( read2.Content[21] ) );
  70. // 数据提取
  71. byte[] buffer = new byte[length];
  72. if (read2.Content[21] == 0xFF && read2.Content[22] == 0x04)
  73. {
  74. Array.Copy( read2.Content, 25, buffer, 0, length );
  75. }
  76. return OperateResult.CreateSuccessResult( buffer );
  77. }
  78. /// <summary>
  79. /// 从西门子的PLC中读取bool数据信息,地址为"M100.0","AI100.1","I0.3","Q0.6","V100.4","S100"等,详细请参照API文档
  80. /// </summary>
  81. /// <param name="address">西门子的地址数据信息</param>
  82. /// <param name="length">数据长度</param>
  83. /// <returns>带返回结果的结果对象</returns>
  84. public OperateResult<bool[]> ReadBool( string address, ushort length )
  85. {
  86. // 解析指令
  87. OperateResult<byte[]> command = BuildReadCommand( station, address, length, true );
  88. if (!command.IsSuccess) return OperateResult.CreateFailedResult<bool[]>( command );
  89. // 第一次数据交互
  90. OperateResult<byte[]> read1 = ReadBase( command.Content );
  91. if (!read1.IsSuccess) return OperateResult.CreateFailedResult<bool[]>( read1 );
  92. // 验证
  93. if (read1.Content[0] != 0xE5) return new OperateResult<bool[]>( "PLC Receive Check Failed:" + SoftBasic.ByteToHexString( read1.Content, ' ' ) );
  94. // 第二次数据交互
  95. OperateResult<byte[]> read2 = ReadBase( executeConfirm );
  96. if (!read2.IsSuccess) return OperateResult.CreateFailedResult<bool[]>( read2 );
  97. // 错误码判断
  98. if (read2.Content.Length < 21) return new OperateResult<bool[]>( read2.ErrorCode, "Failed: " + SoftBasic.ByteToHexString( read2.Content, ' ' ) );
  99. if (read2.Content[17] != 0x00 || read2.Content[18] != 0x00) return new OperateResult<bool[]>( read2.Content[19], GetMsgFromStatus( read2.Content[18], read2.Content[19] ) );
  100. if (read2.Content[21] != 0xFF) return new OperateResult<bool[]>( read2.Content[21], GetMsgFromStatus( read2.Content[21] ) );
  101. // 数据提取
  102. byte[] buffer = new byte[read2.Content.Length - 27];
  103. if (read2.Content[21] == 0xFF && read2.Content[22] == 0x03)
  104. {
  105. Array.Copy( read2.Content, 25, buffer, 0, buffer.Length );
  106. }
  107. return OperateResult.CreateSuccessResult( SoftBasic.ByteToBoolArray( buffer, length ) );
  108. }
  109. /// <summary>
  110. /// 从西门子的PLC中读取bool数据信息,地址为"M100.0","AI100.1","I0.3","Q0.6","V100.4","S100"等,详细请参照API文档
  111. /// </summary>
  112. /// <param name="address">西门子的地址数据信息</param>
  113. /// <returns>带返回结果的结果对象</returns>
  114. public OperateResult<bool> ReadBool( string address )
  115. {
  116. OperateResult<bool[]> read = ReadBool( address, 1 );
  117. if (!read.IsSuccess) return OperateResult.CreateFailedResult<bool>( read );
  118. return OperateResult.CreateSuccessResult( read.Content[0] );
  119. }
  120. /// <summary>
  121. /// 将字节数据写入到西门子PLC中,地址为"M100.0","AI100.1","I0.3","Q0.6","V100.4","S100"等,详细请参照API文档
  122. /// </summary>
  123. /// <param name="address">西门子的地址数据信息</param>
  124. /// <param name="value">数据长度</param>
  125. /// <returns>带返回结果的结果对象</returns>
  126. public override OperateResult Write( string address, byte[] value )
  127. {
  128. // 解析指令
  129. OperateResult<byte[]> command = BuildWriteCommand( station, address, value );
  130. if (!command.IsSuccess) return command;
  131. // 第一次数据交互
  132. OperateResult<byte[]> read1 = ReadBase( command.Content );
  133. if (!read1.IsSuccess) return read1;
  134. // 验证
  135. if (read1.Content[0] != 0xE5) return new OperateResult<byte[]>( "PLC Receive Check Failed:" + read1.Content[0] );
  136. // 第二次数据交互
  137. OperateResult<byte[]> read2 = ReadBase( executeConfirm );
  138. if (!read2.IsSuccess) return read2;
  139. // 错误码判断
  140. if (read2.Content.Length < 21) return new OperateResult( read2.ErrorCode, "Failed: " + SoftBasic.ByteToHexString( read2.Content, ' ' ) );
  141. if (read2.Content[17] != 0x00 || read2.Content[18] != 0x00) return new OperateResult( read2.Content[19], GetMsgFromStatus( read2.Content[18], read2.Content[19] ) );
  142. if (read2.Content[21] != 0xFF) return new OperateResult( read2.Content[21], GetMsgFromStatus( read2.Content[21] ) );
  143. // 数据提取
  144. return OperateResult.CreateSuccessResult( );
  145. }
  146. /// <summary>
  147. /// 将bool数据写入到西门子PLC中,地址为"M100.0","AI100.1","I0.3","Q0.6","V100.4","S100"等,详细请参照API文档
  148. /// </summary>
  149. /// <param name="address">西门子的地址数据信息</param>
  150. /// <param name="value">数据长度</param>
  151. /// <returns>带返回结果的结果对象</returns>
  152. public OperateResult Write(string address, bool[] value )
  153. {
  154. // 解析指令
  155. OperateResult<byte[]> command = BuildWriteCommand( station, address, value );
  156. if (!command.IsSuccess) return command;
  157. // 第一次数据交互
  158. OperateResult<byte[]> read1 = ReadBase( command.Content );
  159. if (!read1.IsSuccess) return read1;
  160. // 验证
  161. if (read1.Content[0] != 0xE5) return new OperateResult<byte[]>( "PLC Receive Check Failed:" + read1.Content[0] );
  162. // 第二次数据交互
  163. OperateResult<byte[]> read2 = ReadBase( executeConfirm );
  164. if (!read2.IsSuccess) return read2;
  165. // 错误码判断
  166. if (read2.Content.Length < 21) return new OperateResult( read2.ErrorCode, "Failed: " + SoftBasic.ByteToHexString( read2.Content, ' ' ) );
  167. if (read2.Content[17] != 0x00 || read2.Content[18] != 0x00) return new OperateResult( read2.Content[19], GetMsgFromStatus( read2.Content[18], read2.Content[19] ) );
  168. if (read2.Content[21] != 0xFF) return new OperateResult( read2.Content[21], GetMsgFromStatus( read2.Content[21] ) );
  169. // 数据提取
  170. return OperateResult.CreateSuccessResult( );
  171. }
  172. /// <summary>
  173. /// 将bool数据写入到西门子PLC中,地址为"M100.0","AI100.1","I0.3","Q0.6","V100.4","S100"等,详细请参照API文档
  174. /// </summary>
  175. /// <param name="address">西门子的地址数据信息</param>
  176. /// <param name="value">数据长度</param>
  177. /// <returns>带返回结果的结果对象</returns>
  178. public OperateResult Write( string address, bool value )
  179. {
  180. return Write( address, new bool[] { value } );
  181. }
  182. #region Byte Read Write
  183. /// <summary>
  184. /// 从西门子的PLC中读取byte数据信息,地址为"M100.0","AI100.1","I0.3","Q0.6","V100.4","S100"等,详细请参照API文档
  185. /// </summary>
  186. /// <param name="address">西门子的地址数据信息</param>
  187. /// <returns>带返回结果的结果对象</returns>
  188. public OperateResult<byte> ReadByte( string address )
  189. {
  190. OperateResult<byte[]> read = Read( address, 1 );
  191. if (!read.IsSuccess) return OperateResult.CreateFailedResult<byte>( read );
  192. return OperateResult.CreateSuccessResult( read.Content[0] );
  193. }
  194. /// <summary>
  195. /// 将byte数据写入到西门子PLC中,地址为"M100.0","AI100.1","I0.3","Q0.6","V100.4","S100"等,详细请参照API文档
  196. /// </summary>
  197. /// <param name="address">西门子的地址数据信息</param>
  198. /// <param name="value">数据长度</param>
  199. /// <returns>带返回结果的结果对象</returns>
  200. public OperateResult WriteByte(string address, byte value )
  201. {
  202. return Write( address, new byte[] { value } );
  203. }
  204. #endregion
  205. #region Start Stop
  206. /// <summary>
  207. /// 启动西门子PLC为RUN模式
  208. /// </summary>
  209. /// <returns>是否启动成功</returns>
  210. public OperateResult Start( )
  211. {
  212. byte[] cmd = new byte[] { 0x68, 0x21, 0x21, 0x68, station, 0x00, 0x6C, 0x32, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFD, 0x00, 0x00, 0x09, 0x50, 0x5F, 0x50, 0x52, 0x4F, 0x47, 0x52, 0x41, 0x4D, 0xAA, 0x16 };
  213. // 第一次数据交互
  214. OperateResult<byte[]> read1 = ReadBase( cmd );
  215. if (!read1.IsSuccess) return read1;
  216. // 验证
  217. if (read1.Content[0] != 0xE5) return new OperateResult<byte[]>( "PLC Receive Check Failed:" + read1.Content[0] );
  218. // 第二次数据交互
  219. OperateResult<byte[]> read2 = ReadBase( executeConfirm );
  220. if (!read2.IsSuccess) return read2;
  221. // 数据提取
  222. return OperateResult.CreateSuccessResult( );
  223. }
  224. /// <summary>
  225. /// 停止西门子PLC,切换为Stop模式
  226. /// </summary>
  227. /// <returns>是否停止成功</returns>
  228. public OperateResult Stop( )
  229. {
  230. byte[] cmd = new byte[] { 0x68, 0x1D, 0x1D, 0x68, station, 0x00, 0x6C, 0x32, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x50, 0x5F, 0x50, 0x52, 0x4F, 0x47, 0x52, 0x41, 0x4D, 0xAA, 0x16 };
  231. // 第一次数据交互
  232. OperateResult<byte[]> read1 = ReadBase( cmd );
  233. if (!read1.IsSuccess) return read1;
  234. // 验证
  235. if (read1.Content[0] != 0xE5) return new OperateResult<byte[]>( "PLC Receive Check Failed:" + read1.Content[0] );
  236. // 第二次数据交互
  237. OperateResult<byte[]> read2 = ReadBase( executeConfirm );
  238. if (!read2.IsSuccess) return read2;
  239. // 数据提取
  240. return OperateResult.CreateSuccessResult( );
  241. }
  242. #endregion
  243. #region Private Member
  244. private byte station = 0x02; // PLC的站号信息
  245. private byte[] executeConfirm = new byte[] { 0x10, 0x02, 0x00, 0x5C, 0x5E, 0x16 };
  246. #endregion
  247. #region Static Helper
  248. /// <summary>
  249. /// 解析数据地址,解析出地址类型,起始地址,DB块的地址 ->
  250. /// Parse data address, parse out address type, start address, db block address
  251. /// </summary>
  252. /// <param name="address">起始地址,例如M100,I0,Q0,V100 ->
  253. /// Start address, such as M100,I0,Q0,V100</param>
  254. /// <returns>解析数据地址,解析出地址类型,起始地址,DB块的地址 ->
  255. /// Parse data address, parse out address type, start address, db block address</returns>
  256. private static OperateResult<byte, int, ushort> AnalysisAddress( string address )
  257. {
  258. var result = new OperateResult<byte, int, ushort>( );
  259. try
  260. {
  261. result.Content3 = 0;
  262. if(address.Substring(0,2) == "AI")
  263. {
  264. result.Content1 = 0x06;
  265. result.Content2 = SiemensS7Net.CalculateAddressStarted( address.Substring( 2 ) );
  266. }
  267. else if (address.Substring( 0, 2 ) == "AQ")
  268. {
  269. result.Content1 = 0x07;
  270. result.Content2 = SiemensS7Net.CalculateAddressStarted( address.Substring( 2 ) );
  271. }
  272. else if (address[0] == 'T')
  273. {
  274. result.Content1 = 0x1F;
  275. result.Content2 = SiemensS7Net.CalculateAddressStarted( address.Substring( 1 ) );
  276. }
  277. else if (address[0] == 'C')
  278. {
  279. result.Content1 = 0x1E;
  280. result.Content2 = SiemensS7Net.CalculateAddressStarted( address.Substring( 1 ) );
  281. }
  282. else if (address.Substring( 0, 2 ) == "SM")
  283. {
  284. result.Content1 = 0x05;
  285. result.Content2 = SiemensS7Net.CalculateAddressStarted( address.Substring( 2 ) );
  286. }
  287. else if (address[0] == 'S')
  288. {
  289. result.Content1 = 0x04;
  290. result.Content2 = SiemensS7Net.CalculateAddressStarted( address.Substring( 1 ) );
  291. }
  292. else if (address[0] == 'I')
  293. {
  294. result.Content1 = 0x81;
  295. result.Content2 = SiemensS7Net.CalculateAddressStarted( address.Substring( 1 ) );
  296. }
  297. else if (address[0] == 'Q')
  298. {
  299. result.Content1 = 0x82;
  300. result.Content2 = SiemensS7Net.CalculateAddressStarted( address.Substring( 1 ) );
  301. }
  302. else if (address[0] == 'M')
  303. {
  304. result.Content1 = 0x83;
  305. result.Content2 = SiemensS7Net.CalculateAddressStarted( address.Substring( 1 ) );
  306. }
  307. else if (address[0] == 'D' || address.Substring( 0, 2 ) == "DB")
  308. {
  309. result.Content1 = 0x84;
  310. string[] adds = address.Split( '.' );
  311. if (address[1] == 'B')
  312. {
  313. result.Content3 = Convert.ToUInt16( adds[0].Substring( 2 ) );
  314. }
  315. else
  316. {
  317. result.Content3 = Convert.ToUInt16( adds[0].Substring( 1 ) );
  318. }
  319. result.Content2 = SiemensS7Net.CalculateAddressStarted( address.Substring( address.IndexOf( '.' ) + 1 ) );
  320. }
  321. else if (address[0] == 'V')
  322. {
  323. result.Content1 = 0x84;
  324. result.Content3 = 1;
  325. result.Content2 = SiemensS7Net.CalculateAddressStarted( address.Substring( 1 ) );
  326. }
  327. else
  328. {
  329. result.Message = StringResources.Language.NotSupportedDataType;
  330. result.Content1 = 0;
  331. result.Content2 = 0;
  332. result.Content3 = 0;
  333. return result;
  334. }
  335. }
  336. catch (Exception ex)
  337. {
  338. result.Message = ex.Message;
  339. return result;
  340. }
  341. result.IsSuccess = true;
  342. return result;
  343. }
  344. /// <summary>
  345. /// 生成一个读取字数据指令头的通用方法 ->
  346. /// A general method for generating a command header to read a Word data
  347. /// </summary>
  348. /// <param name="station">设备的站号信息 -> Station number information for the device</param>
  349. /// <param name="address">起始地址,例如M100,I0,Q0,V100 ->
  350. /// Start address, such as M100,I0,Q0,V100</param>
  351. /// <param name="length">读取数据长度 -> Read Data length</param>
  352. /// <param name="isBit">是否为位读取</param>
  353. /// <returns>包含结果对象的报文 -> Message containing the result object</returns>
  354. public static OperateResult<byte[]> BuildReadCommand( byte station, string address, ushort length, bool isBit )
  355. {
  356. OperateResult<byte, int, ushort> analysis = AnalysisAddress( address );
  357. if (!analysis.IsSuccess) return OperateResult.CreateFailedResult<byte[]>( analysis );
  358. byte[] _PLCCommand = new byte[33];
  359. _PLCCommand[ 0] = 0x68;
  360. _PLCCommand[ 1] = BitConverter.GetBytes( _PLCCommand.Length - 6 )[0];
  361. _PLCCommand[ 2] = BitConverter.GetBytes( _PLCCommand.Length - 6 )[0];
  362. _PLCCommand[ 3] = 0x68;
  363. _PLCCommand[ 4] = station;
  364. _PLCCommand[ 5] = 0x00;
  365. _PLCCommand[ 6] = 0x6C;
  366. _PLCCommand[ 7] = 0x32;
  367. _PLCCommand[ 8] = 0x01;
  368. _PLCCommand[ 9] = 0x00;
  369. _PLCCommand[10] = 0x00;
  370. _PLCCommand[11] = 0x00;
  371. _PLCCommand[12] = 0x00;
  372. _PLCCommand[13] = 0x00;
  373. _PLCCommand[14] = 0x0E;
  374. _PLCCommand[15] = 0x00;
  375. _PLCCommand[16] = 0x00;
  376. _PLCCommand[17] = 0x04;
  377. _PLCCommand[18] = 0x01;
  378. _PLCCommand[19] = 0x12;
  379. _PLCCommand[20] = 0x0A;
  380. _PLCCommand[21] = 0x10;
  381. _PLCCommand[22] = isBit ? (byte)0x01 : (byte)0x02;
  382. _PLCCommand[23] = 0x00;
  383. _PLCCommand[24] = BitConverter.GetBytes( length )[0];
  384. _PLCCommand[25] = BitConverter.GetBytes( length )[1];
  385. _PLCCommand[26] = (byte)analysis.Content3;
  386. _PLCCommand[27] = analysis.Content1;
  387. _PLCCommand[28] = BitConverter.GetBytes( analysis.Content2 )[2];
  388. _PLCCommand[29] = BitConverter.GetBytes( analysis.Content2 )[1];
  389. _PLCCommand[30] = BitConverter.GetBytes( analysis.Content2 )[0];
  390. int count = 0;
  391. for(int i = 4; i< 31; i++)
  392. {
  393. count += _PLCCommand[i];
  394. }
  395. _PLCCommand[31] = BitConverter.GetBytes( count )[0];
  396. _PLCCommand[32] = 0x16;
  397. return OperateResult.CreateSuccessResult( _PLCCommand );
  398. }
  399. /// <summary>
  400. /// 生成一个写入PLC数据信息的报文内容
  401. /// </summary>
  402. /// <param name="station">PLC的站号</param>
  403. /// <param name="address">地址</param>
  404. /// <param name="values">数据值</param>
  405. /// <returns>是否写入成功</returns>
  406. public static OperateResult<byte[]> BuildWriteCommand( byte station, string address, byte[] values )
  407. {
  408. OperateResult<byte, int, ushort> analysis = AnalysisAddress( address );
  409. if (!analysis.IsSuccess) return OperateResult.CreateFailedResult<byte[]>( analysis );
  410. int length = values.Length;
  411. // 68 21 21 68 02 00 6C 32 01 00 00 00 00 00 0E 00 00 04 01 12 0A 10
  412. byte[] _PLCCommand = new byte[37 + values.Length];
  413. _PLCCommand[ 0] = 0x68;
  414. _PLCCommand[ 1] = BitConverter.GetBytes( _PLCCommand.Length - 6 )[0];
  415. _PLCCommand[ 2] = BitConverter.GetBytes( _PLCCommand.Length - 6 )[0];
  416. _PLCCommand[ 3] = 0x68;
  417. _PLCCommand[ 4] = station;
  418. _PLCCommand[ 5] = 0x00;
  419. _PLCCommand[ 6] = 0x7C;
  420. _PLCCommand[ 7] = 0x32;
  421. _PLCCommand[ 8] = 0x01;
  422. _PLCCommand[ 9] = 0x00;
  423. _PLCCommand[10] = 0x00;
  424. _PLCCommand[11] = 0x00;
  425. _PLCCommand[12] = 0x00;
  426. _PLCCommand[13] = 0x00;
  427. _PLCCommand[14] = 0x0E;
  428. _PLCCommand[15] = 0x00;
  429. _PLCCommand[16] = (byte)(values.Length + 4);
  430. _PLCCommand[17] = 0x05;
  431. _PLCCommand[18] = 0x01;
  432. _PLCCommand[19] = 0x12;
  433. _PLCCommand[20] = 0x0A;
  434. _PLCCommand[21] = 0x10;
  435. _PLCCommand[22] = 0x02;
  436. _PLCCommand[23] = 0x00;
  437. _PLCCommand[24] = BitConverter.GetBytes( length )[0];
  438. _PLCCommand[25] = BitConverter.GetBytes( length )[1];
  439. _PLCCommand[26] = (byte)analysis.Content3;
  440. _PLCCommand[27] = analysis.Content1;
  441. _PLCCommand[28] = BitConverter.GetBytes( analysis.Content2 )[2];
  442. _PLCCommand[29] = BitConverter.GetBytes( analysis.Content2 )[1];
  443. _PLCCommand[30] = BitConverter.GetBytes( analysis.Content2 )[0];
  444. _PLCCommand[31] = 0x00;
  445. _PLCCommand[32] = 0x04;
  446. _PLCCommand[33] = BitConverter.GetBytes( length * 8 )[1];
  447. _PLCCommand[34] = BitConverter.GetBytes( length * 8 )[0];
  448. values.CopyTo( _PLCCommand, 35 );
  449. int count = 0;
  450. for (int i = 4; i < _PLCCommand.Length - 2; i++)
  451. {
  452. count += _PLCCommand[i];
  453. }
  454. _PLCCommand[_PLCCommand.Length - 2] = BitConverter.GetBytes( count )[0];
  455. _PLCCommand[_PLCCommand.Length - 1] = 0x16;
  456. return OperateResult.CreateSuccessResult( _PLCCommand );
  457. }
  458. /// <summary>
  459. /// 根据错误信息,获取到文本信息
  460. /// </summary>
  461. /// <param name="code">状态</param>
  462. /// <returns>消息文本</returns>
  463. public static string GetMsgFromStatus( byte code )
  464. {
  465. switch (code)
  466. {
  467. case 0xFF: return "No error";
  468. case 0x01: return "Hardware fault";
  469. case 0x03: return "Illegal object access";
  470. case 0x05: return "Invalid address(incorrent variable address)";
  471. case 0x06: return "Data type is not supported";
  472. case 0x0A: return "Object does not exist or length error";
  473. default: return StringResources.Language.UnknownError;
  474. }
  475. }
  476. /// <summary>
  477. /// 根据错误信息,获取到文本信息
  478. /// </summary>
  479. /// <param name="errorClass">错误类型</param>
  480. /// <param name="errorCode">错误代码</param>
  481. /// <returns>错误信息</returns>
  482. public static string GetMsgFromStatus( byte errorClass, byte errorCode )
  483. {
  484. if(errorClass == 0x80 && errorCode == 0x01)
  485. {
  486. return "Switch in wrong position for requested operation";
  487. }
  488. else if (errorClass == 0x81 && errorCode == 0x04)
  489. {
  490. return "Miscellaneous structure error in command.  Command is not supportedby CPU";
  491. }
  492. else if (errorClass == 0x84 && errorCode == 0x04)
  493. {
  494. return "CPU is busy processing an upload or download CPU cannot process command because of system fault condition";
  495. }
  496. else if (errorClass == 0x85 && errorCode == 0x00)
  497. {
  498. return "Length fields are not correct or do not agree with the amount of data received";
  499. }
  500. else if (errorClass == 0xD2 )
  501. {
  502. return "Error in upload or download command";
  503. }
  504. else if (errorClass == 0xD6)
  505. {
  506. return "Protection error(password)";
  507. }
  508. else if (errorClass == 0xDC && errorCode == 0x01)
  509. {
  510. return "Error in time-of-day clock data";
  511. }
  512. else
  513. {
  514. return StringResources.Language.UnknownError;
  515. }
  516. }
  517. /// <summary>
  518. /// 创建写入PLC的bool类型数据报文指令
  519. /// </summary>
  520. /// <param name="station">PLC的站号信息</param>
  521. /// <param name="address">地址信息</param>
  522. /// <param name="values">bool[]数据值</param>
  523. /// <returns>带有成功标识的结果对象</returns>
  524. public static OperateResult<byte[]> BuildWriteCommand( byte station, string address, bool[] values )
  525. {
  526. OperateResult<byte, int, ushort> analysis = AnalysisAddress( address );
  527. if (!analysis.IsSuccess) return OperateResult.CreateFailedResult<byte[]>( analysis );
  528. byte[] bytesValue = SoftBasic.BoolArrayToByte( values );
  529. // 68 21 21 68 02 00 6C 32 01 00 00 00 00 00 0E 00 00 04 01 12 0A 10
  530. byte[] _PLCCommand = new byte[37 + bytesValue.Length];
  531. _PLCCommand[ 0] = 0x68;
  532. _PLCCommand[ 1] = BitConverter.GetBytes( _PLCCommand.Length - 6 )[0];
  533. _PLCCommand[ 2] = BitConverter.GetBytes( _PLCCommand.Length - 6 )[0];
  534. _PLCCommand[ 3] = 0x68;
  535. _PLCCommand[ 4] = station;
  536. _PLCCommand[ 5] = 0x00;
  537. _PLCCommand[ 6] = 0x7C;
  538. _PLCCommand[ 7] = 0x32;
  539. _PLCCommand[ 8] = 0x01;
  540. _PLCCommand[ 9] = 0x00;
  541. _PLCCommand[10] = 0x00;
  542. _PLCCommand[11] = 0x00;
  543. _PLCCommand[12] = 0x00;
  544. _PLCCommand[13] = 0x00;
  545. _PLCCommand[14] = 0x0E;
  546. _PLCCommand[15] = 0x00;
  547. _PLCCommand[16] = 0x05;
  548. _PLCCommand[17] = 0x05;
  549. _PLCCommand[18] = 0x01;
  550. _PLCCommand[19] = 0x12;
  551. _PLCCommand[20] = 0x0A;
  552. _PLCCommand[21] = 0x10;
  553. _PLCCommand[22] = 0x01;
  554. _PLCCommand[23] = 0x00;
  555. _PLCCommand[24] = BitConverter.GetBytes( values.Length )[0];
  556. _PLCCommand[25] = BitConverter.GetBytes( values.Length )[1];
  557. _PLCCommand[26] = (byte)analysis.Content3;
  558. _PLCCommand[27] = analysis.Content1;
  559. _PLCCommand[28] = BitConverter.GetBytes( analysis.Content2 )[2];
  560. _PLCCommand[29] = BitConverter.GetBytes( analysis.Content2 )[1];
  561. _PLCCommand[30] = BitConverter.GetBytes( analysis.Content2 )[0];
  562. _PLCCommand[31] = 0x00;
  563. _PLCCommand[32] = 0x03;
  564. _PLCCommand[33] = BitConverter.GetBytes( values.Length )[1];
  565. _PLCCommand[34] = BitConverter.GetBytes( values.Length )[0];
  566. bytesValue.CopyTo( _PLCCommand, 35 );
  567. int count = 0;
  568. for (int i = 4; i < _PLCCommand.Length - 2; i++)
  569. {
  570. count += _PLCCommand[i];
  571. }
  572. _PLCCommand[_PLCCommand.Length - 2] = BitConverter.GetBytes( count )[0];
  573. _PLCCommand[_PLCCommand.Length - 1] = 0x16;
  574. return OperateResult.CreateSuccessResult( _PLCCommand );
  575. }
  576. #endregion
  577. }
  578. }