modbus测试

zw
smartwyy 7 months ago
parent 584602cd7f
commit bffb2cbdfc

@ -1,151 +1,37 @@
// See https://aka.ms/new-console-template for more information
using System.Collections;
using ConsoleStarter;
using System.Diagnostics;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Utils;
using HybirdFrameworkDriver.ModbusTcpMaster;
internal class Program
{
public static void Main(string[] args)
{
TestPerson testPerson = new TestPerson();
testPerson.intParam = 11;
testPerson.intParamNull = 12;
testPerson.uit = 13;
testPerson.byteParam = 2;
testPerson._sb = 3;
testPerson.s = "abcdef";
testPerson.st = -15;
testPerson.ust = 15;
testPerson.I16 = -16;
testPerson.UI16 = 16
;
testPerson.I32 = -32;
testPerson.UI32 = 32;
//testPerson.I64 = -64;
//testPerson.UI64 = 64;
//testPerson.lg = -100;
//testPerson.ulg = 100;
testPerson.ft = 12.11f;
testPerson.de = -12.12d;
// testPerson.de2 = -12.12d;
// testPerson.bl = false;
testPerson.ft2 = -13.126f;
testPerson.ft3 = 1000f;
testPerson.d3 = -13.129d;
byte[] bytes2 = ModelConvert.Encode(testPerson);
TestPerson testPerson1 = ModelConvert.Declode<TestPerson>(bytes2);
int a = testPerson1.intParam;
int b9 = (int)testPerson1.intParamNull;
uint c = testPerson1.uit;
byte d = testPerson1.byteParam;
byte e = testPerson1._sb;
string f = testPerson1.s;
short a1 = testPerson1.st;
ushort a2 = testPerson1.ust;
Int16 a3 = testPerson1.I16;
UInt16 a4 = testPerson1.UI16;
Int32 b1 = testPerson1.I32;
UInt32 b2 = testPerson1.UI32;
//Int64 b3 = testPerson1.I64;
//UInt64 b4 = testPerson1.UI64;
//long b5 = testPerson1.lg;
//ulong c1 = testPerson1.ulg;
float c2 = testPerson1.ft;
double c3 = testPerson1.de;
//double c6 = testPerson1.de2;
//bool c4 = testPerson1.bl;
float c5 = testPerson1.ft2;
// 偏移量
float c6 = testPerson1.ft3;
// double负数转换
double d3 = testPerson1.d3;
double d4 = -d3;
List<PropertyAttribute> list = new List<PropertyAttribute>
ModbusTcpMaster master = new ModbusTcpMaster()
{
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)
Ip = "",
ReadAction = ReadFunc
};
int start = 0;
int length = 0;
foreach (PropertyAttribute attribute in list)
{
if (start < attribute.Start)
{
start = attribute.Start;
length = attribute.Length;
}
}
Console.WriteLine($"{start}, {length}");
byte[] bytes = new Byte[] { 0xFE, 0xFF };
ushort uInt16 = BitConverter.ToUInt16(bytes);
Console.WriteLine($"{uInt16}, {BitUtls.BytesToHexStr(bytes)}");
BitArray bitArray = new BitArray(16);
int index = 0;
foreach (var b in bytes)
{
for (int i = 0; i < 8; i++)
{
bool flag = (b & (1 << (index % 8))) > 0;
Console.WriteLine(flag);
bitArray[index++] = flag;
}
}
byte[] newbytes = new byte[2];
bitArray.CopyTo(newbytes, 0);
foreach (var b in newbytes)
{
Console.WriteLine(b);
}
bool connected = master.Connect();
Debug.Assert(connected, "连接modbus server 失败");
ushort int16 = BitConverter.ToUInt16(newbytes);
Console.WriteLine($"{int16}, {BitUtls.BytesToHexStr(newbytes)}");
WaterCoolData coolData = new WaterCoolData();
UInt64 max = UInt64.MaxValue / 8;
byte[] bytes1 = BitConverter.GetBytes(max);
Console.WriteLine($"{max}, {BitUtls.BytesToHexStr(bytes1)}");
foreach (var b in bytes1)
{
Console.WriteLine(b);
}
coolData.DraughtFan2.Value = true;
bool writeResult = master.WriteValue(coolData.DraughtFan2);
Debug.Assert(writeResult, "写入失败");
}
BitArray array = new BitArray(16);
array[0] = true;
byte[] ab = new byte[2];
array.CopyTo(ab, 0);
foreach (var b in ab)
private static void ReadFunc(ModbusTcpMaster master)
{
byte[]? bytes = master.BatchRead(0, 46);
if (bytes != null)
{
Console.WriteLine(b);
WaterCoolData coolData = ModbusDecoder.Decode<WaterCoolData>(bytes);
float temperature1Value = coolData.Temperature1.Value;
}
}

@ -6,7 +6,7 @@ using log4net;
namespace HybirdFrameworkDriver.ModbusTcpMaster;
public abstract class ModbusTcpMaster
public class ModbusTcpMaster
{
private static readonly ILog Log = LogManager.GetLogger(typeof(ModbusTcpMaster));
@ -17,9 +17,11 @@ public abstract class ModbusTcpMaster
public int Duration { get; set; } = 1000;
public bool Connected { get; set; } = false;
private bool StopFlag { get; set; } = false;
public delegate void MyReadAction(ModbusTcpMaster str);
public MyReadAction ReadAction { get; set; }
public Action? Action { get; set; }
private bool StopFlag { get; set; } = false;
private ModbusTcpNet ModbusTcpNet;
@ -83,7 +85,7 @@ public abstract class ModbusTcpMaster
{
try
{
Action?.Invoke();
ReadAction(this);
Thread.Sleep(Duration);
}
catch (Exception e)
@ -109,6 +111,17 @@ public abstract class ModbusTcpMaster
return null;
}
public byte[]? BatchRead(int registerNo, int length)
{
OperateResult<byte[]> result = ModbusTcpNet.Read("x=3;" + registerNo, (ushort)length);
if (result.IsSuccess)
{
return result.Content;
}
return null;
}
public bool WriteValue<T>(ModbusProperty<T> property)
{
T value = property.Value;

Loading…
Cancel
Save