FinsMessage.cs 2.0 KB

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