Handling RMCLink.Interop .NET Assembly Errors |
The RMCLink object uses .NET exceptions to report unexpected errors. Each RMCLink method's documentation lists the exceptions that it may throw. It should be pointed out that while many of these exceptions can be avoided while the program is being developed (e.g. ArgumentOutOfRangeException), some can simply not be avoided at runtime. For example, if the communication cable is disconnected while a read or write is in progress, the communication will fail unexpectedly. Therefore, it is important that applications using RMCLink handle, at a minimum, the ConnectionNotMadeException and ReadWriteFailedException, and in multithreaded applications, CancelledException as well.
As is recommended for all .NET programming, exceptions should be caught specifically by type. You can then use the Message property to determine the root cause of the exception. For example, after catching a ConnectionNotMadeException, the Message property might tell you that the serial port is in use by another application.
The following code example illustrates the proper use of .NET exceptions with the RMCLink.Interop assembly.
Visual Basic .NET |
Imports RMCLinkNET Module Module1 Sub Main() Try Dim rmc As RMCLink = RMCLink.CreateEthernetLink(DeviceType.RMC70, "192.168.0.10") Console.WriteLine("Connecting...") rmc.Connect() Try Dim data() As Single = New Single(0) {} Console.WriteLine("Reading Axis 0 Actual Position...") rmc.ReadFFile(FileNumber70.fn70StatusAxis0, 8, data, 0, 1) Console.WriteLine(String.Format(" ActPos: {0:F3}", data(0))) Console.WriteLine("Done") Finally rmc.Disconnect() End Try Catch ex As ConnectionNotMadeException Console.WriteLine("Unable to connect to the controller. " & ex.Message) Catch ex As ReadWriteFailedException Console.WriteLine("Unable to read from the controller. " & ex.Message) End Try End Sub End Module |
C# |
using System; using RMCLinkNET; namespace Example { public class Program { static void Main() { try { RMCLink rmc = RMCLink.CreateEthernetLink(DeviceType.RMC70, "192.168.0.10"); Console.WriteLine("Connecting..."); rmc.Connect(); try { float[] data = new float[1]; Console.WriteLine("Reading Axis 0 Actual Position..."); rmc.ReadFFile((int)FileNumber70.fn70StatusAxis0, 8, data, 0, 1); Console.WriteLine(String.Format(" ActPos: {0:F3}", data[0])); Console.WriteLine("Done"); } finally { rmc.Disconnect(); } } catch (ConnectionNotMadeException e) { Console.WriteLine("Unable to connect to the controller. " + e.Message); } catch (ReadWriteFailedException e) { Console.WriteLine("Unable to read from the controller. " + e.Message); } } } } |
Visual C++/CLI |
using namespace System; using namespace RMCLinkNET; int main() { try { RMCLink^ rmc = RMCLink::CreateEthernetLink(DeviceType::RMC70, "192.168.0.10"); Console::WriteLine("Connecting..."); rmc->Connect(); try { array<float>^ data = gcnew array<float>(1); Console::WriteLine("Reading Axis 0 Actual Position..."); rmc->ReadFFile((int)FileNumber70::fn70StatusAxis0, 8, data, 0, 1); Console::WriteLine(String::Format(" ActPos: {0:F3}", data[0])); Console::WriteLine("Done"); } finally { rmc->Disconnect(); } } catch (ConnectionNotMadeException^ e) { Console::WriteLine("Unable to connect to the controller. " + e->Message); } catch (ReadWriteFailedException^ e) { Console::WriteLine("Unable to read from the controller. " + e->Message); } return 0; } |
See Also
Copyright (c) 2024 Delta Computer Systems, Inc. dba Delta Motion