Browse Source

fixed modbus exception code

master
Z 5 months ago
parent
commit
21c907f287
  1. 18
      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

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

@ -42,7 +42,11 @@ namespace EGFramework.Examples.Gateway{
public async void ReadTest(){ public async void ReadTest(){
ModbusRTU_Response? result = await this.EGModbus().ReadRTUAsync(ModbusRegisterType.HoldingRegister,"COM4",0x01,0x00,0x01); ModbusRTU_Response? result = await this.EGModbus().ReadRTUAsync(ModbusRegisterType.HoldingRegister,"COM4",0x01,0x00,0x01);
if(result != null){ 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{ }else{
GD.Print("Timeout!"); GD.Print("Timeout!");
} }
@ -51,7 +55,11 @@ namespace EGFramework.Examples.Gateway{
public async void ReadTest2(){ public async void ReadTest2(){
ModbusRTU_Response? result2 = await this.EGModbus().ReadRTUAsync(ModbusRegisterType.HoldingRegister,"COM4",0x01,0x01,0x01); ModbusRTU_Response? result2 = await this.EGModbus().ReadRTUAsync(ModbusRegisterType.HoldingRegister,"COM4",0x01,0x01,0x01);
if(result2 != null){ 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{ }else{
GD.Print("Timeout!"); GD.Print("Timeout!");
} }
@ -59,7 +67,11 @@ namespace EGFramework.Examples.Gateway{
public async void ReadTest3(){ public async void ReadTest3(){
ModbusRTU_Response? result3 = await this.EGModbus().ReadRTUAsync(ModbusRegisterType.HoldingRegister,"COM4",0x01,0x10,0x01); ModbusRTU_Response? result3 = await this.EGModbus().ReadRTUAsync(ModbusRegisterType.HoldingRegister,"COM4",0x01,0x10,0x01);
if(result3 != null){ 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{ }else{
GD.Print("Timeout!"); GD.Print("Timeout!");
} }

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

@ -377,7 +377,7 @@ namespace EGFramework{
public byte[] SourceData { set; get; } public byte[] SourceData { set; get; }
public ModbusFunctionType FunctionType { set; get; } public ModbusFunctionType FunctionType { set; get; }
public ModbusErrorCode ErrorCode { set; get; } public ModbusErrorCode ErrorCode { set; get; }
public bool IsError { set; get; }
public bool TrySetData(string protocolData, byte[] protocolBytes) public bool TrySetData(string protocolData, byte[] protocolBytes)
{ {
try try
@ -391,6 +391,7 @@ namespace EGFramework{
if(FunctionCode == 0x83){ if(FunctionCode == 0x83){
ErrorCode = (ModbusErrorCode)protocolBytes[2]; ErrorCode = (ModbusErrorCode)protocolBytes[2];
IsError = true;
return true; return true;
} }
byte[] dataLength = new byte[4]; byte[] dataLength = new byte[4];
@ -763,6 +764,7 @@ namespace EGFramework{
public byte[] SourceData { set; get; } public byte[] SourceData { set; get; }
public ModbusFunctionType FunctionType { set; get; } public ModbusFunctionType FunctionType { set; get; }
public ModbusErrorCode ErrorCode { set; get; } public ModbusErrorCode ErrorCode { set; get; }
public bool IsError { set; get; }
public bool TrySetData(string protocolData, byte[] protocolBytes) public bool TrySetData(string protocolData, byte[] protocolBytes)
{ {
@ -777,6 +779,7 @@ namespace EGFramework{
FunctionType = (ModbusFunctionType)protocolBytes[1]; FunctionType = (ModbusFunctionType)protocolBytes[1];
if(FunctionCode == 0x83){ if(FunctionCode == 0x83){
ErrorCode = (ModbusErrorCode)protocolBytes[2]; ErrorCode = (ModbusErrorCode)protocolBytes[2];
IsError = true;
return true; return true;
} }

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

@ -62,7 +62,7 @@ namespace EGFramework{
// this.AppendMessage("【发送-"+DataModbusItem.SerialPort+"】 "+ReadRequest.ToProtocolByteData().ToStringByHex()); // this.AppendMessage("【发送-"+DataModbusItem.SerialPort+"】 "+ReadRequest.ToProtocolByteData().ToStringByHex());
this.EGSendMessage(ReadRequest,serialPort,ProtocolType.SerialPort); this.EGSendMessage(ReadRequest,serialPort,ProtocolType.SerialPort);
// this.EGSerialPort().SetExpectReceivedDataLength(5+count*2); // this.EGSerialPort().SetExpectReceivedDataLength(5+count*2);
this.EGSerialPort().SetExpectReceivedDataLength(5); this.EGSerialPort().SetExpectReceivedDataLength(5+count*2);
break; break;
} }
await Task.Run(async ()=>{ await Task.Run(async ()=>{

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

@ -23,6 +23,8 @@ namespace EGFramework{
public Queue<ResponseMsg> ResponseMsgs { set; get; } = new Queue<ResponseMsg>(); public Queue<ResponseMsg> ResponseMsgs { set; get; } = new Queue<ResponseMsg>();
public byte[] ReceivedCache { set; get; } = new byte[0];
public void Init() public void Init()
{ {
this.EGRegisterSendAction(request=>{ this.EGRegisterSendAction(request=>{
@ -170,17 +172,23 @@ namespace EGFramework{
{ {
//await Task.Run(() => {}).ConfigureAwait(false); //await Task.Run(() => {}).ConfigureAwait(false);
SerialPort serialPort = (SerialPort)sender; SerialPort serialPort = (SerialPort)sender;
if(serialPort.BytesToRead >= MinDataPackLength){ if(serialPort.BytesToRead >= 0){
int bufferSize = serialPort.BytesToRead; int bufferSize = serialPort.BytesToRead;
byte[] buffer = new byte[bufferSize]; byte[] buffer = new byte[bufferSize];
serialPort.Read(buffer,0,serialPort.BytesToRead); serialPort.Read(buffer,0,serialPort.BytesToRead);
string str = StringEncoding.GetString(buffer); ReceivedCache = ReceivedCache.Concat(buffer).ToArray();
Godot.GD.Print("[Receive]"+buffer.ToStringByHex()); }
ResponseMsgs.Enqueue(new ResponseMsg(str,buffer,serialPort.PortName,ProtocolType.SerialPort)); 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; MinDataPackLength = 0;
//this.EGOnReceivedData(new ResponseMsg(str,buffer,serialPort.PortName,ProtocolType.SerialPort)); //this.EGOnReceivedData(new ResponseMsg(str,buffer,serialPort.PortName,ProtocolType.SerialPort));
}else{ }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