OmronFinsNet.cs 18 KB

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