diff --git a/Example/Gateway/ModbusGateway.tscn b/Example/Gateway/ModbusGateway.tscn index 64c17d9..ff4fb77 100644 --- a/Example/Gateway/ModbusGateway.tscn +++ b/Example/Gateway/ModbusGateway.tscn @@ -13,6 +13,5 @@ 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/View/ViewModbusGateway.cs b/Example/Gateway/Script/View/ViewModbusGateway.cs index 1398d10..a1ff9f4 100644 --- a/Example/Gateway/Script/View/ViewModbusGateway.cs +++ b/Example/Gateway/Script/View/ViewModbusGateway.cs @@ -124,6 +124,19 @@ namespace EGFramework.Examples.Gateway{ } } + public async void ReadTestTCP(ushort address){ + ModbusTCP_Response? result3 = await this.EGModbus().ReadTCPAsync(ModbusRegisterType.HoldingRegister,"192.168.1.170:8234",0x01,address,0x01); + if(result3 != null){ + if(!((ModbusTCP_Response)result3).IsError){ + GD.Print("Register"+((ModbusTCP_Response)result3).HoldingRegister[0]); + }else{ + GD.Print("Error:"+((ModbusTCP_Response)result3).ErrorCode); + } + }else{ + GD.Print("Timeout!"); + } + } + public async void WriteTest1(){ ModbusRTU_Response? result = await this.EGModbus().WriteOnceRTUAsync(ModbusRegisterType.HoldingRegister,"COM4",0x01,0x02,(ushort)0x50); if(result != null){ @@ -149,6 +162,19 @@ namespace EGFramework.Examples.Gateway{ GD.Print("Timeout!"); } } + + public async void WriteTestTCP(){ + ModbusTCP_Response? result = await this.EGModbus().WriteOnceTCPAsync(ModbusRegisterType.HoldingRegister,"192.168.1.170:8234",0x01,2005,(ushort)20); + if(result != null){ + if(!((ModbusTCP_Response)result).IsError){ + GD.Print("Write"+((ModbusTCP_Response)result).FunctionType); + }else{ + GD.Print("Error:"+((ModbusTCP_Response)result).ErrorCode); + } + }else{ + GD.Print("Timeout!"); + } + } } } diff --git a/addons/EGFramework/Module/ProtocolTools/EGModbus.cs b/addons/EGFramework/Module/ProtocolTools/EGModbus.cs index 4568c20..434a995 100644 --- a/addons/EGFramework/Module/ProtocolTools/EGModbus.cs +++ b/addons/EGFramework/Module/ProtocolTools/EGModbus.cs @@ -17,6 +17,10 @@ namespace EGFramework{ public int NextSendRTU = 0; public int SendPointerRTU = 1; + public Queue WaitForSendTCP = new Queue(); + public int NextSendTCP = 0; + public int SendPointerTCP = 1; + public EasyEvent OnReadTimeOut = new EasyEvent(); public void Init() @@ -159,14 +163,18 @@ namespace EGFramework{ private bool IsRequestTCP { set; get; } public async Task ReadTCPAsync(ModbusRegisterType registerType,string ipPort,byte deviceAddress,ushort start,ushort count){ if(IsRequestTCP){ + SendPointerTCP++; + int messageId = SendPointerTCP; + WaitForSendTCP.Enqueue(messageId); await Task.Run(async () => { - while (IsRequestTCP) + while (IsRequestTCP || NextSendTCP != messageId) { await Task.Delay(10); //break; } }); + GD.Print("-----Read"+messageId+" ----"); //return null; } TCPCache.Clear(); @@ -194,9 +202,64 @@ namespace EGFramework{ } }); IsRequestTCP = false; + if(this.WaitForSendTCP.Count>0){ + NextSendTCP = this.WaitForSendTCP.Dequeue(); + } return res; } + public async Task WriteOnceTCPAsync(ModbusRegisterType registerType,string serialPort,byte deviceAddress,ushort registerAddress,object value){ + if(IsRequestTCP){ + SendPointerTCP++; + int messageId = SendPointerTCP; + WaitForSendTCP.Enqueue(messageId); + await Task.Run(async () => + { + while (IsRequestTCP || NextSendTCP != messageId) + { + await Task.Delay(10); + //break; + } + }); + GD.Print("-----Write"+messageId+" ----"); + //return null; + } + TCPCache.Clear(); + IsRequestTCP = true; + IRequest WriteRequest; + ModbusTCP_Response? res = null; + switch(registerType){ + case ModbusRegisterType.Coil: + WriteRequest = new ModbusTCP_WriteSingleCoil(deviceAddress,registerAddress,(bool)value); + this.EGSendMessage(WriteRequest,serialPort,ProtocolType.TCPClient); + break; + case ModbusRegisterType.HoldingRegister: + WriteRequest = new ModbusTCP_WriteSingleHoldingRegister(deviceAddress,registerAddress,(ushort)value); + this.EGSendMessage(WriteRequest,serialPort,ProtocolType.TCPClient); + break; + } + await Task.Run(async ()=>{ + int timeout = 0; + while(TCPCache.Count==0 && timeout < Delay){ + await Task.Delay(10); + timeout+=10; + } + if(TCPCache.Count>0){ + res = TCPCache.Dequeue(); + }else{ + //Print Error Timeout + OnReadTimeOut.Invoke(); + } + }); + IsRequestTCP = false; + if(this.WaitForSendTCP.Count>0){ + NextSendTCP = this.WaitForSendTCP.Dequeue(); + } + if(this.WaitForSendTCP.Count>0){ + NextSendTCP = this.WaitForSendTCP.Dequeue(); + } + return res; + } #endregion public IArchitecture GetArchitecture() {