devmonkZA / nmodbus

Automatically exported from code.google.com/p/nmodbus
0 stars 0 forks source link

Impossible to return error status response #41

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Write a DataStoreWrittenToEventHandler that throws an exception on 
erroneous input values
2. Register the event handler
3. Write erroneous input data via modbus

What is the expected output? What do you see instead?
Expected: A Response with the highest bit of the function code set

Instead the unhandled exception propagates all the way out of 
ModbusTcpSlave.

What version of the product are you using? On what operating system?
NModbus 1.8 on Windows XP professional.

Please provide any additional information below.
Exceptions from the event handlers (also ModbusSlaveRequestEventHandler 
and DataStoreReadFromEventHandler) should be caught, and the response 
function code should have the high order bit set.

Alternatively (or also), it should be possible from the event handlers to 
raise an error flag, that will cause the response to reflect that an error 
was detected, by setting the high order bit of the function code.

By the way,
thanks for NModbus, it is really easy to create the functionality we need!

Best regards,
Henrik R. Scheel
Siemens Wind Power

Original issue reported on code.google.com by langeman...@gmail.com on 17 Dec 2008 at 1:46

GoogleCodeExporter commented 8 years ago
I have coded the necessary changes for a modbus TCP slave to catch exceptions 
caused 
by simple errors in the modbus request package, and consequently return a 
modbus 
exception message (as opposed to terminating the app, as it does now). I also 
wrapped the event rasing code raising the events my app use, so exception 
thrown 
from an eventhandler will cause a modbus exception reply. I even included an 
(untested) event that will be raised when a modbus exception reply is returned, 
it 
might even allowing user code to control the contents of the reply (that was 
the 
original idea, but I never wrote an eventhandler utilizing it, so it may go 
horribly 
wrong if someone tries).

List of changes compared to the install package 
(http://nmodbus.googlecode.com/files/NModbus_net-3.5_1.11.0.0-source.zip):

New files:

src\modbus\Message\ModbusErrorResponse.cs
src\modbus\ModbusException.cs
src\modbus\Device\ModbusExceptionEventArgs.cs

Changed files:
src\modbus\Device\ModbusSlave.cs
src\modbus\Device\ModbusMasterTcpConnection.cs
src\modbus\Message\ModbusMessageImpl.cs
src\modbus\Data\DataStore.cs

src\modbus\modbus.csproj: NOTE: This file is not suitable for use as-is, it has 
been 
modified to suit the needs of my solution. I include it for completeness only. 
Just 
add the 3 new files in the original modbus.csproj, it should work fine.

The files are in the attached zip-file.

I believe similar tweaks can be made to the parts of the code implementing 
other 
types of modbus slaves, but I do not use these myself, so I haven't ventured to 
do 
so. Feel free to take it further :-)

Original comment by hrsch...@gmail.com on 20 Nov 2009 at 3:45

Attachments: