FinsMessage.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. namespace MECF.Framework.RT.Core.IoProviders.Omron
  7. {
  8. /// <summary>
  9. /// 用于欧姆龙通信的Fins协议的消息解析规则
  10. /// </summary>
  11. public class FinsMessage : INetMessage
  12. {
  13. /// <summary>
  14. /// 消息头的指令长度
  15. /// </summary>
  16. public int ProtocolHeadBytesLength
  17. {
  18. get { return 8; }
  19. }
  20. /// <summary>
  21. /// 从当前的头子节文件中提取出接下来需要接收的数据长度
  22. /// </summary>
  23. /// <returns>返回接下来的数据内容长度</returns>
  24. public int GetContentLengthByHeadBytes( )
  25. {
  26. byte[] buffer = new byte[4];
  27. buffer[0] = HeadBytes[7];
  28. buffer[1] = HeadBytes[6];
  29. buffer[2] = HeadBytes[5];
  30. buffer[3] = HeadBytes[4];
  31. return BitConverter.ToInt32( buffer, 0 );
  32. }
  33. /// <summary>
  34. /// 检查头子节的合法性
  35. /// </summary>
  36. /// <param name="token">特殊的令牌,有些特殊消息的验证</param>
  37. /// <returns>是否成功的结果</returns>
  38. public bool CheckHeadBytesLegal( byte[] token )
  39. {
  40. if (HeadBytes == null) return false;
  41. if (HeadBytes[0] == 0x46 && HeadBytes[1] == 0x49 && HeadBytes[2] == 0x4E && HeadBytes[3] == 0x53)
  42. {
  43. return true;
  44. }
  45. else
  46. {
  47. return false;
  48. }
  49. }
  50. /// <summary>
  51. /// 获取头子节里的消息标识
  52. /// </summary>
  53. /// <returns>消息id</returns>
  54. public int GetHeadBytesIdentity( )
  55. {
  56. return 0;
  57. }
  58. /// <summary>
  59. /// 消息头字节
  60. /// </summary>
  61. public byte[] HeadBytes { get; set; }
  62. /// <summary>
  63. /// 消息内容字节
  64. /// </summary>
  65. public byte[] ContentBytes { get; set; }
  66. /// <summary>
  67. /// 发送的字节信息
  68. /// </summary>
  69. public byte[] SendBytes { get; set; }
  70. }
  71. }