S7Message.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. /// 西门子S7协议的消息解析规则
  9. /// </summary>
  10. public class S7Message : INetMessage
  11. {
  12. /// <summary>
  13. /// 西门子头字节的长度
  14. /// </summary>
  15. public int ProtocolHeadBytesLength
  16. {
  17. get { return 4; }
  18. }
  19. /// <summary>
  20. /// 头子节的数据
  21. /// </summary>
  22. public byte[] HeadBytes { get; set; }
  23. /// <summary>
  24. /// 内容字节的数据
  25. /// </summary>
  26. public byte[] ContentBytes { get; set; }
  27. /// <summary>
  28. /// 检查头子节是否合法的判断
  29. /// </summary>
  30. /// <param name="token">令牌</param>
  31. /// <returns>是否合法的</returns>
  32. public bool CheckHeadBytesLegal(byte[] token)
  33. {
  34. if (HeadBytes[0] == 0x03 && HeadBytes[1] == 0x00)
  35. {
  36. return true;
  37. }
  38. else
  39. {
  40. return false;
  41. }
  42. }
  43. /// <summary>
  44. /// 获取剩余的内容长度
  45. /// </summary>
  46. /// <returns></returns>
  47. public int GetContentLengthByHeadBytes()
  48. {
  49. if (HeadBytes?.Length >= 4)
  50. {
  51. return HeadBytes[2] * 256 + HeadBytes[3] - 4;
  52. }
  53. else
  54. {
  55. return 0;
  56. }
  57. }
  58. /// <summary>
  59. /// 获取消息号,此处无效
  60. /// </summary>
  61. /// <returns></returns>
  62. public int GetHeadBytesIdentity()
  63. {
  64. return 0;
  65. }
  66. /// <summary>
  67. /// 发送的字节信息
  68. /// </summary>
  69. public byte[] SendBytes { get; set; }
  70. }
  71. }