FetchWriteMessage.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace MECF.Framework.RT.Core.IoProviders.Common.IMessage
  6. {
  7. /// <summary>
  8. /// 西门子Fetch/Write消息解析协议
  9. /// </summary>
  10. public class FetchWriteMessage : INetMessage
  11. {
  12. /// <summary>
  13. /// 消息头的指令长度
  14. /// </summary>
  15. public int ProtocolHeadBytesLength
  16. {
  17. get
  18. {
  19. return 16;
  20. }
  21. }
  22. /// <summary>
  23. /// 从当前的头子节文件中提取出接下来需要接收的数据长度
  24. /// </summary>
  25. /// <returns>返回接下来的数据内容长度</returns>
  26. public int GetContentLengthByHeadBytes( )
  27. {
  28. if (SendBytes != null)
  29. {
  30. if (HeadBytes[5] == 0x04)
  31. {
  32. return 0;
  33. }
  34. else
  35. {
  36. return SendBytes[12] * 256 + SendBytes[13];
  37. }
  38. }
  39. else
  40. {
  41. return 16;
  42. }
  43. }
  44. /// <summary>
  45. /// 检查头子节的合法性
  46. /// </summary>
  47. /// <param name="token">特殊的令牌,有些特殊消息的验证</param>
  48. /// <returns>是否合法</returns>
  49. public bool CheckHeadBytesLegal( byte[] token )
  50. {
  51. if (HeadBytes == null) return false;
  52. if (HeadBytes[0] == 0x53 && HeadBytes[1] == 0x35)
  53. {
  54. return true;
  55. }
  56. else
  57. {
  58. return false;
  59. }
  60. }
  61. /// <summary>
  62. /// 获取头子节里的消息标识
  63. /// </summary>
  64. /// <returns>消息标识</returns>
  65. public int GetHeadBytesIdentity( )
  66. {
  67. return HeadBytes[3];
  68. }
  69. /// <summary>
  70. /// 消息头字节
  71. /// </summary>
  72. public byte[] HeadBytes { get; set; }
  73. /// <summary>
  74. /// 消息内容字节
  75. /// </summary>
  76. public byte[] ContentBytes { get; set; }
  77. /// <summary>
  78. /// 发送的字节信息
  79. /// </summary>
  80. public byte[] SendBytes { get; set; }
  81. }
  82. }