123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using Edge.Core.Parser.BinaryParser.Attributes;
- using Edge.Core.Parser.BinaryParser.MessageEntity;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace SuZhouSuAnXin_BatteryEMS.MessageEntity
- {
- public enum FunctionCodeEnum
- {
- /// <summary>
- /// OPW 远端监控系统为主机,储能 EMS 为从机
- /// 读多个寄存器,主机读取从机的数据
- /// </summary>
- 读多个寄存器 = 0x03,
- /// <summary>
- /// OPW 远端监控系统为主机,储能 EMS 为从机
- /// 主机修改从机的数据
- /// 对单个寄存器写入
- /// </summary>
- 写单个寄存器 = 0x06,
- /// <summary>
- /// OPW 远端监控系统为主机,储能 EMS 为从机
- /// 主机修改从机的数据
- /// 写多个寄存器
- /// </summary>
- 写多个寄存器 = 0x10,
- ///// <summary>
- /////
- ///// </summary>
- //预置多个寄存器 = 0x11,
- }
- public abstract class MessageBase : MessageTemplateBase
- {
- [Format(2, EncodingType.BIN, -9999)]
- public int 事务处理标识符 { get; set; }
- [Format(2, EncodingType.BIN, -9980)]
- public int 协议标识符 { get; set; }
- [Format(2, EncodingType.BIN, -9970)]
- public int OuterDataLength { get; set; }
- [EnumerableFormat("OuterDataLength", -9950)]
- public virtual List<byte> OuterRawData { get; set; }
- }
- public class IncomingMessage : MessageBase
- {
- /// <summary>
- /// OPW 远端监控系统为主机,储能 EMS 为从机
- /// 表示被寻址的从机地址,本协议中规定从机地址为 20 H
- /// </summary>
- public byte SlaveAddress { get { return this.OuterRawData[0]; } }
- /// <summary>
- /// 功能码
- /// </summary>
- public FunctionCodeEnum FunctionCode { get { var r = Enum.ToObject(typeof(FunctionCodeEnum), this.OuterRawData[1]); return (FunctionCodeEnum)r; } }
- public byte InnerDataLength { get { return this.OuterRawData[2]; } }
- public IEnumerable<byte> InnerRawData { get { return this.OuterRawData.Skip(3); } }
- }
- public class OutgoingQueryMessage : MessageBase
- {
- /// <summary>
- ///
- /// </summary>
- /// <param name="startingRegAddress">地址分配规则: BMS 0x0000~0x009F 160 BMS 数据域, FMS 0x0100~0x019F 160 FMS 数据域, 系统 0x0200~0x029F 160 系统数据域</param>
- /// <param name="noOfRegAddress"></param>
- public OutgoingQueryMessage(int startingRegAddress, int noOfRegAddress)
- {
- //本协议中规定从机地址为 20 H
- base.OuterRawData = new List<byte>() { 0x20, (byte)FunctionCodeEnum.读多个寄存器 }
- .Concat(BitConverter.GetBytes(startingRegAddress).Take(2).Reverse())
- .Concat(BitConverter.GetBytes(noOfRegAddress).Take(2).Reverse()).ToList();
- }
- }
- //public class IncomingQueryMessage : MessageBase
- //{
- // [Format(1, EncodingType.BIN, -9980)]
- // public byte DataLength { get; set; }
- // [EnumerableFormat("DataLength", -9970)]
- // public List<byte> RawData { get; set; }
- // //[EnumerableFormat(2, -9960)]
- // //public List<byte> CRC16 { get; set; }
- //}
- /// <summary>
- /// used for 设置多个寄存器数据
- /// </summary>
- public class OutgoingWriteToMultipleRegMessage : MessageBase
- {
- /// <summary>
- ///
- /// </summary>
- /// <param name="startingRegAddress">地址分配规则: BMS 0x0000~0x009F 160 BMS 数据域, FMS 0x0100~0x019F 160 FMS 数据域, 系统 0x0200~0x029F 160 系统数据域</param>
- /// <param name="noOfRegAddress"></param>
- /// <param name="datas">byte[0]为高字节, byte[1]为低字节....</param>
- public OutgoingWriteToMultipleRegMessage(int startingRegAddress, int noOfRegAddress, byte[] datas)
- {
- base.OuterRawData = new List<byte>() { 0x20, (byte)FunctionCodeEnum.写多个寄存器 }
- .Concat(BitConverter.GetBytes(startingRegAddress).Take(2).Reverse())
- .Concat(BitConverter.GetBytes(noOfRegAddress).Take(2).Reverse())
- .Concat(datas).ToList();
- }
- }
- /// <summary>
- /// used for 设置单个寄存器数据
- /// </summary>
- public class OutgoingWriteToSingleRegMessage : MessageBase
- {
- /// <summary>
- ///
- /// </summary>
- /// <param name="startingRegAddress">地址分配规则: BMS 0x0000~0x009F 160 BMS 数据域, FMS 0x0100~0x019F 160 FMS 数据域, 系统 0x0200~0x029F 160 系统数据域</param>
- /// <param name="datas">byte[0]为高字节, byte[1]为低字节....</param>
- public OutgoingWriteToSingleRegMessage(int startingRegAddress, byte[] datas)
- {
- base.OuterRawData = new List<byte>() { 0x20, (byte)FunctionCodeEnum.写多个寄存器 }
- .Concat(BitConverter.GetBytes(startingRegAddress).Take(2).Reverse())
- .Concat(BitConverter.GetBytes(1).Take(2).Reverse())
- .Concat(datas).ToList();
- }
- }
- }
|