modbus测试

master
smartwyy 7 months ago
parent 584602cd7f
commit bffb2cbdfc

@ -1,151 +1,37 @@
// See https://aka.ms/new-console-template for more information // See https://aka.ms/new-console-template for more information
using System.Collections; using System.Diagnostics;
using ConsoleStarter;
using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Utils; using HybirdFrameworkCore.Utils;
using HybirdFrameworkDriver.ModbusTcpMaster;
internal class Program internal class Program
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
TestPerson testPerson = new TestPerson(); ModbusTcpMaster master = new ModbusTcpMaster()
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>
{ {
new(0, 4), Ip = "",
new(4, 8), ReadAction = ReadFunc
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)
}; };
int start = 0; bool connected = master.Connect();
int length = 0; Debug.Assert(connected, "连接modbus server 失败");
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 }; WaterCoolData coolData = new WaterCoolData();
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]; coolData.DraughtFan2.Value = true;
bitArray.CopyTo(newbytes, 0); bool writeResult = master.WriteValue(coolData.DraughtFan2);
foreach (var b in newbytes) Debug.Assert(writeResult, "写入失败");
{ }
Console.WriteLine(b);
}
ushort int16 = BitConverter.ToUInt16(newbytes);
Console.WriteLine($"{int16}, {BitUtls.BytesToHexStr(newbytes)}");
UInt64 max = UInt64.MaxValue / 8;
byte[] bytes1 = BitConverter.GetBytes(max);
Console.WriteLine($"{max}, {BitUtls.BytesToHexStr(bytes1)}");
foreach (var b in bytes1)
{
Console.WriteLine(b);
}
BitArray array = new BitArray(16); private static void ReadFunc(ModbusTcpMaster master)
array[0] = true; {
byte[] ab = new byte[2]; byte[]? bytes = master.BatchRead(0, 46);
array.CopyTo(ab, 0); if (bytes != null)
foreach (var b in ab)
{ {
Console.WriteLine(b); WaterCoolData coolData = ModbusDecoder.Decode<WaterCoolData>(bytes);
float temperature1Value = coolData.Temperature1.Value;
} }
} }

@ -6,7 +6,7 @@ using log4net;
namespace HybirdFrameworkDriver.ModbusTcpMaster; namespace HybirdFrameworkDriver.ModbusTcpMaster;
public abstract class ModbusTcpMaster public class ModbusTcpMaster
{ {
private static readonly ILog Log = LogManager.GetLogger(typeof(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 int Duration { get; set; } = 1000;
public bool Connected { get; set; } = false; 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; private ModbusTcpNet ModbusTcpNet;
@ -83,7 +85,7 @@ public abstract class ModbusTcpMaster
{ {
try try
{ {
Action?.Invoke(); ReadAction(this);
Thread.Sleep(Duration); Thread.Sleep(Duration);
} }
catch (Exception e) catch (Exception e)
@ -109,6 +111,17 @@ public abstract class ModbusTcpMaster
return null; 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) public bool WriteValue<T>(ModbusProperty<T> property)
{ {
T value = property.Value; T value = property.Value;

Loading…
Cancel
Save