找回密码
 立即注册
首页 业界区 业界 基于.NET和C#构建光伏IoT物模型方案

基于.NET和C#构建光伏IoT物模型方案

米嘉怡 2 小时前
一、目前国内接入最常见、最有代表性的 4 类光伏设备

 
1.png

二、华为 SUN2000 逆变器通讯报文示例

这是一个标准 Modbus TCP 请求报文
00 01 00 00 00 06 01 03 75 30 00 06
含义:
Modbus TCP 报文由两部分组成:
MBAP Header(7字节) + PDU(功能码 + 数据)
2.png

3.png

4.png

在光伏逆变器里,这类请求通常用于:
5.png

响应
这是一个标准的Modbus TCP 响应报文结构
00 01 00 00 00 0F 01 03 0C 09 C4 00 64 13 88 00 32
MBAP Header(7字节) + PDU(功能码 + 数据)
6.png

PDU数据解析
7.png
8.png

结合光伏业务的数值换算
厂家协议通常定义 倍率(Scale)
9.png

最终物模型字段赋值示例
{ “dcVoltage”: 250.0, “dcCurrent”: 10.0, “dcPower”: 5.0, “internalTemp”: 50 }
三、示例代码

报文请求构造 → 响应报文解析 → 寄存器解码 → 光伏设备物模型组织
目标:

  • 协议层、解析层、物模型层解耦
  • 方便后续做 多品牌 Adapter + 配置化映射
代码结构
10.png

1、Modbus TCP 请求报文构造

ModbusRequest.cs
  1. public class ModbusRequest
  2. {
  3.     public ushort TransactionId { get; set; }
  4.     public byte UnitId { get; set; }
  5.     public byte FunctionCode { get; set; } = 0x03;
  6.     public ushort StartAddress { get; set; }
  7.     public ushort Quantity { get; set; }
  8.     public byte[] ToBytes()
  9.     {
  10.         var buffer = new List<byte>();
  11.         // Transaction ID
  12.         buffer.AddRange(BitConverter.GetBytes(TransactionId).Reverse());
  13.         // Protocol ID (0x0000)
  14.         buffer.Add(0x00);
  15.         buffer.Add(0x00);
  16.         // Length = UnitId + PDU
  17.         ushort length = 1 + 1 + 2 + 2;
  18.         buffer.AddRange(BitConverter.GetBytes(length).Reverse());
  19.         // Unit ID
  20.         buffer.Add(UnitId);
  21.         // PDU
  22.         buffer.Add(FunctionCode);
  23.         buffer.AddRange(BitConverter.GetBytes(StartAddress).Reverse());
  24.         buffer.AddRange(BitConverter.GetBytes(Quantity).Reverse());
  25.         return buffer.ToArray();
  26.     }
  27. }
复制代码
2. Modbus TCP 响应解析

ModbusResponse.cs
  1. public class ModbusResponse
  2. {
  3.     public ushort TransactionId { get; set; }
  4.     public byte UnitId { get; set; }
  5.     public byte FunctionCode { get; set; }
  6.     public byte ByteCount { get; set; }
  7.     public byte[] Data { get; set; }
  8. }
复制代码
ModbusParser.cs

[code]public static class ModbusParser{    public static ModbusResponse ParseResponse(byte[] response)    {        if (response.Length < 9)            throw new Exception("Invalid Modbus response length");        var result = new ModbusResponse();        result.TransactionId = ReadUInt16(response, 0);        ushort protocolId = ReadUInt16(response, 2);        ushort length = ReadUInt16(response, 4);        if (protocolId != 0)            throw new Exception("Invalid Protocol ID");        result.UnitId = response[6];        result.FunctionCode = response[7];        result.ByteCount = response[8];        result.Data = response.Skip(9).Take(result.ByteCount).ToArray();        return result;    }    private static ushort ReadUInt16(byte[] buffer, int index)    {        return (ushort)((buffer[index]

相关推荐

您需要登录后才可以回帖 登录 | 立即注册