Browse Source

fixed modbus exception code

master
Z 5 months ago
parent
commit
21c907f287
  1. 12
      Example/Gateway/Script/View/ViewModbusGateway.cs
  2. 5
      addons/EGFramework/Module/ProtocolExtension/EGModbusExtension.cs
  3. 2
      addons/EGFramework/Module/ProtocolTools/EGModbus.cs
  4. 18
      addons/EGFramework/Module/ProtocolTools/EGSerialPort.cs

12
Example/Gateway/Script/View/ViewModbusGateway.cs

@ -42,7 +42,11 @@ namespace EGFramework.Examples.Gateway{ @@ -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){
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{ @@ -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){
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{ @@ -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){
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!");
}

5
addons/EGFramework/Module/ProtocolExtension/EGModbusExtension.cs

@ -377,7 +377,7 @@ namespace EGFramework{ @@ -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{ @@ -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{ @@ -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{ @@ -777,6 +779,7 @@ namespace EGFramework{
FunctionType = (ModbusFunctionType)protocolBytes[1];
if(FunctionCode == 0x83){
ErrorCode = (ModbusErrorCode)protocolBytes[2];
IsError = true;
return true;
}

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

@ -62,7 +62,7 @@ namespace EGFramework{ @@ -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 ()=>{

18
addons/EGFramework/Module/ProtocolTools/EGSerialPort.cs

@ -23,6 +23,8 @@ namespace EGFramework{ @@ -23,6 +23,8 @@ namespace EGFramework{
public Queue<ResponseMsg> ResponseMsgs { set; get; } = new Queue<ResponseMsg>();
public byte[] ReceivedCache { set; get; } = new byte[0];
public void Init()
{
this.EGRegisterSendAction(request=>{
@ -170,17 +172,23 @@ namespace EGFramework{ @@ -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));
}
}

Loading…
Cancel
Save