RMCLink Component

How to: Use from Visual Basic .NET

Visual Basic (.NET) users should use the RMCLink.Interop .NET Assembly instead of using the RMCLink COM Component directly. Specifics on using this component from Visual Basic (.NET) are described below, including a step-by-step walkthrough. The project developed in the walkthrough is included as a Visual Basic (.NET) project for Visual Studio in the RMCLink example projects.

Adding a Reference to the RMCLink.Interop .NET Assembly

Before referencing types in the RMCLinkNET namespace, you must add a reference to the RMCLink.Interop .NET assembly.

To add a reference to the RMCLink.Interop .NET assembly:

  1. Open or create a new Visual Basic project in Visual Studio.

  2. On the Project menu, click Add Reference or Add Project Reference. The Add References dialog box is different between Visual Studio versions.

  3. A Visual Basic project in Visual Studio 2022 has the folder structure Dependencies > Assemblies; the RMCLink.Interop assembly appears here. A Visual Basic project in Visual Studio 2015 has a References folder and the assembly appears there. Visual Basic projects in Visual Studio 2005-2013 do not have a References folder in the project tree by default. You can open the project Property Pages and look at the References page to verify that the RMCLink.Interop assembly appears.

    1. In Visual Studio 2022 or later, do the following to ensure that RMCLink is properly linked:

      • In the Solution Explorer, expand Dependencies and Assemblies.

      • Click the RMCLink.Interop entry.

      • If the Properties window is not visible, then on the View menu and then click Properties Window, or press ALT+ENTER or the F4 key.

      • In the Properties window, for the RMCLink.Interop Reference Properties, set Embed Interop Types to No.

      • In the Properties window, for the RMCLink.Interop Reference Properties, set Copy Local to Yes.

    2. In Visual Studio 2015, do the following to ensure that RMCLink is properly linked:

      • In the Solution Explorer, expand References.

      • Click the RMCLink.Interop entry.

      • If the Properties window is not visible, then on the View menu and then click Properties Window, or press ALT+ENTER or the F4 key.

      • In the Properties window, for the RMCLink.Interop Reference Properties, set Embed Interop Types to No.

      • In the Properties window, for the RMCLink.Interop Reference Properties, set Copy Local to Yes.

    3. In Visual Studio 2013, do the following to ensure that RMCLink is properly linked:

      • On the Project menu, click [Project] Properties.

      • Select the References tab.

      • In the References list, select the RMCLink.Interop line.

      • If the Properties window is not visible, then on the View menu, point to Other Windows, and then click Properties Window, or press ALT+ENTER.

      • In the Properties window, for the RMCLink.Interop Reference Properties, set Embed Interop Types to False.

      • In the Properties window, for the RMCLink.Interop Reference Properties, set Copy Local to True.

You can now browse the RMCLinkNET namespace using the Object Browser:

  1. On the View menu, click Object Browser.

  2. You should see the RMCLink.Interop assembly and be able to browse all the types included in it.

  3. Close the Object Browser when you are done.

Using the RMCLinkNET Namespace

All types in the RMCLink.Interop assembly are contained within the namespace RMCLinkNET. When any .NET type is referenced in the program, the compiler must know which namespace(s) to look in. Notice that adding a reference to the assembly does not make the compiler look in its namespace by default. In Visual Basic .NET, use one of the following to specify the namespace.

First, you can qualify each type located in the RMCLinkNET namespace by prefixing them with the RMCLinkNET. as shown below.

Dim rmc As RMCLinkNET.RMCLink = RMCLinkNET.RMCLink.CreateEthernetLink(RMCLinkNET.DeviceType.RMC70, "192.168.0.10")

However, most .NET programmers prefer the Imports statement, as shown below.

Imports RMCLinkNET

...

Dim rmc As RMCLink = RMCLink.CreateEthernetLink(DeviceType.RMC70, "192.168.0.10")

The Imports statement tells the compiler to always check the given namespace when resolving a type, and eliminates the need to specify the namespace on every type reference.

Creating an RMCLink Object

The first step is to create an instance of the RMCLink class. Use one of the static CreateEthernetLink, CreateSerialLink or CreateUSBLink methods to create the instance; because there is no public constructor, the New keyword cannot be used. The following line demonstrates how this is done in Visual Basic .NET:

Imports RMCLinkNET

  ...

Dim rmc As RMCLink = RMCLink.CreateEthernetLink(DeviceType.RMC70, "192.168.0.10")

Notice that this line will only compile correctly if the reference to the RMCLink.Interop assembly has been added to the project as described above.

Lifetime of the RMCLink Object

