From 061a6648013fd1122aa7a3005bbc583e551e8802 Mon Sep 17 00:00:00 2001 From: Z Date: Mon, 24 Jun 2024 10:25:32 +0800 Subject: [PATCH] add tcp test --- Example/Gateway/ModbusGateway.tscn | 6 ++ .../Script/Data/DataModbusGatewaySettings.cs | 10 ++-- .../Gateway/Script/View/ViewModbusGateway.cs | 59 +++++++++++++++---- 3 files changed, 58 insertions(+), 17 deletions(-) diff --git a/Example/Gateway/ModbusGateway.tscn b/Example/Gateway/ModbusGateway.tscn index 58b9d20..64c17d9 100644 --- a/Example/Gateway/ModbusGateway.tscn +++ b/Example/Gateway/ModbusGateway.tscn @@ -10,3 +10,9 @@ anchor_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 script = ExtResource("1_34uaa") + +[node name="Timer" type="Timer" parent="."] +wait_time = 2.0 +autostart = true + +[connection signal="timeout" from="Timer" to="." method="PushDataToGateway"] diff --git a/Example/Gateway/Script/Data/DataModbusGatewaySettings.cs b/Example/Gateway/Script/Data/DataModbusGatewaySettings.cs index 0046ef2..e3af024 100644 --- a/Example/Gateway/Script/Data/DataModbusGatewaySettings.cs +++ b/Example/Gateway/Script/Data/DataModbusGatewaySettings.cs @@ -4,8 +4,8 @@ using System.Collections.Generic; namespace EGFramework.Examples.Gateway{ public class DataModbusGatewaySetting{ public float Delay { set; get; } - public List Devices485 = new List(); - public List DevicesTCP = new List(); + public Dictionary Devices485 = new Dictionary(); + public Dictionary DevicesTCP = new Dictionary(); } public class DataModbus485Device{ public string SerialPort { set; get; } @@ -21,10 +21,10 @@ namespace EGFramework.Examples.Gateway{ public Dictionary Registers = new Dictionary(); } public class DataModbusRegister{ - public short Address { set; get; } + public ushort Address { set; get; } public ModbusRegisterType RegisterType { set; get; } = ModbusRegisterType.HoldingRegister; - public string Info { set; get; } - public string Unit { set; get; } + public string Name { set; get; } + // public string Unit { set; get; } } diff --git a/Example/Gateway/Script/View/ViewModbusGateway.cs b/Example/Gateway/Script/View/ViewModbusGateway.cs index babd396..be3aa87 100644 --- a/Example/Gateway/Script/View/ViewModbusGateway.cs +++ b/Example/Gateway/Script/View/ViewModbusGateway.cs @@ -1,7 +1,12 @@ using EGFramework; using EGFramework.Examples.Gateway; using Godot; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System; +using System.Collections.Generic; +using System.Text.Json.Nodes; + namespace EGFramework.Examples.Gateway{ public partial class ViewModbusGateway : Control,IEGFramework,IGateway { @@ -10,17 +15,7 @@ namespace EGFramework.Examples.Gateway{ // Called when the node enters the scene tree for the first time. public override void _Ready() { - this.EGEnabledProtocolTool(); - this.EGEnabledProtocolTool(); - this.EGSerialPort().SetBaudRate(9600); - // ReadTest(); - // ReadTest2(); - // ReadTest3(); - // ReadTest3(); - // ReadTest2(); - // ReadTest(); - WriteTest1(); - WriteTest2(); + InitGateway(); } // Called every frame. 'delta' is the elapsed time since the previous frame. @@ -31,15 +26,55 @@ namespace EGFramework.Examples.Gateway{ public void InitGateway() { if(this.EGSave().GetDataByFile() == null){ - Setting = new DataModbusGatewaySetting(); + InitSettings(); this.EGSave().SetDataToFile(Setting); }else{ Setting = this.EGSave().GetDataByFile(); } this.EGEnabledProtocolTool(); this.EGEnabledProtocolTool(); + this.EGSerialPort().SetBaudRate(9600); //this.EGOnMessage(); } + + public void InitSettings(){ + Setting = new DataModbusGatewaySetting(); + Setting.Delay = 1.0f; + Setting.Devices485.Add("COM4",new DataModbus485Device(){ + SerialPort = "COM4", + Address = 0x01, + BaudRate = 9600 + }); + Setting.Devices485["COM4"].Registers.Add("表头读数",new DataModbusRegister(){ + Address = 0x00, + RegisterType = ModbusRegisterType.HoldingRegister, + Name = "表头读数" + }); + } + + public async void PushDataToGateway(){ + JObject pushData = new JObject(); + foreach(KeyValuePair device485 in Setting.Devices485){ + foreach(KeyValuePair register in Setting.Devices485[device485.Key].Registers){ + ModbusRTU_Response? result = await this.EGModbus().ReadRTUAsync(register.Value.RegisterType,device485.Key,device485.Value.Address,register.Value.Address,0x01); + if(result != null){ + if(!((ModbusRTU_Response)result).IsError){ + if(register.Value.RegisterType == ModbusRegisterType.HoldingRegister){ + pushData.Add(register.Key,((ModbusRTU_Response)result).HoldingRegister[0]); + } + }else{ + GD.Print("Error:"+((ModbusRTU_Response)result).ErrorCode); + } + }else{ + GD.Print("Timeout!"); + } + } + } + string resultJson = JsonConvert.SerializeObject(pushData,Formatting.Indented); + GD.Print(resultJson); + + this.EGTCPClient().SendStringData("192.168.1.170",5501,resultJson); + } public async void ReadTest(){ ModbusRTU_Response? result = await this.EGModbus().ReadRTUAsync(ModbusRegisterType.HoldingRegister,"COM4",0x01,0x00,0x01);