From bffb2cbdfcd0f4bd702fb9f60d50dea4d14d89b2 Mon Sep 17 00:00:00 2001 From: smartwyy <645583145@qq.com> Date: Fri, 19 Apr 2024 11:39:14 +0800 Subject: [PATCH] =?UTF-8?q?modbus=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConsoleStarter/Program.cs | 150 +++--------------- .../ModbusTcpMaster/ModbusTcpMaster.cs | 21 ++- 2 files changed, 35 insertions(+), 136 deletions(-) diff --git a/ConsoleStarter/Program.cs b/ConsoleStarter/Program.cs index 650c04c..f05a0c0 100644 --- a/ConsoleStarter/Program.cs +++ b/ConsoleStarter/Program.cs @@ -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(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 list = new List + 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}"); + bool connected = master.Connect(); + Debug.Assert(connected, "连接modbus server 失败"); - 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; - } - } + WaterCoolData coolData = new WaterCoolData(); - byte[] newbytes = new byte[2]; - bitArray.CopyTo(newbytes, 0); - foreach (var b in newbytes) - { - 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); - } + 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(bytes); + float temperature1Value = coolData.Temperature1.Value; } } diff --git a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs index f0f8a9e..f1c2e6e 100644 --- a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs +++ b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs @@ -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 result = ModbusTcpNet.Read("x=3;" + registerNo, (ushort)length); + if (result.IsSuccess) + { + return result.Content; + } + + return null; + } + public bool WriteValue(ModbusProperty property) { T value = property.Value;