diff --git a/Example/Gateway/Script/View/ViewModbusGateway.cs b/Example/Gateway/Script/View/ViewModbusGateway.cs index c6d5600..babd396 100644 --- a/Example/Gateway/Script/View/ViewModbusGateway.cs +++ b/Example/Gateway/Script/View/ViewModbusGateway.cs @@ -13,13 +13,14 @@ namespace EGFramework.Examples.Gateway{ this.EGEnabledProtocolTool(); this.EGEnabledProtocolTool(); 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{ } 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{ } } 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{ } 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); } diff --git a/addons/EGFramework/Module/ProtocolTools/EGModbus.cs b/addons/EGFramework/Module/ProtocolTools/EGModbus.cs index c9c4fc7..4568c20 100644 --- a/addons/EGFramework/Module/ProtocolTools/EGModbus.cs +++ b/addons/EGFramework/Module/ProtocolTools/EGModbus.cs @@ -35,6 +35,7 @@ namespace EGFramework{ this.EGOnMessage(); } + #region Modbus RTU Operations private bool IsRequestRTU { set; get; } public async Task ReadRTUAsync(ModbusRegisterType registerType,string serialPort,byte deviceAddress,ushort start,ushort count){ if(IsRequestRTU){ @@ -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{ return res; } - public async Task WriteOnceRTUAsync(ModbusRegisterType registerType,string serialPort,byte deviceAddress,ushort registerAddress,ushort value){ + public async Task WriteOnceRTUAsync(ModbusRegisterType registerType,string serialPort,byte deviceAddress,ushort registerAddress,object value){ if(IsRequestRTU){ SendPointerRTU++; int messageId = SendPointerRTU; @@ -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 ()=>{ @@ -135,8 +152,10 @@ namespace EGFramework{ } return res; } + + #endregion - + #region Modbus TCP Operations private bool IsRequestTCP { set; get; } public async Task ReadTCPAsync(ModbusRegisterType registerType,string ipPort,byte deviceAddress,ushort start,ushort count){ if(IsRequestTCP){ @@ -177,7 +196,8 @@ namespace EGFramework{ IsRequestTCP = false; return res; } - + + #endregion public IArchitecture GetArchitecture() { return EGArchitectureImplement.Interface;