MessageBase.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using Edge.Core.Parser.BinaryParser.Attributes;
  2. using Edge.Core.Parser.BinaryParser.MessageEntity;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. namespace SuZhouSuAnXin_BatteryEMS.MessageEntity
  8. {
  9. public enum FunctionCodeEnum
  10. {
  11. /// <summary>
  12. /// OPW 远端监控系统为主机,储能 EMS 为从机
  13. /// 读多个寄存器,主机读取从机的数据
  14. /// </summary>
  15. 读多个寄存器 = 0x03,
  16. /// <summary>
  17. /// OPW 远端监控系统为主机,储能 EMS 为从机
  18. /// 主机修改从机的数据
  19. /// 对单个寄存器写入
  20. /// </summary>
  21. 写单个寄存器 = 0x06,
  22. /// <summary>
  23. /// OPW 远端监控系统为主机,储能 EMS 为从机
  24. /// 主机修改从机的数据
  25. /// 写多个寄存器
  26. /// </summary>
  27. 写多个寄存器 = 0x10,
  28. ///// <summary>
  29. /////
  30. ///// </summary>
  31. //预置多个寄存器 = 0x11,
  32. }
  33. public abstract class MessageBase : MessageTemplateBase
  34. {
  35. [Format(2, EncodingType.BIN, -9999)]
  36. public int 事务处理标识符 { get; set; }
  37. [Format(2, EncodingType.BIN, -9980)]
  38. public int 协议标识符 { get; set; }
  39. [Format(2, EncodingType.BIN, -9970)]
  40. public int OuterDataLength { get; set; }
  41. [EnumerableFormat("OuterDataLength", -9950)]
  42. public virtual List<byte> OuterRawData { get; set; }
  43. }
  44. public class IncomingMessage : MessageBase
  45. {
  46. /// <summary>
  47. /// OPW 远端监控系统为主机,储能 EMS 为从机
  48. /// 表示被寻址的从机地址,本协议中规定从机地址为 20 H
  49. /// </summary>
  50. public byte SlaveAddress { get { return this.OuterRawData[0]; } }
  51. /// <summary>
  52. /// 功能码
  53. /// </summary>
  54. public FunctionCodeEnum FunctionCode { get { var r = Enum.ToObject(typeof(FunctionCodeEnum), this.OuterRawData[1]); return (FunctionCodeEnum)r; } }
  55. public byte InnerDataLength { get { return this.OuterRawData[2]; } }
  56. public IEnumerable<byte> InnerRawData { get { return this.OuterRawData.Skip(3); } }
  57. }
  58. public class OutgoingQueryMessage : MessageBase
  59. {
  60. /// <summary>
  61. ///
  62. /// </summary>
  63. /// <param name="startingRegAddress">地址分配规则: BMS 0x0000~0x009F 160 BMS 数据域, FMS 0x0100~0x019F 160 FMS 数据域, 系统 0x0200~0x029F 160 系统数据域</param>
  64. /// <param name="noOfRegAddress"></param>
  65. public OutgoingQueryMessage(int startingRegAddress, int noOfRegAddress)
  66. {
  67. //本协议中规定从机地址为 20 H
  68. base.OuterRawData = new List<byte>() { 0x20, (byte)FunctionCodeEnum.读多个寄存器 }
  69. .Concat(BitConverter.GetBytes(startingRegAddress).Take(2).Reverse())
  70. .Concat(BitConverter.GetBytes(noOfRegAddress).Take(2).Reverse()).ToList();
  71. }
  72. }
  73. //public class IncomingQueryMessage : MessageBase
  74. //{
  75. // [Format(1, EncodingType.BIN, -9980)]
  76. // public byte DataLength { get; set; }
  77. // [EnumerableFormat("DataLength", -9970)]
  78. // public List<byte> RawData { get; set; }
  79. // //[EnumerableFormat(2, -9960)]
  80. // //public List<byte> CRC16 { get; set; }
  81. //}
  82. /// <summary>
  83. /// used for 设置多个寄存器数据
  84. /// </summary>
  85. public class OutgoingWriteToMultipleRegMessage : MessageBase
  86. {
  87. /// <summary>
  88. ///
  89. /// </summary>
  90. /// <param name="startingRegAddress">地址分配规则: BMS 0x0000~0x009F 160 BMS 数据域, FMS 0x0100~0x019F 160 FMS 数据域, 系统 0x0200~0x029F 160 系统数据域</param>
  91. /// <param name="noOfRegAddress"></param>
  92. /// <param name="datas">byte[0]为高字节, byte[1]为低字节....</param>
  93. public OutgoingWriteToMultipleRegMessage(int startingRegAddress, int noOfRegAddress, byte[] datas)
  94. {
  95. base.OuterRawData = new List<byte>() { 0x20, (byte)FunctionCodeEnum.写多个寄存器 }
  96. .Concat(BitConverter.GetBytes(startingRegAddress).Take(2).Reverse())
  97. .Concat(BitConverter.GetBytes(noOfRegAddress).Take(2).Reverse())
  98. .Concat(datas).ToList();
  99. }
  100. }
  101. /// <summary>
  102. /// used for 设置单个寄存器数据
  103. /// </summary>
  104. public class OutgoingWriteToSingleRegMessage : MessageBase
  105. {
  106. /// <summary>
  107. ///
  108. /// </summary>
  109. /// <param name="startingRegAddress">地址分配规则: BMS 0x0000~0x009F 160 BMS 数据域, FMS 0x0100~0x019F 160 FMS 数据域, 系统 0x0200~0x029F 160 系统数据域</param>
  110. /// <param name="datas">byte[0]为高字节, byte[1]为低字节....</param>
  111. public OutgoingWriteToSingleRegMessage(int startingRegAddress, byte[] datas)
  112. {
  113. base.OuterRawData = new List<byte>() { 0x20, (byte)FunctionCodeEnum.写多个寄存器 }
  114. .Concat(BitConverter.GetBytes(startingRegAddress).Take(2).Reverse())
  115. .Concat(BitConverter.GetBytes(1).Take(2).Reverse())
  116. .Concat(datas).ToList();
  117. }
  118. }
  119. }