S7Message.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace MECF.Framework.RT.Core.IoProviders.Siemens.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 == null) return false;
  35. if (HeadBytes[0] == 0x03 && HeadBytes[1] == 0x00)
  36. {
  37. return true;
  38. }
  39. else
  40. {
  41. return false;
  42. }
  43. }
  44. /// <summary>
  45. /// 获取剩余的内容长度
  46. /// </summary>
  47. /// <returns>数据内容长度</returns>
  48. public int GetContentLengthByHeadBytes()
  49. {
  50. if (HeadBytes?.Length >= 4)
  51. {
  52. return HeadBytes[2] * 256 + HeadBytes[3] - 4;
  53. }
  54. else
  55. {
  56. return 0;
  57. }
  58. }
  59. /// <summary>
  60. /// 获取消息号,此处无效
  61. /// </summary>
  62. /// <returns>消息标识</returns>
  63. public int GetHeadBytesIdentity()
  64. {
  65. return 0;
  66. }
  67. /// <summary>
  68. /// 发送的字节信息
  69. /// </summary>
  70. public byte[] SendBytes { get; set; }
  71. }
  72. }