SiemensFetchWriteNet.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Sockets;
  6. using MECF.Framework.RT.Core.IoProviders.Common.IMessage;
  7. using MECF.Framework.RT.Core.IoProviders.Common.Net.NetworkBase;
  8. using MECF.Framework.RT.Core.IoProviders.Common.Transfer;
  9. using MECF.Framework.RT.Core.IoProviders.Common;
  10. /********************************************************************************
  11. *
  12. * 说明:西门子通讯类,使用Fetch/Write消息解析规格,和反字节转换规格来实现的
  13. *
  14. * 继承自统一的自定义方法,需要在PLC端进行相关的数据配置
  15. *
  16. *
  17. *********************************************************************************/
  18. namespace MECF.Framework.RT.Core.IoProviders.Siemens
  19. {
  20. /// <summary>
  21. /// 使用了Fetch/Write协议来和西门子进行通讯,该种方法需要在PLC侧进行一些配置 ->
  22. /// Using the Fetch/write protocol to communicate with Siemens, this method requires some configuration on the PLC side
  23. /// </summary>
  24. /// <remarks>
  25. /// 与S7协议相比较而言,本协议不支持对单个的点位的读写操作。如果读取M100.0,需要读取M100的值,然后进行提取位数据
  26. /// 地址支持的列表如下:
  27. /// <list type="table">
  28. /// <listheader>
  29. /// <term>地址名称</term>
  30. /// <term>地址代号</term>
  31. /// <term>示例</term>
  32. /// <term>地址进制</term>
  33. /// <term>字操作</term>
  34. /// <term>位操作</term>
  35. /// <term>备注</term>
  36. /// </listheader>
  37. /// <item>
  38. /// <term>中间寄存器</term>
  39. /// <term>M</term>
  40. /// <term>M100,M200</term>
  41. /// <term>10</term>
  42. /// <term>√</term>
  43. /// <term>×</term>
  44. /// <term></term>
  45. /// </item>
  46. /// <item>
  47. /// <term>输入寄存器</term>
  48. /// <term>I</term>
  49. /// <term>I100,I200</term>
  50. /// <term>10</term>
  51. /// <term>√</term>
  52. /// <term>×</term>
  53. /// <term></term>
  54. /// </item>
  55. /// <item>
  56. /// <term>输出寄存器</term>
  57. /// <term>Q</term>
  58. /// <term>Q100,Q200</term>
  59. /// <term>10</term>
  60. /// <term>√</term>
  61. /// <term>×</term>
  62. /// <term></term>
  63. /// </item>
  64. /// <item>
  65. /// <term>DB块寄存器</term>
  66. /// <term>DB</term>
  67. /// <term>DB1.100,DB1.200</term>
  68. /// <term>10</term>
  69. /// <term>√</term>
  70. /// <term>×</term>
  71. /// <term></term>
  72. /// </item>
  73. /// <item>
  74. /// <term>定时器的值</term>
  75. /// <term>T</term>
  76. /// <term>T100,T200</term>
  77. /// <term>10</term>
  78. /// <term>√</term>
  79. /// <term>×</term>
  80. /// <term></term>
  81. /// </item>
  82. /// <item>
  83. /// <term>计数器的值</term>
  84. /// <term>C</term>
  85. /// <term>C100,C200</term>
  86. /// <term>10</term>
  87. /// <term>√</term>
  88. /// <term>×</term>
  89. /// <term></term>
  90. /// </item>
  91. /// </list>
  92. /// </remarks>
  93. /// <example>
  94. /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\SiemensFetchWriteNet.cs" region="Usage" title="简单的短连接使用" />
  95. /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\SiemensFetchWriteNet.cs" region="Usage2" title="简单的长连接使用" />
  96. /// </example>
  97. public class SiemensFetchWriteNet : NetworkDeviceBase<FetchWriteMessage, ReverseBytesTransform>
  98. {
  99. #region Constructor
  100. /// <summary>
  101. /// 实例化一个西门子的Fetch/Write协议的通讯对象 ->
  102. /// Instantiate a communication object for a Siemens Fetch/write protocol
  103. /// </summary>
  104. public SiemensFetchWriteNet()
  105. {
  106. WordLength = 2;
  107. }
  108. /// <summary>
  109. /// 实例化一个西门子的Fetch/Write协议的通讯对象 ->
  110. /// Instantiate a communication object for a Siemens Fetch/write protocol
  111. /// </summary>
  112. /// <param name="ipAddress">PLC的Ip地址 -> Specify IP Address</param>
  113. /// <param name="port">PLC的端口 -> Specify IP Port</param>
  114. public SiemensFetchWriteNet(string ipAddress,int port)
  115. {
  116. WordLength = 2;
  117. IpAddress = ipAddress;
  118. Port = port;
  119. }
  120. #endregion
  121. #region Read Support
  122. /// <summary>
  123. /// 从PLC读取数据,地址格式为I100,Q100,DB20.100,M100,T100,C100,以字节为单位 ->
  124. /// Read data from PLC, address format I100,Q100,DB20.100,M100,T100,C100, in bytes
  125. /// </summary>
  126. /// <param name="address">起始地址,格式为I100,M100,Q100,DB20.100,T100,C100 ->
  127. /// Starting address, formatted as I100,M100,Q100,DB20.100,T100,C100
  128. /// </param>
  129. /// <param name="length">读取的数量,以字节为单位 -> The number of reads, in bytes</param>
  130. /// <returns>带有成功标志的字节信息 -> Byte information with a success flag</returns>
  131. /// <example>
  132. /// 假设起始地址为M100,M100存储了温度,100.6℃值为1006,M102存储了压力,1.23Mpa值为123,M104,M105,M106,M107存储了产量计数,读取如下:
  133. /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\SiemensFetchWriteNet.cs" region="ReadExample2" title="Read示例" />
  134. /// 以下是读取不同类型数据的示例
  135. /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\SiemensFetchWriteNet.cs" region="ReadExample1" title="Read示例" />
  136. /// </example>
  137. public override OperateResult<byte[]> Read( string address, ushort length )
  138. {
  139. // 指令解析 -> Instruction parsing
  140. OperateResult<byte[]> command = BuildReadCommand( address, length );
  141. if (!command.IsSuccess) return command;
  142. // 核心交互 -> Core Interactions
  143. OperateResult<byte[]> read = ReadFromCoreServer( command.Content );
  144. if (!read.IsSuccess) return read;
  145. // 错误码验证 -> Error code Verification
  146. if (read.Content[8] != 0x00) return new OperateResult<byte[]>( read.Content[8], StringResources.Language.SiemensFWError );
  147. // 读取正确 -> Read Right
  148. byte[] buffer = new byte[read.Content.Length - 16];
  149. Array.Copy( read.Content, 16, buffer, 0, buffer.Length );
  150. return OperateResult.CreateSuccessResult( buffer );
  151. }
  152. /// <summary>
  153. /// 读取指定地址的byte数据 -> Reads the byte data for the specified address
  154. /// </summary>
  155. /// <param name="address">起始地址,格式为M100,I100,Q100,DB1.100 -> Starting address, formatted as M100,I100,Q100,DB1.100</param>
  156. /// <returns>byte类型的结果对象 -> Result object of type Byte</returns>
  157. /// <remarks>
  158. /// <note type="warning">
  159. /// 不适用于DB块,定时器,计数器的数据读取,会提示相应的错误,读取长度必须为偶数
  160. /// </note>
  161. /// </remarks>
  162. public OperateResult<byte> ReadByte(string address)
  163. {
  164. return ByteTransformHelper.GetResultFromArray( Read( address, 1 ) );
  165. }
  166. #endregion
  167. #region Write Base
  168. /// <summary>
  169. /// 将数据写入到PLC数据,地址格式为I100,Q100,DB20.100,M100,以字节为单位 ->
  170. /// Writes data to the PLC data, in the address format i100,q100,db20.100,m100, in bytes
  171. /// </summary>
  172. /// <param name="address">起始地址,格式为M100,I100,Q100,DB1.100 -> Starting address, formatted as M100,I100,Q100,DB1.100</param>
  173. /// <param name="value">要写入的实际数据 -> The actual data to write</param>
  174. /// <returns>是否写入成功的结果对象 -> Whether to write a successful result object</returns>
  175. /// <example>
  176. /// 假设起始地址为M100,M100,M101存储了温度,100.6℃值为1006,M102,M103存储了压力,1.23Mpa值为123,M104-M107存储了产量计数,写入如下:
  177. /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\SiemensFetchWriteNet.cs" region="WriteExample2" title="Write示例" />
  178. /// 以下是写入不同类型数据的示例
  179. /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\SiemensFetchWriteNet.cs" region="WriteExample1" title="Write示例" />
  180. /// </example>
  181. public override OperateResult Write( string address, byte[] value )
  182. {
  183. // 指令解析 -> Instruction parsing
  184. OperateResult<byte[]> command = BuildWriteCommand( address, value );
  185. if (!command.IsSuccess) return command;
  186. // 核心交互 -> Core Interactions
  187. OperateResult<byte[]> write = ReadFromCoreServer( command.Content );
  188. if (!write.IsSuccess) return write;
  189. // 错误码验证 -> Error code Verification
  190. if (write.Content[8] != 0x00) return new OperateResult( write.Content[8], StringResources.Language.SiemensWriteError + write.Content[8] );
  191. // 写入成功 -> Write Right
  192. return OperateResult.CreateSuccessResult( );
  193. }
  194. #endregion
  195. #region Write bool[]
  196. /// <summary>
  197. /// 向PLC中写入bool数组,返回值说明,比如你写入M100,那么data[0]对应M100.0 ->
  198. /// Write the bool array to the PLC, return the value description, for example, if you write M100, then data[0] corresponds to M100.0
  199. /// </summary>
  200. /// <param name="address">起始地址,格式为M100,I100,Q100,DB1.100 -> Starting address, formatted as M100,I100,Q100,DB1.100</param>
  201. /// <param name="values">要写入的实际数据,长度为8的倍数 -> The actual data to write, a multiple of 8 in length</param>
  202. /// <returns>是否写入成功的结果对象 -> Whether to write a successful result object</returns>
  203. /// <remarks>
  204. /// <note type="warning">
  205. /// 批量写入bool数组存在一定的风险,原因是只能批量写入长度为8的倍数的数组,否则会影响其他的位的数据,请谨慎使用。 ->
  206. /// There is a risk in bulk writing to a bool array, because it is possible to write arrays of multiples of length 8 in bulk, otherwise it will affect the data of other bits, please use sparingly.
  207. /// </note>
  208. /// </remarks>
  209. public OperateResult Write(string address, bool[] values)
  210. {
  211. return Write( address, SoftBasic.BoolArrayToByte( values ) );
  212. }
  213. #endregion
  214. #region Write Byte
  215. /// <summary>
  216. /// 向PLC中写入byte数据,返回是否写入成功 -> Writes byte data to the PLC and returns whether the write succeeded
  217. /// </summary>
  218. /// <param name="address">起始地址,格式为M100,I100,Q100,DB1.100 -> Starting address, formatted as M100,I100,Q100,DB1.100</param>
  219. /// <param name="value">要写入的实际数据 -> The actual data to write</param>
  220. /// <returns>是否写入成功的结果对象 -> Whether to write a successful result object</returns>
  221. public OperateResult Write(string address, byte value)
  222. {
  223. return Write( address, new byte[] { value } );
  224. }
  225. #endregion
  226. #region Object Override
  227. /// <summary>
  228. /// 返回表示当前对象的字符串 -> Returns a String representing the current object
  229. /// </summary>
  230. /// <returns>字符串数据 -> String data</returns>
  231. public override string ToString()
  232. {
  233. return $"SiemensFetchWriteNet[{IpAddress}:{Port}]";
  234. }
  235. #endregion
  236. #region Static Method Helper
  237. /// <summary>
  238. /// 计算特殊的地址信息
  239. /// </summary>
  240. /// <param name="address">字符串信息</param>
  241. /// <returns>实际值</returns>
  242. private static int CalculateAddressStarted( string address )
  243. {
  244. if (address.IndexOf( '.' ) < 0)
  245. {
  246. return Convert.ToInt32( address );
  247. }
  248. else
  249. {
  250. string[] temp = address.Split( '.' );
  251. return Convert.ToInt32( temp[0] );
  252. }
  253. }
  254. /// <summary>
  255. /// 解析数据地址,解析出地址类型,起始地址,DB块的地址 -> Parse data address, parse out address type, start address, db block address
  256. /// </summary>
  257. /// <param name="address">起始地址,格式为M100,I100,Q100,DB1.100 -> Starting address, formatted as M100,I100,Q100,DB1.100</param>
  258. /// <returns>解析出地址类型,起始地址,DB块的地址 -> Resolves address type, start address, db block address</returns>
  259. private static OperateResult<byte, int, ushort> AnalysisAddress( string address )
  260. {
  261. var result = new OperateResult<byte, int, ushort>( );
  262. try
  263. {
  264. result.Content3 = 0;
  265. if (address[0] == 'I')
  266. {
  267. result.Content1 = 0x03;
  268. result.Content2 = CalculateAddressStarted( address.Substring( 1 ) );
  269. }
  270. else if (address[0] == 'Q')
  271. {
  272. result.Content1 = 0x04;
  273. result.Content2 = CalculateAddressStarted( address.Substring( 1 ) );
  274. }
  275. else if (address[0] == 'M')
  276. {
  277. result.Content1 = 0x02;
  278. result.Content2 = CalculateAddressStarted( address.Substring( 1 ) );
  279. }
  280. else if (address[0] == 'D' || address.Substring( 0, 2 ) == "DB")
  281. {
  282. result.Content1 = 0x01;
  283. string[] adds = address.Split( '.' );
  284. if (address[1] == 'B')
  285. {
  286. result.Content3 = Convert.ToUInt16( adds[0].Substring( 2 ) );
  287. }
  288. else
  289. {
  290. result.Content3 = Convert.ToUInt16( adds[0].Substring( 1 ) );
  291. }
  292. if (result.Content3 > 255)
  293. {
  294. result.Message = StringResources.Language.SiemensDBAddressNotAllowedLargerThan255;
  295. return result;
  296. }
  297. result.Content2 = CalculateAddressStarted( address.Substring( address.IndexOf( '.' ) + 1 ) );
  298. }
  299. else if (address[0] == 'T')
  300. {
  301. result.Content1 = 0x07;
  302. result.Content2 = CalculateAddressStarted( address.Substring( 1 ) );
  303. }
  304. else if (address[0] == 'C')
  305. {
  306. result.Content1 = 0x06;
  307. result.Content2 = CalculateAddressStarted( address.Substring( 1 ) );
  308. }
  309. else
  310. {
  311. result.Message = StringResources.Language.NotSupportedDataType;
  312. result.Content1 = 0;
  313. result.Content2 = 0;
  314. result.Content3 = 0;
  315. return result;
  316. }
  317. }
  318. catch (Exception ex)
  319. {
  320. result.Message = ex.Message;
  321. return result;
  322. }
  323. result.IsSuccess = true;
  324. return result;
  325. }
  326. #endregion
  327. #region Build Command
  328. /// <summary>
  329. /// 生成一个读取字数据指令头的通用方法 -> A general method for generating a command header to read a Word data
  330. /// </summary>
  331. /// <param name="address">起始地址,格式为M100,I100,Q100,DB1.100 -> Starting address, formatted as M100,I100,Q100,DB1.100</param>
  332. /// <param name="count">读取数据个数 -> Number of Read data</param>
  333. /// <returns>带结果对象的报文数据 -> Message data with a result object</returns>
  334. public static OperateResult<byte[]> BuildReadCommand( string address, ushort count )
  335. {
  336. OperateResult<byte, int, ushort> analysis = AnalysisAddress( address );
  337. if (!analysis.IsSuccess) return OperateResult.CreateFailedResult<byte[]>( analysis );
  338. byte[] _PLCCommand = new byte[16];
  339. _PLCCommand[0] = 0x53;
  340. _PLCCommand[1] = 0x35;
  341. _PLCCommand[2] = 0x10;
  342. _PLCCommand[3] = 0x01;
  343. _PLCCommand[4] = 0x03;
  344. _PLCCommand[5] = 0x05;
  345. _PLCCommand[6] = 0x03;
  346. _PLCCommand[7] = 0x08;
  347. // 指定数据区 -> Specify Data area
  348. _PLCCommand[8] = analysis.Content1;
  349. _PLCCommand[9] = (byte)analysis.Content3;
  350. // 指定数据地址 -> Specify Data address
  351. _PLCCommand[10] = (byte)(analysis.Content2 / 256);
  352. _PLCCommand[11] = (byte)(analysis.Content2 % 256);
  353. // DB块,定时器,计数器读取长度按照字为单位,1代表2个字节,I,Q,M的1代表1个字节 ->
  354. // DB block, timer, counter read length per word, 1 for 2 bytes, i,q,m 1 for 1 bytes
  355. if (analysis.Content1 == 0x01 || analysis.Content1 == 0x06 || analysis.Content1 == 0x07)
  356. {
  357. if (count % 2 != 0)
  358. {
  359. return new OperateResult<byte[]>( StringResources.Language.SiemensReadLengthMustBeEvenNumber );
  360. }
  361. else
  362. {
  363. // 指定数据长度 -> Specify data length
  364. _PLCCommand[12] = (byte)(count / 2 / 256);
  365. _PLCCommand[13] = (byte)(count / 2 % 256);
  366. }
  367. }
  368. else
  369. {
  370. // 指定数据长度 -> Specify data length
  371. _PLCCommand[12] = (byte)(count / 256);
  372. _PLCCommand[13] = (byte)(count % 256);
  373. }
  374. _PLCCommand[14] = 0xff;
  375. _PLCCommand[15] = 0x02;
  376. return OperateResult.CreateSuccessResult( _PLCCommand );
  377. }
  378. /// <summary>
  379. /// 生成一个写入字节数据的指令 -> Generate an instruction to write byte data
  380. /// </summary>
  381. /// <param name="address">起始地址,格式为M100,I100,Q100,DB1.100 -> Starting address, formatted as M100,I100,Q100,DB1.100</param>
  382. /// <param name="data">实际的写入的内容 -> The actual content of the write</param>
  383. /// <returns>带结果对象的报文数据 -> Message data with a result object</returns>
  384. public static OperateResult<byte[]> BuildWriteCommand( string address, byte[] data )
  385. {
  386. if (data == null) data = new byte[0];
  387. OperateResult<byte, int, ushort> analysis = AnalysisAddress( address );
  388. if (!analysis.IsSuccess) return OperateResult.CreateFailedResult<byte[]>( analysis );
  389. byte[] _PLCCommand = new byte[16 + data.Length];
  390. _PLCCommand[0] = 0x53;
  391. _PLCCommand[1] = 0x35;
  392. _PLCCommand[2] = 0x10;
  393. _PLCCommand[3] = 0x01;
  394. _PLCCommand[4] = 0x03;
  395. _PLCCommand[5] = 0x03;
  396. _PLCCommand[6] = 0x03;
  397. _PLCCommand[7] = 0x08;
  398. // 指定数据区 -> Specify Data area
  399. _PLCCommand[8] = analysis.Content1;
  400. _PLCCommand[9] = (byte)analysis.Content3;
  401. // 指定数据地址 -> Specify Data address
  402. _PLCCommand[10] = (byte)(analysis.Content2 / 256);
  403. _PLCCommand[11] = (byte)(analysis.Content2 % 256);
  404. if (analysis.Content1 == 0x01 || analysis.Content1 == 0x06 || analysis.Content1 == 0x07)
  405. {
  406. if (data.Length % 2 != 0)
  407. {
  408. return new OperateResult<byte[]>( StringResources.Language.SiemensReadLengthMustBeEvenNumber );
  409. }
  410. else
  411. {
  412. // 指定数据长度 -> Specify data length
  413. _PLCCommand[12] = (byte)(data.Length / 2 / 256);
  414. _PLCCommand[13] = (byte)(data.Length / 2 % 256);
  415. }
  416. }
  417. else
  418. {
  419. // 指定数据长度 -> Specify data length
  420. _PLCCommand[12] = (byte)(data.Length / 256);
  421. _PLCCommand[13] = (byte)(data.Length % 256);
  422. }
  423. _PLCCommand[14] = 0xff;
  424. _PLCCommand[15] = 0x02;
  425. // 放置数据 -> Placing data
  426. Array.Copy( data, 0, _PLCCommand, 16, data.Length );
  427. return OperateResult.CreateSuccessResult( _PLCCommand );
  428. }
  429. #endregion
  430. }
  431. }