You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.0 KiB

8 months ago
// See https://aka.ms/new-console-template for more information
8 months ago
using System.Diagnostics;
using System.IO.Pipes;
using HslCommunication.Core;
8 months ago
using HybirdFrameworkCore.Autofac.Attribute;
8 months ago
using HybirdFrameworkCore.Utils;
8 months ago
using HybirdFrameworkDriver.ModbusTcpMaster;
8 months ago
internal class Program
{
public static void Main(string[] args)
{
8 months ago
ModbusTcpMaster master = new ModbusTcpMaster()
8 months ago
{
Ip = "192.168.1.5",
//默认DataFormat.ABCD
//DataFormat = DataFormat.ABCD,
8 months ago
ReadAction = ReadFunc
8 months ago
};
8 months ago
bool connected = master.Connect();
Debug.Assert(connected, "连接modbus server 失败");
8 months ago
#region 测试写
8 months ago
WaterCoolData coolData = new WaterCoolData();
8 months ago
//写入ushort
coolData.ushortType.Value = 18;
bool writeResult4 = master.WriteValue(coolData.ushortType);
Debug.Assert(writeResult4, "写入失败");
//写入byte 0-8 byte[1]
coolData.Status.Value = 12;
bool writeResult1 = master.WriteValue(coolData.Status);
Debug.Assert(writeResult1, "写入失败");
//写入bit
coolData.DraughtFan1.Value = true;
bool writeResult3 = master.WriteValue(coolData.DraughtFan1);
Debug.Assert(writeResult3, "写入失败");
coolData.Temperature1.Value = 12.7f;
bool writeResult5 = master.WriteValue(coolData.Temperature1);
Debug.Assert(writeResult5, "写入失败");
#endregion 测试写
8 months ago
}
private static WaterCoolData coolData = new WaterCoolData();
8 months ago
private static void ReadFunc(ModbusTcpMaster master)
{
byte[]? bytes = master.BatchRead(1, 46);
8 months ago
if (bytes != null)
{
ModbusDecoder.Decode<WaterCoolData>(bytes, coolData);
Debug.WriteLine("12.7f" + coolData.Temperature1.Value);
Debug.WriteLine("12" + coolData.Status.Value);
Debug.WriteLine("fasle" + coolData.CompressorState1.Value);
Debug.WriteLine("18" + coolData.ushortType.Value);
8 months ago
}
}
private static void Test1()
{
Console.WriteLine("Hello, World!");
List<PropertyAttribute> list = new List<PropertyAttribute>
{
new(0, 4),
new(4, 8),
new(12, 1),
new(13, 2),
new(15, 1),
new(16, 1),
new(17, 1),
new(18, 2),
new(20, 2),
new(22, 2),
new(24, 2),
new(26, 1),
new(27, 2),
new(29, 2),
new(31, 1),
new(32, 1),
new(33, 29)
};
8 months ago
8 months ago
Queue<EncodeData> queue = new Queue<EncodeData>();
int size = 0;
foreach (PropertyAttribute p in list)
{
size += p.Length;
if (size < 8)
{
queue.Enqueue(new EncodeData()
{
Start = 0,
Length = p.Length
8 months ago
});
}
else
8 months ago
{
8 months ago
Console.WriteLine("---------------------------begin one byte---------------------");
int length = 0;
while (queue.Count > 0)
{
EncodeData data = queue.Dequeue();
length += data.Length;
Console.WriteLine($"use {data.Start}, {data.Length}");
}
size -= length;
int occupy = 8 - length;
Console.WriteLine($"use 0, {occupy}");
Console.WriteLine("---------------------------end one byte---------------------");
size -= occupy;
while (size >= 8)
{
Console.WriteLine("---------------------------begin one byte---------------------");
Console.WriteLine($"use {occupy}, 8");
Console.WriteLine("---------------------------end one byte---------------------");
size -= 8;
occupy += 8;
}
if (size > 0)
{
queue.Enqueue(new EncodeData()
{
Start = occupy,
Length = size
8 months ago
});
}
8 months ago
}
8 months ago
}
8 months ago
8 months ago
foreach (EncodeData d in queue)
{
Console.WriteLine($"{d.Start}, {d.Length}");
8 months ago
}
8 months ago
UInt32 i = UInt32.MaxValue - 1;
byte[] bytes = BitConverter.GetBytes(i).Reverse().ToArray();
foreach (byte b in bytes)
{
Console.WriteLine(b);
}
Console.WriteLine(BitUtls.BytesToHexStr(bytes));
UInt32 r = BitConverter.ToUInt32(bytes);
Console.WriteLine(r);
bytes = BitConverter.GetBytes(r);
foreach (byte b in bytes)
{
Console.WriteLine(b);
}
Console.WriteLine(BitUtls.BytesToHexStr(bytes));
8 months ago
}
}
8 months ago
public struct EncodeData
{
public int Start { get; set; }
public int Length { get; set; }
public byte[] Value { get; set; }
}