Notice that although most Visual Basic .NET samples in this documentation control the entire lifetime of the RMCLink object within a single function, this is often not the most efficient way of using the RMCLink control. Generally, when you first want to connect to a controller, you will create the RMCLink object and call its Connect method. This RMCLink object should be located outside of the function or subroutine that creates and connects it, so that it can be used by other functions and subroutines to read or write from it.

Walkthrough

This walkthrough will create a simple application in Visual Basic .NET for reading the Target and Actual Positions from either axis 0 or axis 1 of an RMC70. The project developed in this walkthrough is included as a Visual Basic .NET project in the RMCLink example projects.

The final application will look like this:

Step 1: Create a New Project

  1. Open the Visual Basic .NET IDE.

  2. On the File menu, point to New, and then click Project. This will open the New Project dialog box.

  3. For Visual Basic 2008 or later, locate and select the Visual Basic template named Windows Forms Application. For Visual Basic 2005, locate and select the Visual Basic template named Windows Application.

  4. In the Name text box, type "RMCLinkSample".

  5. In the Location text box, type the path where you want to save this project.

  6. Click OK.

Step 2: Create the User Interface

Add controls to the Form1 using the Toolbox so that the form looks approximately like this, using the properties described below:

Form1.vb [Design]

For the Form, use the default properties except for the following changes:

Form1 Form

Text

RMCLink Sample

 

For the controls in the first column, from top to bottom, use the default properties except for the following changes:

axisGroup GroupBox

(Name)

axisGroup

Text

Axis 0

 

Label1 Label

Text

Target Position:

 

tarPosValue TextBox

(Name)

tarPosValue

ReadOnly

True

TabStop

False

Text

(blank)

 

Label2 Label

Text

Actual Position:

 

actPosValue TextBox

(Name)

actPosValue

ReadOnly

True

TabStop

False

Text

(blank)

 

For the controls in the second column, from top to bottom, use the default properties except for the following changes:

connectButton Button

(Name)

connectButton

Text

&Connect

TabIndex

0

 

readAxis0Button Button

(Name)

readAxis0Button

Text

Read Axis &0

TabIndex

1

 

readAxis1Button Button

(Name)

readAxis1Button

Text

Read Axis &1

TabIndex

2

 

disconnectButton Button

(Name)

disconnectButton

Text

&Disconnect

TabIndex

3

To test the application, on the Debug menu, click Start Without Debugging. The buttons do nothing yet.

Step 3: Add the RMCLink Reference

Before using the RMCLink.Interop assembly, we must add a reference to the assembly:

  1. On the Project menu, click Add Reference. The Add References dialog box is different between Visual Studio versions.

  2. In Visual Studio 2010 or later, do the following to ensure that RMCLink is properly linked:

  3. Open the project Property Pages and look at the References page to verify that the RMCLink.Interop assembly appears.

Step 4: Declare the RMCLink Object

We will first define a private field to hold the RMCLink object reference. This reference will be used by the various member methods that will be added later. Open the Code View of Form1.vb (on the View menu, click Code), and add the following lines to the code:

Top of Form1 Module

Imports RMCLinkNET
 
Public Class Form1
    ...
 
    Private rmc As RMCLink
End Class

The declaration of the rmc variable as an RMCLink object reference will be used by each of the handler functions added in later steps. The Imports statement instructs the compiler to automatically check the RMCLinkNET namespace when resolving type references in this file.

Step 5: Handle Form Load and Closing

The previous step defined the rmc variable. However, we have not yet created an instance of it nor used it in any other way. We will now add two Form handlers, one for Load and one for FormClosing.

Add the following handler for the Load event for Form1.

Form1_Load Handler

Imports RMCLinkNET
 
Public Class Form1
    ...
 
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' Create an RMCLink object.
        rmc = RMCLink.CreateEthernetLink(DeviceType.RMC70, "192.168.0.22")
 
        ' Since we are disconnected to start, enable the buttons accordingly.
        connectButton.Enabled = True
        readAxis0Button.Enabled = False
        readAxis1Button.Enabled = False
        disconnectButton.Enabled = False
    End Sub
End Class

This handler will create an instance of the RMCLink class. This way all other handlers in our form can count on this object existing. Notice that the last four lines of this subroutine simply enable the Connect button and disable the other buttons since they can't be used until the link has been connected.

Next, add the following handler for the FormClosing event on the Form1 class.

Form1_FormClosing Handler

Imports RMCLinkNET
 
Public Class Form1
    ...
 
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If rmc.IsConnected(PingType.DoNotPing) Then
            rmc.Disconnect()
        End If
    End Sub
End Class

This handler simply makes sure that the RMCLink object is disconnected. This is not strictly necessary since when the application closed the RMCLink object is released, which will disconnect it from the controller, but it is good practice nonetheless. For example, if this was not the only form in the application, then we have no control over when the rmc object is actually garbage collected, and therefore the serial port or TCP/IP connection may be left open unnecessarily long.

