OmronFinsUdp.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using MECF.Framework.RT.Core.IoProviders.Common;
  7. using MECF.Framework.RT.Core.IoProviders.Common.Net.NetworkBase;
  8. using MECF.Framework.RT.Core.IoProviders.Common.Transfer;
  9. namespace MECF.Framework.RT.Core.IoProviders.Omron
  10. {
  11. /// <summary>
  12. /// 欧姆龙的Udp的数据对象
  13. /// </summary>
  14. /// <remarks>
  15. /// <note type="warning">如果在测试的时候报错误码64,是因为PLC中产生了报警,如伺服报警,模块错误等产生的,但是数据还是能正常读到的,屏蔽64报警或清除plc错误可解决</note>
  16. /// 地址支持的列表如下:
  17. /// <list type="table">
  18. /// <listheader>
  19. /// <term>地址名称</term>
  20. /// <term>地址代号</term>
  21. /// <term>示例</term>
  22. /// <term>地址进制</term>
  23. /// <term>字操作</term>
  24. /// <term>位操作</term>
  25. /// <term>备注</term>
  26. /// </listheader>
  27. /// <item>
  28. /// <term>DM Area</term>
  29. /// <term>D</term>
  30. /// <term>D100,D200</term>
  31. /// <term>10</term>
  32. /// <term>√</term>
  33. /// <term>√</term>
  34. /// <term></term>
  35. /// </item>
  36. /// <item>
  37. /// <term>CIO Area</term>
  38. /// <term>C</term>
  39. /// <term>C100,C200</term>
  40. /// <term>10</term>
  41. /// <term>√</term>
  42. /// <term>√</term>
  43. /// <term></term>
  44. /// </item>
  45. /// <item>
  46. /// <term>Work Area</term>
  47. /// <term>W</term>
  48. /// <term>W100,W200</term>
  49. /// <term>10</term>
  50. /// <term>√</term>
  51. /// <term>√</term>
  52. /// <term></term>
  53. /// </item>
  54. /// <item>
  55. /// <term>Holding Bit Area</term>
  56. /// <term>H</term>
  57. /// <term>H100,H200</term>
  58. /// <term>10</term>
  59. /// <term>√</term>
  60. /// <term>√</term>
  61. /// <term></term>
  62. /// </item>
  63. /// <item>
  64. /// <term>Auxiliary Bit Area</term>
  65. /// <term>A</term>
  66. /// <term>A100,A200</term>
  67. /// <term>10</term>
  68. /// <term>√</term>
  69. /// <term>√</term>
  70. /// <term></term>
  71. /// </item>
  72. /// </list>
  73. /// </remarks>
  74. public class OmronFinsUdp : NetworkUdpDeviceBase<ReverseWordTransform>
  75. {
  76. #region Constructor
  77. /// <summary>
  78. /// 实例化一个默认的欧姆龙Udp的对象
  79. /// </summary>
  80. /// <param name="ipAddress">Ip地址</param>
  81. /// <param name="port">端口号</param>
  82. public OmronFinsUdp(string ipAddress, int port )
  83. {
  84. WordLength = 1;
  85. IpAddress = ipAddress;
  86. Port = port;
  87. ByteTransform.DataFormat = DataFormat.CDAB;
  88. }
  89. /// <summary>
  90. /// 实例化一个默认的欧姆龙Udp的对象
  91. /// </summary>
  92. public OmronFinsUdp( )
  93. {
  94. WordLength = 1;
  95. ByteTransform.DataFormat = DataFormat.CDAB;
  96. }
  97. #endregion
  98. #region IpAddress Override
  99. /// <summary>
  100. /// 设备的Ip地址信息
  101. /// </summary>
  102. public override string IpAddress {
  103. get => base.IpAddress;
  104. set
  105. {
  106. DA1 = Convert.ToByte( value.Substring( value.LastIndexOf( "." ) + 1 ) );
  107. base.IpAddress = value;
  108. }
  109. }
  110. #endregion
  111. #region Public Member
  112. /// <summary>
  113. /// 信息控制字段,默认0x80
  114. /// </summary>
  115. public byte ICF { get; set; } = 0x80;
  116. /// <summary>
  117. /// 系统使用的内部信息
  118. /// </summary>
  119. public byte RSV { get; private set; } = 0x00;
  120. /// <summary>
  121. /// 网络层信息,默认0x02,如果有八层消息,就设置为0x07
  122. /// </summary>
  123. public byte GCT { get; set; } = 0x02;
  124. /// <summary>
  125. /// PLC的网络号地址,默认0x00
  126. /// </summary>
  127. public byte DNA { get; set; } = 0x00;
  128. /// <summary>
  129. /// PLC的节点地址,这个值在配置了ip地址之后是默认赋值的,默认为Ip地址的最后一位
  130. /// </summary>
  131. /// <remarks>
  132. /// <note type="important">假如你的PLC的Ip地址为192.168.0.10,那么这个值就是10</note>
  133. /// </remarks>
  134. public byte DA1 { get; set; } = 0x13;
  135. /// <summary>
  136. /// PLC的单元号地址
  137. /// </summary>
  138. /// <remarks>
  139. /// <note type="important">通常都为0</note>
  140. /// </remarks>
  141. public byte DA2 { get; set; } = 0x00;
  142. /// <summary>
  143. /// 上位机的网络号地址
  144. /// </summary>
  145. public byte SNA { get; set; } = 0x00;
  146. /// <summary>
  147. /// 上位机的节点地址,假如你的电脑的Ip地址为192.168.0.13,那么这个值就是13
  148. /// </summary>
  149. /// <remarks>
  150. /// <note type="important">假如你的电脑的Ip地址为192.168.0.13,那么这个值就是13</note>
  151. /// </remarks>
  152. public byte SA1 { get; set; } = 13;
  153. /// <summary>
  154. /// 上位机的单元号地址
  155. /// </summary>
  156. public byte SA2 { get; set; }
  157. /// <summary>
  158. /// 设备的标识号
  159. /// </summary>
  160. public byte SID { get; set; } = 0x00;
  161. #endregion
  162. #region Build Command
  163. /// <summary>
  164. /// 将普通的指令打包成完整的指令
  165. /// </summary>
  166. /// <param name="cmd"></param>
  167. /// <returns></returns>
  168. private byte[] PackCommand( byte[] cmd )
  169. {
  170. byte[] buffer = new byte[10 + cmd.Length];
  171. buffer[0] = ICF;
  172. buffer[1] = RSV;
  173. buffer[2] = GCT;
  174. buffer[3] = DNA;
  175. buffer[4] = DA1;
  176. buffer[5] = DA2;
  177. buffer[6] = SNA;
  178. buffer[7] = SA1;
  179. buffer[8] = SA2;
  180. buffer[9] = SID;
  181. cmd.CopyTo( buffer, 10 );
  182. return buffer;
  183. }
  184. /// <summary>
  185. /// 根据类型地址长度确认需要读取的指令头
  186. /// </summary>
  187. /// <param name="address">起始地址</param>
  188. /// <param name="length">长度</param>
  189. /// <param name="isBit">是否是位读取</param>
  190. /// <returns>带有成功标志的报文数据</returns>
  191. public OperateResult<byte[]> BuildReadCommand( string address, ushort length, bool isBit )
  192. {
  193. var command = OmronFinsNetHelper.BuildReadCommand( address, length, isBit );
  194. if (!command.IsSuccess) return command;
  195. return OperateResult.CreateSuccessResult( PackCommand( command.Content ) );
  196. }
  197. /// <summary>
  198. /// 根据类型地址以及需要写入的数据来生成指令头
  199. /// </summary>
  200. /// <param name="address">起始地址</param>
  201. /// <param name="value">真实的数据值信息</param>
  202. /// <param name="isBit">是否是位操作</param>
  203. /// <returns>带有成功标志的报文数据</returns>
  204. public OperateResult<byte[]> BuildWriteCommand( string address, byte[] value, bool isBit )
  205. {
  206. var command = OmronFinsNetHelper.BuildWriteWordCommand( address, value, isBit );
  207. if (!command.IsSuccess) return command;
  208. return OperateResult.CreateSuccessResult( PackCommand( command.Content ) );
  209. }
  210. #endregion
  211. #region Read Support
  212. /// <summary>
  213. /// 从欧姆龙PLC中读取想要的数据,返回读取结果,读取单位为字
  214. /// </summary>
  215. /// <param name="address">读取地址,格式为"D100","C100","W100","H100","A100"</param>
  216. /// <param name="length">读取的数据长度</param>
  217. /// <returns>带成功标志的结果数据对象</returns>
  218. /// <example>
  219. /// 假设起始地址为D100,D100存储了温度,100.6℃值为1006,D101存储了压力,1.23Mpa值为123,D102,D103存储了产量计数,读取如下:
  220. /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\OmronFinsNet.cs" region="ReadExample2" title="Read示例" />
  221. /// 以下是读取不同类型数据的示例
  222. /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\OmronFinsNet.cs" region="ReadExample1" title="Read示例" />
  223. /// </example>
  224. public override OperateResult<byte[]> Read( string address, ushort length )
  225. {
  226. // 获取指令
  227. var command = BuildReadCommand( address, length, false );
  228. if (!command.IsSuccess) return OperateResult.CreateFailedResult<byte[]>( command );
  229. // 核心数据交互
  230. OperateResult<byte[]> read = ReadFromCoreServer( command.Content );
  231. if (!read.IsSuccess) return OperateResult.CreateFailedResult<byte[]>( read );
  232. // 数据有效性分析
  233. OperateResult<byte[]> valid = OmronFinsNetHelper.UdpResponseValidAnalysis( read.Content, true );
  234. if (!valid.IsSuccess) return OperateResult.CreateFailedResult<byte[]>( valid );
  235. // 读取到了正确的数据
  236. return OperateResult.CreateSuccessResult( valid.Content );
  237. }
  238. /// <summary>
  239. /// 从欧姆龙PLC中批量读取位软元件,返回读取结果
  240. /// </summary>
  241. /// <param name="address">读取地址,格式为"D100","C100","W100","H100","A100"</param>
  242. /// <param name="length">读取的长度</param>
  243. /// <returns>带成功标志的结果数据对象</returns>
  244. /// <example>
  245. /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\OmronFinsNet.cs" region="ReadBool" title="ReadBool示例" />
  246. /// </example>
  247. public OperateResult<bool[]> ReadBool( string address, ushort length )
  248. {
  249. // 获取指令
  250. var command = BuildReadCommand( address, length, true );
  251. if (!command.IsSuccess) return OperateResult.CreateFailedResult<bool[]>( command );
  252. // 核心数据交互
  253. OperateResult<byte[]> read = ReadFromCoreServer( command.Content );
  254. if (!read.IsSuccess) return OperateResult.CreateFailedResult<bool[]>( read );
  255. // 数据有效性分析
  256. OperateResult<byte[]> valid = OmronFinsNetHelper.UdpResponseValidAnalysis( read.Content, true );
  257. if (!valid.IsSuccess) return OperateResult.CreateFailedResult<bool[]>( valid );
  258. // 返回正确的数据信息
  259. return OperateResult.CreateSuccessResult( valid.Content.Select( m => m != 0x00 ? true : false ).ToArray( ) );
  260. }
  261. /// <summary>
  262. /// 从欧姆龙PLC中批量读取位软元件,返回读取结果
  263. /// </summary>
  264. /// <param name="address">读取地址,格式为"D100.0","C100.15","W100.7","H100.4","A100.9"</param>
  265. /// <returns>带成功标志的结果数据对象</returns>
  266. /// <remarks>
  267. /// 地址的格式请参照<see cref="ReadBool(string, ushort)"/>方法
  268. /// </remarks>
  269. /// <example>
  270. /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\OmronFinsNet.cs" region="ReadBool" title="ReadBool示例" />
  271. /// </example>
  272. public OperateResult<bool> ReadBool( string address )
  273. {
  274. OperateResult<bool[]> read = ReadBool( address, 1 );
  275. if (!read.IsSuccess) return OperateResult.CreateFailedResult<bool>( read );
  276. return OperateResult.CreateSuccessResult( read.Content[0] );
  277. }
  278. #endregion
  279. #region Write Base
  280. /// <summary>
  281. /// 向PLC写入数据,数据格式为原始的字节类型
  282. /// </summary>
  283. /// <param name="address">初始地址</param>
  284. /// <param name="value">原始的字节数据</param>
  285. /// <returns>结果</returns>
  286. /// <example>
  287. /// 假设起始地址为D100,D100存储了温度,100.6℃值为1006,D101存储了压力,1.23Mpa值为123,D102,D103存储了产量计数,读取如下:
  288. /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\OmronFinsNet.cs" region="WriteExample2" title="Write示例" />
  289. /// 以下是写入不同类型数据的示例
  290. /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\OmronFinsNet.cs" region="WriteExample1" title="Write示例" />
  291. /// </example>
  292. public override OperateResult Write( string address, byte[] value )
  293. {
  294. // 获取指令
  295. var command = BuildWriteCommand( address, value, false );
  296. if (!command.IsSuccess) return command;
  297. // 核心数据交互
  298. OperateResult<byte[]> read = ReadFromCoreServer( command.Content );
  299. if (!read.IsSuccess) return read;
  300. // 数据有效性分析
  301. OperateResult<byte[]> valid = OmronFinsNetHelper.UdpResponseValidAnalysis( read.Content, false );
  302. if (!valid.IsSuccess) return valid;
  303. // 成功
  304. return OperateResult.CreateSuccessResult( );
  305. }
  306. #endregion
  307. #region Write bool[]
  308. /// <summary>
  309. /// 向PLC中位软元件写入bool数组,返回值说明,比如你写入D100,values[0]对应D100.0
  310. /// </summary>
  311. /// <param name="address">要写入的数据地址</param>
  312. /// <param name="value">要写入的实际数据,长度为8的倍数</param>
  313. /// <returns>返回写入结果</returns>
  314. /// <example>
  315. /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\OmronFinsNet.cs" region="WriteBool" title="WriteBool示例" />
  316. /// </example>
  317. public OperateResult Write( string address, bool value )
  318. {
  319. return Write( address, new bool[] { value } );
  320. }
  321. /// <summary>
  322. /// 向PLC中位软元件写入bool数组,返回值说明,比如你写入D100,values[0]对应D100.0
  323. /// </summary>
  324. /// <param name="address">要写入的数据地址</param>
  325. /// <param name="values">要写入的实际数据,可以指定任意的长度</param>
  326. /// <returns>返回写入结果</returns>
  327. /// <example>
  328. /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\OmronFinsNet.cs" region="WriteBool" title="WriteBool示例" />
  329. /// </example>
  330. public OperateResult Write( string address, bool[] values )
  331. {
  332. // 获取指令
  333. var command = BuildWriteCommand( address, values.Select( m => m ? (byte)0x01 : (byte)0x00 ).ToArray( ), true );
  334. if (!command.IsSuccess) return command;
  335. // 核心数据交互
  336. OperateResult<byte[]> read = ReadFromCoreServer( command.Content );
  337. if (!read.IsSuccess) return read;
  338. // 数据有效性分析
  339. OperateResult<byte[]> valid = OmronFinsNetHelper.UdpResponseValidAnalysis( read.Content, false );
  340. if (!valid.IsSuccess) return valid;
  341. // 写入成功
  342. return OperateResult.CreateSuccessResult( );
  343. }
  344. #endregion
  345. #region Object Override
  346. /// <summary>
  347. /// 返回表示当前对象的字符串
  348. /// </summary>
  349. /// <returns>字符串</returns>
  350. public override string ToString( )
  351. {
  352. return $"OmronFinsUdp[{IpAddress}:{Port}]";
  353. }
  354. #endregion
  355. }
  356. }