S7Message.cs 2.0 KB

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