FetchWriteMessage.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. /// 西门子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[0] == 0x53 && HeadBytes[1] == 0x35)
  52. {
  53. return true;
  54. }
  55. else
  56. {
  57. return false;
  58. }
  59. }
  60. /// <summary>
  61. /// 获取头子节里的消息标识
  62. /// </summary>
  63. /// <returns></returns>
  64. public int GetHeadBytesIdentity( )
  65. {
  66. return HeadBytes[3];
  67. }
  68. /// <summary>
  69. /// 消息头字节
  70. /// </summary>
  71. public byte[] HeadBytes { get; set; }
  72. /// <summary>
  73. /// 消息内容字节
  74. /// </summary>
  75. public byte[] ContentBytes { get; set; }
  76. /// <summary>
  77. /// 发送的字节信息
  78. /// </summary>
  79. public byte[] SendBytes { get; set; }
  80. }
  81. }