Here's an odd one. Using NModbus, I wrote a
VB.NET (Visual Studio 2010 Pro) application to communicate with some power meters using Modbus/TCP.
Some of these power meters are Modbus RTU devices hanging off of a Lantronix convertor box.
All works fine until I attempt to read a non-responding Modbus ID on one of the Lantronix convertors.
My code has an exception handler which gets the exception, but when I attempt to display the exception text using ex.ToString() or ex.Message, I get an unhandled exception (if I'm not running in the debugger).
When I run in the debugger, the output window shows:
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in Modbus.dll
Here's the simplified code:
Try
Using client As New TcpClient(txtIPAddress.Text, 502)
Dim master As ModbusIpMaster = ModbusIpMaster.CreateIp(client)
usModbusRegisters = master.ReadHoldingRegisters(txtModbusUnitID.Text, nStartingAddress, nDataLength)
DisplayMeterResults()
End Using
Catch ex As Exception
AddToResultsList("Error reading from Modbus IP of " & txtIPAddress.Text, Color.Red)
AddToResultsList("Error was: " & ex.ToString, Color.Red) ' When this line is uncommented, we throw an exception when run outside of the debugger
Exit Sub
End Try
The first message "Error redaing from Modbus IP" gets displayed, but then we get the unhandled exception when trying to access ex.ToString
Here's the exception text:
************** Exception Text **************
System.TypeInitializationException: The type initializer for 'Modbus.Message.SlaveExceptionResponse' threw an exception.
---> System.Resources.MissingManifestResourceException: Could not find any resources appropriate for the specified
culture or the neutral culture. Make sure "Modbus.Resources.resources" was correctly embedded or linked into assembly
"Modbus" at compile time, or that all the satellite assemblies required are loadable and fully signed.
at System.Resources.ManifestBasedResourceGroveler.HandleResourceStreamMissing(String fileName)
at System.Resources.ManifestBasedResourceGroveler.GrovelForResourceSet(CultureInfo culture, Dictionary`2 localResourceSets, Boolean tryParents, Boolean createIfNotExists, StackCrawlMark& stackMark)
at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo requestedCulture, Boolean createIfNotExists, Boolean tryParents, StackCrawlMark& stackMark)
at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents)
at System.Resources.ResourceManager.GetString(String name, CultureInfo culture)
at(Modbus.Resources.get_IllegalFunction())
at(Modbus.Message.SlaveExceptionResponse.CreateExceptionMessages())
at Modbus.Message.SlaveExceptionResponse..cctor()
--- End of inner exception stack trace ---
at(Modbus.Message.SlaveExceptionResponse.ToString())
at System.String.Concat(Object arg0, Object arg1)
at(Modbus.SlaveException.get_Message())
at System.Exception.ToString(Boolean needFileLineInfo)
at(System.Exception.ToString())
at vMeter.frmMain.btnReadMeter_Click(Object sender, EventArgs e) in c:\users\blah-blah-blah\frmMain.vb:line 257
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Any idea why I catch the exception but can't display the message from the exception?
Simply commenting out the line which displays the ex.ToString keeps the unhandled exception from being thrown.
Thanks,
JimE