From 21c907f2873e6a102babdbb8b65356171392ce05 Mon Sep 17 00:00:00 2001 From: Z Date: Sat, 22 Jun 2024 15:15:11 +0800 Subject: [PATCH] fixed modbus exception code --- .../Gateway/Script/View/ViewModbusGateway.cs | 18 +++++++++++++++--- .../ProtocolExtension/EGModbusExtension.cs | 5 ++++- .../Module/ProtocolTools/EGModbus.cs | 2 +- .../Module/ProtocolTools/EGSerialPort.cs | 18 +++++++++++++----- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/Example/Gateway/Script/View/ViewModbusGateway.cs b/Example/Gateway/Script/View/ViewModbusGateway.cs index 6ba6f97..8bab602 100644 --- a/Example/Gateway/Script/View/ViewModbusGateway.cs +++ b/Example/Gateway/Script/View/ViewModbusGateway.cs @@ -42,7 +42,11 @@ namespace EGFramework.Examples.Gateway{ public async void ReadTest(){ ModbusRTU_Response? result = await this.EGModbus().ReadRTUAsync(ModbusRegisterType.HoldingRegister,"COM4",0x01,0x00,0x01); if(result != null){ - GD.Print("Register[0]"+((ModbusRTU_Response)result).HoldingRegister[0]); + if(!((ModbusRTU_Response)result).IsError){ + GD.Print("Register[0]"+((ModbusRTU_Response)result).HoldingRegister[0]); + }else{ + GD.Print("Error:"+((ModbusRTU_Response)result).ErrorCode); + } }else{ GD.Print("Timeout!"); } @@ -51,7 +55,11 @@ namespace EGFramework.Examples.Gateway{ public async void ReadTest2(){ ModbusRTU_Response? result2 = await this.EGModbus().ReadRTUAsync(ModbusRegisterType.HoldingRegister,"COM4",0x01,0x01,0x01); if(result2 != null){ - GD.Print("Register[1]"+((ModbusRTU_Response)result2).HoldingRegister[0]); + if(!((ModbusRTU_Response)result2).IsError){ + GD.Print("Register[1]"+((ModbusRTU_Response)result2).HoldingRegister[0]); + }else{ + GD.Print("Error:"+((ModbusRTU_Response)result2).ErrorCode); + } }else{ GD.Print("Timeout!"); } @@ -59,7 +67,11 @@ namespace EGFramework.Examples.Gateway{ public async void ReadTest3(){ ModbusRTU_Response? result3 = await this.EGModbus().ReadRTUAsync(ModbusRegisterType.HoldingRegister,"COM4",0x01,0x10,0x01); if(result3 != null){ - GD.Print("Register[2]"+((ModbusRTU_Response)result3).HoldingRegister[0]); + if(!((ModbusRTU_Response)result3).IsError){ + GD.Print("Register[2]"+((ModbusRTU_Response)result3).HoldingRegister[0]); + }else{ + GD.Print("Error:"+((ModbusRTU_Response)result3).ErrorCode); + } }else{ GD.Print("Timeout!"); } diff --git a/addons/EGFramework/Module/ProtocolExtension/EGModbusExtension.cs b/addons/EGFramework/Module/ProtocolExtension/EGModbusExtension.cs index 6ea564d..00fb5a5 100644 --- a/addons/EGFramework/Module/ProtocolExtension/EGModbusExtension.cs +++ b/addons/EGFramework/Module/ProtocolExtension/EGModbusExtension.cs @@ -377,7 +377,7 @@ namespace EGFramework{ public byte[] SourceData { set; get; } public ModbusFunctionType FunctionType { set; get; } public ModbusErrorCode ErrorCode { set; get; } - + public bool IsError { set; get; } public bool TrySetData(string protocolData, byte[] protocolBytes) { try @@ -391,6 +391,7 @@ namespace EGFramework{ if(FunctionCode == 0x83){ ErrorCode = (ModbusErrorCode)protocolBytes[2]; + IsError = true; return true; } byte[] dataLength = new byte[4]; @@ -763,6 +764,7 @@ namespace EGFramework{ public byte[] SourceData { set; get; } public ModbusFunctionType FunctionType { set; get; } public ModbusErrorCode ErrorCode { set; get; } + public bool IsError { set; get; } public bool TrySetData(string protocolData, byte[] protocolBytes) { @@ -777,6 +779,7 @@ namespace EGFramework{ FunctionType = (ModbusFunctionType)protocolBytes[1]; if(FunctionCode == 0x83){ ErrorCode = (ModbusErrorCode)protocolBytes[2]; + IsError = true; return true; } diff --git a/addons/EGFramework/Module/ProtocolTools/EGModbus.cs b/addons/EGFramework/Module/ProtocolTools/EGModbus.cs index 586e6ce..abf7d98 100644 --- a/addons/EGFramework/Module/ProtocolTools/EGModbus.cs +++ b/addons/EGFramework/Module/ProtocolTools/EGModbus.cs @@ -62,7 +62,7 @@ namespace EGFramework{ // this.AppendMessage("【发送-"+DataModbusItem.SerialPort+"】 "+ReadRequest.ToProtocolByteData().ToStringByHex()); this.EGSendMessage(ReadRequest,serialPort,ProtocolType.SerialPort); // this.EGSerialPort().SetExpectReceivedDataLength(5+count*2); - this.EGSerialPort().SetExpectReceivedDataLength(5); + this.EGSerialPort().SetExpectReceivedDataLength(5+count*2); break; } await Task.Run(async ()=>{ diff --git a/addons/EGFramework/Module/ProtocolTools/EGSerialPort.cs b/addons/EGFramework/Module/ProtocolTools/EGSerialPort.cs index 51224a6..21db30a 100644 --- a/addons/EGFramework/Module/ProtocolTools/EGSerialPort.cs +++ b/addons/EGFramework/Module/ProtocolTools/EGSerialPort.cs @@ -23,6 +23,8 @@ namespace EGFramework{ public Queue ResponseMsgs { set; get; } = new Queue(); + public byte[] ReceivedCache { set; get; } = new byte[0]; + public void Init() { this.EGRegisterSendAction(request=>{ @@ -170,17 +172,23 @@ namespace EGFramework{ { //await Task.Run(() => {}).ConfigureAwait(false); SerialPort serialPort = (SerialPort)sender; - if(serialPort.BytesToRead >= MinDataPackLength){ + if(serialPort.BytesToRead >= 0){ int bufferSize = serialPort.BytesToRead; byte[] buffer = new byte[bufferSize]; serialPort.Read(buffer,0,serialPort.BytesToRead); - string str = StringEncoding.GetString(buffer); - Godot.GD.Print("[Receive]"+buffer.ToStringByHex()); - ResponseMsgs.Enqueue(new ResponseMsg(str,buffer,serialPort.PortName,ProtocolType.SerialPort)); + ReceivedCache = ReceivedCache.Concat(buffer).ToArray(); + } + if(ReceivedCache.Length >= MinDataPackLength){ + string str = StringEncoding.GetString(ReceivedCache); + Godot.GD.Print("[Receive]"+ReceivedCache.ToStringByHex()); + ResponseMsgs.Enqueue(new ResponseMsg(str,ReceivedCache,serialPort.PortName,ProtocolType.SerialPort)); + ReceivedCache = new byte[0]; MinDataPackLength = 0; //this.EGOnReceivedData(new ResponseMsg(str,buffer,serialPort.PortName,ProtocolType.SerialPort)); }else{ - Godot.GD.Print("[Data Get]" + serialPort.BytesToRead); + Godot.GD.Print("[Data Get]" + ReceivedCache.Length); + string str = StringEncoding.GetString(ReceivedCache); + ResponseMsgs.Enqueue(new ResponseMsg(str,ReceivedCache,serialPort.PortName,ProtocolType.SerialPort)); } }