Browse Source

add read type and write once type

master
Z 5 months ago
parent
commit
ba6de689b1
  1. 34
      Example/Gateway/Script/View/ViewModbusGateway.cs
  2. 38
      addons/EGFramework/Module/ProtocolTools/EGModbus.cs

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

@ -13,13 +13,14 @@ namespace EGFramework.Examples.Gateway{ @@ -13,13 +13,14 @@ namespace EGFramework.Examples.Gateway{
this.EGEnabledProtocolTool<EGSerialPort>();
this.EGEnabledProtocolTool<EGTCPClient>();
this.EGSerialPort().SetBaudRate(9600);
ReadTest();
ReadTest2();
ReadTest3();
ReadTest3();
// ReadTest();
// ReadTest2();
// ReadTest3();
// ReadTest3();
// ReadTest2();
// ReadTest();
WriteTest1();
WriteTest2();
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
@ -54,10 +55,10 @@ namespace EGFramework.Examples.Gateway{ @@ -54,10 +55,10 @@ namespace EGFramework.Examples.Gateway{
}
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.Coil,"COM4",0x01,0x01,0x01);
if(result2 != null){
if(!((ModbusRTU_Response)result2).IsError){
GD.Print("Register[1]"+((ModbusRTU_Response)result2).HoldingRegister[0]);
GD.Print("Register[1]"+((ModbusRTU_Response)result2).Coil[0]);
}else{
GD.Print("Error:"+((ModbusRTU_Response)result2).ErrorCode);
}
@ -66,10 +67,10 @@ namespace EGFramework.Examples.Gateway{ @@ -66,10 +67,10 @@ namespace EGFramework.Examples.Gateway{
}
}
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.DiscreteInput,"COM4",0x01,0x01,0x01);
if(result3 != null){
if(!((ModbusRTU_Response)result3).IsError){
GD.Print("Register[2]"+((ModbusRTU_Response)result3).HoldingRegister[0]);
GD.Print("Register[2]"+((ModbusRTU_Response)result3).DiscreteInput[0]);
}else{
GD.Print("Error:"+((ModbusRTU_Response)result3).ErrorCode);
}
@ -79,10 +80,23 @@ namespace EGFramework.Examples.Gateway{ @@ -79,10 +80,23 @@ namespace EGFramework.Examples.Gateway{
}
public async void WriteTest1(){
ModbusRTU_Response? result = await this.EGModbus().WriteOnceRTUAsync(ModbusRegisterType.HoldingRegister,"COM4",0x01,0x2000,0x50);
ModbusRTU_Response? result = await this.EGModbus().WriteOnceRTUAsync(ModbusRegisterType.HoldingRegister,"COM4",0x01,0x02,(ushort)0x50);
if(result != null){
if(!((ModbusRTU_Response)result).IsError){
GD.Print("Write[1]"+((ModbusRTU_Response)result).FunctionType);
}else{
GD.Print("Error:"+((ModbusRTU_Response)result).ErrorCode);
}
}else{
GD.Print("Timeout!");
}
}
public async void WriteTest2(){
ModbusRTU_Response? result = await this.EGModbus().WriteOnceRTUAsync(ModbusRegisterType.Coil,"COM4",0x01,0x02,true);
if(result != null){
if(!((ModbusRTU_Response)result).IsError){
GD.Print("Write[0]"+((ModbusRTU_Response)result).FunctionType);
GD.Print("Write[2]"+((ModbusRTU_Response)result).FunctionType);
}else{
GD.Print("Error:"+((ModbusRTU_Response)result).ErrorCode);
}

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

@ -35,6 +35,7 @@ namespace EGFramework{ @@ -35,6 +35,7 @@ namespace EGFramework{
this.EGOnMessage<ModbusTCP_Response>();
}
#region Modbus RTU Operations
private bool IsRequestRTU { set; get; }
public async Task<ModbusRTU_Response?> ReadRTUAsync(ModbusRegisterType registerType,string serialPort,byte deviceAddress,ushort start,ushort count){
if(IsRequestRTU){
@ -57,11 +58,24 @@ namespace EGFramework{ @@ -57,11 +58,24 @@ namespace EGFramework{
IRequest ReadRequest;
ModbusRTU_Response? res = null;
switch(registerType){
case ModbusRegisterType.Coil:
ReadRequest = new ModbusRTU_ReadCoils(deviceAddress,start,count);
this.EGSendMessage(ReadRequest,serialPort,ProtocolType.SerialPort);
this.EGSerialPort().SetExpectReceivedDataLength(6+count/8);
break;
case ModbusRegisterType.DiscreteInput:
ReadRequest = new ModbusRTU_ReadDiscreteInput(deviceAddress,start,count);
this.EGSendMessage(ReadRequest,serialPort,ProtocolType.SerialPort);
this.EGSerialPort().SetExpectReceivedDataLength(6+count/8);
break;
case ModbusRegisterType.HoldingRegister:
ReadRequest = new ModbusRTU_ReadHoldingRegisters(deviceAddress,start,count);
// this.AppendMessage("【发送-"+DataModbusItem.SerialPort+"】 "+ReadRequest.ToProtocolByteData().ToStringByHex());
this.EGSendMessage(ReadRequest,serialPort,ProtocolType.SerialPort);
// this.EGSerialPort().SetExpectReceivedDataLength(5+count*2);
this.EGSerialPort().SetExpectReceivedDataLength(5+count*2);
break;
case ModbusRegisterType.InputRegisters:
ReadRequest = new ModbusRTU_ReadInputRegisters(deviceAddress,start,count);
this.EGSendMessage(ReadRequest,serialPort,ProtocolType.SerialPort);
this.EGSerialPort().SetExpectReceivedDataLength(5+count*2);
break;
}
@ -86,7 +100,7 @@ namespace EGFramework{ @@ -86,7 +100,7 @@ namespace EGFramework{
return res;
}
public async Task<ModbusRTU_Response?> WriteOnceRTUAsync(ModbusRegisterType registerType,string serialPort,byte deviceAddress,ushort registerAddress,ushort value){
public async Task<ModbusRTU_Response?> WriteOnceRTUAsync(ModbusRegisterType registerType,string serialPort,byte deviceAddress,ushort registerAddress,object value){
if(IsRequestRTU){
SendPointerRTU++;
int messageId = SendPointerRTU;
@ -104,15 +118,18 @@ namespace EGFramework{ @@ -104,15 +118,18 @@ namespace EGFramework{
}
RTUCache.Clear();
IsRequestRTU = true;
IRequest ReadRequest;
IRequest WriteRequest;
ModbusRTU_Response? res = null;
switch(registerType){
case ModbusRegisterType.Coil:
WriteRequest = new ModbusRTU_WriteSingleCoil(deviceAddress,registerAddress,(bool)value);
this.EGSendMessage(WriteRequest,serialPort,ProtocolType.SerialPort);
this.EGSerialPort().SetExpectReceivedDataLength(WriteRequest.ToProtocolByteData().Length);
break;
case ModbusRegisterType.HoldingRegister:
ReadRequest = new ModbusRTU_WriteSingleHoldingRegister(deviceAddress,registerAddress,value);
// this.AppendMessage("【发送-"+DataModbusItem.SerialPort+"】 "+ReadRequest.ToProtocolByteData().ToStringByHex());
this.EGSendMessage(ReadRequest,serialPort,ProtocolType.SerialPort);
// this.EGSerialPort().SetExpectReceivedDataLength(5+count*2);
this.EGSerialPort().SetExpectReceivedDataLength(ReadRequest.ToProtocolByteData().Length);
WriteRequest = new ModbusRTU_WriteSingleHoldingRegister(deviceAddress,registerAddress,(ushort)value);
this.EGSendMessage(WriteRequest,serialPort,ProtocolType.SerialPort);
this.EGSerialPort().SetExpectReceivedDataLength(WriteRequest.ToProtocolByteData().Length);
break;
}
await Task.Run(async ()=>{
@ -136,7 +153,9 @@ namespace EGFramework{ @@ -136,7 +153,9 @@ namespace EGFramework{
return res;
}
#endregion
#region Modbus TCP Operations
private bool IsRequestTCP { set; get; }
public async Task<ModbusTCP_Response?> ReadTCPAsync(ModbusRegisterType registerType,string ipPort,byte deviceAddress,ushort start,ushort count){
if(IsRequestTCP){
@ -178,6 +197,7 @@ namespace EGFramework{ @@ -178,6 +197,7 @@ namespace EGFramework{
return res;
}
#endregion
public IArchitecture GetArchitecture()
{
return EGArchitectureImplement.Interface;

Loading…
Cancel
Save