Browse Source

fixed gateway

master
Z 5 months ago
parent
commit
aafec00e0b
  1. 1
      Example/Gateway/ModbusGateway.tscn
  2. 26
      Example/Gateway/Script/View/ViewModbusGateway.cs
  3. 65
      addons/EGFramework/Module/ProtocolTools/EGModbus.cs

1
Example/Gateway/ModbusGateway.tscn

@ -13,6 +13,5 @@ script = ExtResource("1_34uaa")
[node name="Timer" type="Timer" parent="."] [node name="Timer" type="Timer" parent="."]
wait_time = 2.0 wait_time = 2.0
autostart = true
[connection signal="timeout" from="Timer" to="." method="PushDataToGateway"] [connection signal="timeout" from="Timer" to="." method="PushDataToGateway"]

26
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(){ public async void WriteTest1(){
ModbusRTU_Response? result = await this.EGModbus().WriteOnceRTUAsync(ModbusRegisterType.HoldingRegister,"COM4",0x01,0x02,(ushort)0x50); ModbusRTU_Response? result = await this.EGModbus().WriteOnceRTUAsync(ModbusRegisterType.HoldingRegister,"COM4",0x01,0x02,(ushort)0x50);
if(result != null){ if(result != null){
@ -149,6 +162,19 @@ namespace EGFramework.Examples.Gateway{
GD.Print("Timeout!"); 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!");
}
}
} }
} }

65
addons/EGFramework/Module/ProtocolTools/EGModbus.cs

@ -17,6 +17,10 @@ namespace EGFramework{
public int NextSendRTU = 0; public int NextSendRTU = 0;
public int SendPointerRTU = 1; public int SendPointerRTU = 1;
public Queue<int> WaitForSendTCP = new Queue<int>();
public int NextSendTCP = 0;
public int SendPointerTCP = 1;
public EasyEvent OnReadTimeOut = new EasyEvent(); public EasyEvent OnReadTimeOut = new EasyEvent();
public void Init() public void Init()
@ -159,14 +163,18 @@ namespace EGFramework{
private bool IsRequestTCP { set; get; } private bool IsRequestTCP { set; get; }
public async Task<ModbusTCP_Response?> ReadTCPAsync(ModbusRegisterType registerType,string ipPort,byte deviceAddress,ushort start,ushort count){ public async Task<ModbusTCP_Response?> ReadTCPAsync(ModbusRegisterType registerType,string ipPort,byte deviceAddress,ushort start,ushort count){
if(IsRequestTCP){ if(IsRequestTCP){
SendPointerTCP++;
int messageId = SendPointerTCP;
WaitForSendTCP.Enqueue(messageId);
await Task.Run(async () => await Task.Run(async () =>
{ {
while (IsRequestTCP) while (IsRequestTCP || NextSendTCP != messageId)
{ {
await Task.Delay(10); await Task.Delay(10);
//break; //break;
} }
}); });
GD.Print("-----Read"+messageId+" ----");
//return null; //return null;
} }
TCPCache.Clear(); TCPCache.Clear();
@ -194,9 +202,64 @@ namespace EGFramework{
} }
}); });
IsRequestTCP = false; IsRequestTCP = false;
if(this.WaitForSendTCP.Count>0){
NextSendTCP = this.WaitForSendTCP.Dequeue();
}
return res; return res;
} }
public async Task<ModbusTCP_Response?> 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 #endregion
public IArchitecture GetArchitecture() public IArchitecture GetArchitecture()
{ {

Loading…
Cancel
Save