Notice that we call the RMCLink.IsConnected method to avoid an exception being thrown by the RMCLink.Disconnect method if the connection was not currently open. It is generally better to catch this case with the above logic rather than an exception.

To test the application, on the Debug menu, clicking Start Without Debugging. The buttons still do nothing, but they should be properly enabled on startup.

Step 6: Handle Connect and Disconnect

Next, we will handle the user clicking the Connect and Disconnect buttons.

When the Connect button is clicked, we make a call to RMCLink.Connect. However, since this method can fail if the controller cannot be connected to, we need to handle the error case. Therefore, we wrap the code in a Try Catch block, catching specifically the ConnectionNotMadeException. Without catching this exception, the application would crash if the connection could not be established. Finally, if we successfully connect (that is, the exception is not thrown), we enable the buttons that required us to be connected and disable the Connect button.

connectButton_Click Handler

Imports RMCLinkNET
 
Public Class Form1
    ...
 
    Private Sub connectButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles connectButton.Click
        Try
            rmc.Connect()
 
            ' We have successfully connected, so update which buttons are enabled.
            readAxis0Button.Enabled = True
            readAxis1Button.Enabled = True
            disconnectButton.Enabled = True
            connectButton.Enabled = False
 
        Catch ex As ConnectionNotMadeException
            MessageBox.Show("Unable to connect. " + ex.Message)
 
        End Try
    End Sub
End Class

 

When the Disconnect button is clicked, we make a call to RMCLink.Disconnect. This function will always succeed, provided we don't call it when the link is already disconnected, but we have covered that case by correctly enabling the command buttons. After disconnecting, we disable the buttons that require a connection to be established and re-enable the Connect button.

disconnectButton_Click Handler

Imports RMCLinkNET
 
Public Class Form1
    ...
 
    Private Sub disconnectButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles disconnectButton.Click
        rmc.Disconnect()
 
        ' We have successfully disconnected, so update which buttons are enabled.
        connectButton.Enabled = True
        readAxis0Button.Enabled = False
        readAxis1Button.Enabled = False
        disconnectButton.Enabled = False
    End Sub
End Class

To test the application, on the Debug menu, clicking Start Without Debugging. The Connect and Disconnect buttons should now work, although the Read Axis 0 and Read Axis 1 buttons still do nothing.

Step 7: Handle Read Axis 0 and Read Axis 1

The final step is to implement the handlers for the Read Axis 0 and Read Axis 1 buttons. Both functions do the same things, except that they set the axisGroup text differently, and they read from different status files. Both will call a common private method to do the majority of the work. Add the following three methods to your Form1 class:

readAxis0Button_Click Handler

Imports RMCLinkNET
 
Public Class Form1
    ...
 
    Private Sub readAxis0Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles readAxis0Button.Click
        HandleReadAxisButton(0)
    End Sub
 
    Private Sub readAxis1Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles readAxis1Button.Click
        HandleReadAxisButton(1)
    End Sub
 
    Private Sub HandleReadAxisButton(ByVal axis As Integer)
        Try
            Dim data() As Single = New Single(1) {}
 
            ' Read the Target Position (Fx:53) into data(0).
            rmc.ReadFFile(FileNumber70.fn70StatusAxis0 + axis, 53, data, 0, 1)
 
            ' Read the Actual Position (Fx:8) into data(1).
            rmc.ReadFFile(FileNumber70.fn70StatusAxis0 + axis, 8, data, 1, 1)
 
            ' Update the display.
            axisGroup.Text = "Axis " + axis.ToString()
            tarPosValue.Text = String.Format("{0:F3}", data(0))
            actPosValue.Text = String.Format("{0:F3}", data(1))
 
        Catch ex As ReadWriteFailedException
            disconnectButton_Click(Nothing, Nothing)
            MessageBox.Show("Unable to read data. " + ex.Message)
 
        End Try
    End Sub
End Class

The HandleReadAxisButton method wraps the code handling in a Try Catch block, catching specifically the ReadWriteFailedException. If this exception was not caught, then the application would crash if the cable was disconnected and a read was attempted. The exception handler displays an error message before, but it also calls the disconnectButton_Click handler. This is because a failed read or write does not disconnect the connection, and plus we want to return the button enabled states to being disconnected.

The rest of the body of these methods simply reads the values and updates the text boxes and axis label accordingly.

To test the completed application, on the Debug menu, clicking Start Without Debugging.

 

See Also

RMCLink.Interop .NET Assembly | How Do I Overview | RMCLink Component


Send comments on this topic.

Copyright (c) 2024 Delta Computer Systems, Inc. dba Delta Motion