Files
ThingsGateway/framework/Foundation/ThingsGateway.Foundation.Adapter.DLT645/DLT645/DLT645_2007BitConverter.cs

118 lines
3.8 KiB
C#
Raw Normal View History

2023-08-25 19:33:08 +08:00
#region copyright
//------------------------------------------------------------------------------
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
// 此代码版权除特别声明外的代码归作者本人Diego所有
// 源代码使用协议遵循本仓库的开源协议及附加协议
// Gitee源代码仓库https://gitee.com/diego2098/ThingsGateway
// Github源代码仓库https://github.com/kimdiego2098/ThingsGateway
// 使用文档https://diego2098.gitee.io/thingsgateway-docs/
// QQ群605534569
//------------------------------------------------------------------------------
#endregion
using System.Text;
using ThingsGateway.Foundation.Extension.Generic;
namespace ThingsGateway.Foundation.Adapter.DLT645;
/// <summary>
/// DLT645_2007
/// </summary>
public class DLT645_2007BitConverter : ThingsGatewayBitConverter
{
/// <summary>
/// DLT645_2007
/// </summary>
public DLT645_2007BitConverter(EndianType endianType) : base(endianType)
{
}
/// <summary>
/// DLT645协议转换double
/// </summary>
/// <param name="buffer">带数据项标识</param>
/// <param name="offset"></param>
/// <returns></returns>
public override double ToDouble(byte[] buffer, int offset)
{
return Convert.ToDouble(this.ToString(buffer, offset, buffer.Length));
}
2023-09-30 23:05:53 +08:00
/// <inheritdoc/>
public override IThingsGatewayBitConverter CopyNew()
{
return new DLT645_2007BitConverter(EndianType)
{
DataFormat = DataFormat,
BcdFormat = BcdFormat,
Encoding = Encoding,
IsStringReverseByteWord = IsStringReverseByteWord,
Length = Length,
};
}
2023-08-25 19:33:08 +08:00
/// <inheritdoc/>
public override string ToString(byte[] buffer)
{
return this.ToString(buffer, 0, buffer.Length);
}
/// <inheritdoc/>
public override string ToString(byte[] buffer, int offset, int length)
{
2023-09-30 23:05:53 +08:00
buffer = buffer.RemoveBegin(offset);
2023-08-25 19:33:08 +08:00
buffer = buffer.BytesAdd(-0x33);
var dataInfos = DLT645Helper.GetDataInfos(buffer);
StringBuilder stringBuilder = new();
foreach (var dataInfo in dataInfos)
{
//实际数据
var content = buffer.SelectMiddle(4, dataInfo.ByteLength).Reverse().ToArray();
if (dataInfo.IsSigned)//可能为负数
{
if (content[0] > 0x80)//最高位是表示正负
{
content[0] = (byte)(content[0] - 0x80);
if (dataInfo.Digtal == 0)//无小数点
{
2023-09-30 23:05:53 +08:00
stringBuilder.Append($"-{content.ToHexString()}");
2023-08-25 19:33:08 +08:00
}
else
{
2023-09-30 23:05:53 +08:00
stringBuilder.Append((-(Convert.ToDouble(content.ToHexString()) / Math.Pow(10.0, dataInfo.Digtal))).ToString());
2023-08-25 19:33:08 +08:00
}
}
else
{
ToString(stringBuilder, dataInfo, content);
}
}
else
{
ToString(stringBuilder, dataInfo, content);
}
}
return stringBuilder.ToString();
static void ToString(StringBuilder stringBuilder, DataInfo dataInfo, byte[] content)
{
if (dataInfo.Digtal < 0)
{
2023-09-30 23:05:53 +08:00
stringBuilder.Append($"{Encoding.ASCII.GetString(content)}");
2023-08-25 19:33:08 +08:00
}
else if (dataInfo.Digtal == 0)//无小数点
{
2023-09-30 23:05:53 +08:00
stringBuilder.Append($"{content.ToHexString()}");
2023-08-25 19:33:08 +08:00
}
else
{
2023-09-30 23:05:53 +08:00
stringBuilder.Append(((Convert.ToDouble(content.ToHexString()) / Math.Pow(10.0, dataInfo.Digtal))).ToString());
2023-08-25 19:33:08 +08:00
}
}
}
}