Contact Info

Crumbtrail

ActiveXperts.com » Serial Port Component » How to Use Serial Port Component » ASP

Using ActiveXperts Serial Port Component with Visual Basic .NET

ActiveXperts Serial Port Component is a software development kit (SDK) that enables the user to communicate to a device over a serial interface.

Such a device can be: a weight indicator, a modem, a scanner, or any other device that is equiped with a serial port. It can even be another PC, connected via a NULL modem cable.

ActiveXperts Serial Port Component features the following:

Serial Port Component can be well integrated into .NET environments.

This document describes how the ActiveXperts Serial Port Component can be integrated into into Visual Basic .NET projects.

Step 1: Download and install the ActiveXperts Serial Port Component

Download the the ActiveXperts Serial Port Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a new Visual Basic .NET Project

Launch Microsoft Visual Studio (for instance 'Microsoft Visual Studio 2005') from the Start menu. Choose 'New' from the 'File' menu and click on 'Project'. In the 'New Project' dialog, select a Visual Studio template (for instance: 'Console Application'). Select a name for the application (for instance: 'DemoApp') and a name for the solution (for instance: 'DemoSolution'). Also, select the directory where you want to store the project (for instance: 'C:\MyProjects):

Visual Basic.NET

(Click on the picture to enlarge)

Step 3: Refer to the ActiveXperts Serial Port Component Library and create the objects

Now that a new project has been created, you must add a reference to the ActiveXperts Serial Port Component in the project to be able to use the the ActiveXperts Serial Port Component object. To do so, choose 'Add Reference...' from the 'Project' menu. In the 'Add Reference' dialog that pops up, select the 'COM' tab and select the 'Serial Port Component 2.2 Type Library' as shown in the following picture:

.NET framework

(Click on the picture to enlarge)

Click 'OK' to close the 'Add Reference' dialog.

On top of your code, type the following line to use the ActiveXperts Serial Port Component namespace:

Imports AxSerial

In your Main function, declare and create the following object:

Public m_objComport As ComPort

m_objComport = New ComPort()

Step 4: Send an AT command to a connected Hayes compatible modem

You can now send and receive data to and from a serial interface.

The following code shows how to query a modem:

Imports AxSerial

Public Class Form1
    
    Public m_objComport As ComPort
    Public m_bPortOpened As System.Boolean

    Inherits System.Windows.Forms.Form

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                Handles MyBase.Load
        Dim i As System.Int32

        m_objComport = New ComPort()

        For i = 1 To m_objComport.GetDeviceCount()
            comboDevice.Items.Add(m_objComport.GetDevice(i))
        Next

        For i = 1 To 9
            comboDevice.Items.Add("COM" + i.ToString())
        Next

        comboDevice.SelectedIndex = 0

        comboSpeed.Items.Add("Default")
        comboSpeed.Items.Add("1200")
        comboSpeed.Items.Add("2400")
        comboSpeed.Items.Add("4800")
        comboSpeed.Items.Add("9600")
        comboSpeed.Items.Add("14400")
        comboSpeed.Items.Add("19200")
        comboSpeed.Items.Add("28800")
        comboSpeed.Items.Add("38400")
        comboSpeed.Items.Add("56000")
        comboSpeed.Items.Add("57600")
        comboSpeed.Items.Add("115200")
        comboSpeed.Items.Add("128000")

        comboSpeed.SelectedIndex = 0

        comboSWFlow.Items.Add("Default")
        comboSWFlow.Items.Add("Disabled")
        comboSWFlow.Items.Add("Enabled")

        comboSWFlow.SelectedIndex = 0

        comboHWFlow.Items.Add("Default")
        comboHWFlow.Items.Add("Disabled")
        comboHWFlow.Items.Add("Enabled")

        comboHWFlow.SelectedIndex = 0

        comboFormat.Items.Add("Default")
        comboFormat.Items.Add("n,8,1")
        comboFormat.Items.Add("e,7,1")

        comboFormat.SelectedIndex = 0

        EnableControls()

    End Sub

    Private Sub EnableControls()
        buttonOpen.Enabled = Not m_objComport.IsOpened
        buttonClose.Enabled = m_objComport.IsOpened
        buttonUpdate.Enabled = m_objComport.IsOpened
        buttonSend.Enabled = m_objComport.IsOpened
        textData.Enabled = m_objComport.IsOpened
        listReceived.Enabled = m_objComport.IsOpened

        checkCTS.Enabled = m_objComport.IsOpened
        checkDSR.Enabled = m_objComport.IsOpened
        checkDCD.Enabled = m_objComport.IsOpened
        checkRI.Enabled = m_objComport.IsOpened
        checkDTR.Enabled = m_objComport.IsOpened
        checkRTS.Enabled = m_objComport.IsOpened

        checkDTR.Checked = True
        checkRTS.Checked = True

    End Sub

    Private Sub timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                Handles timer1.Tick
        Dim strReceived As String

        If m_objComport.IsOpened Then

            strReceived = m_objComport.ReadString()

            checkCTS.Checked = m_objComport.QueryCTS() = -1
            checkDSR.Checked = m_objComport.QueryDSR() = -1
            checkDCD.Checked = m_objComport.QueryDCD() = -1
            checkRI.Checked = m_objComport.QueryRI() = -1

            If m_objComport.LastError = 0 And strReceived <> "" Then
                listReceived.Items.Add(strReceived)
            End If
        End If
    End Sub

    Private Function GetResult()
        GetResult = m_objComport.LastError

        textResult.Text = "ERROR: " & m_objComport.LastError.ToString() & " : " & _
          m_objComport.GetErrorDescription(m_objComport.LastError)
    End Function

    Private Sub RetrieveSettings()
        ' Retrieve Device
        m_objComport.Device = comboDevice.SelectedItem.ToString()

        ' Retrieve LogFile
        m_objComport.LogFile = textLogfile.Text

        ' Retrieve Baudrate
        If comboSpeed.SelectedIndex = 0 Then
            m_objComport.BaudRate = 0
        Else
            m_objComport.BaudRate = System.Int32.Parse(comboSpeed.SelectedItem.ToString())
        End If

        ' Retrieve Flow Control
        m_objComport.HardwareFlowControl = comboHWFlow.SelectedIndex
        m_objComport.SoftwareFlowControl = comboSWFlow.SelectedIndex

        ' Retrieve Device Settings
        If comboFormat.Text = "Default" Then
            m_objComport.DataBits = m_objComport.asDATABITS_DEFAULT
            m_objComport.StopBits = m_objComport.asSTOPBITS_DEFAULT
            m_objComport.Parity = m_objComport.asPARITY_DEFAULT
        End If

        If comboFormat.Text = "n,8,1" Then
            m_objComport.DataBits = m_objComport.asDATABITS_DEFAULT
            m_objComport.StopBits = m_objComport.asSTOPBITS_DEFAULT
            m_objComport.Parity = m_objComport.asPARITY_DEFAULT
        End If

        If comboFormat.Text = "e,7,1" Then

            m_objComport.DataBits = m_objComport.asDATABITS_7
            m_objComport.StopBits = m_objComport.asSTOPBITS_1
            m_objComport.Parity = m_objComport.asPARITY_EVEN
        End If

        ' Set Comm Timeout ( 50 ms )
        m_objComport.ComTimeout = 50
    End Sub

    Private Sub buttonOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                _ Handles buttonOpen.Click
        RetrieveSettings()

        ' Set logfile
        m_objComport.LogFile = textLogfile.Text

        ' Open the port
        m_objComport.Open()

        GetResult()

        EnableControls()

        listReceived.Items.Clear()
    End Sub

    Private Sub buttonClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                Handles buttonClose.Click
        m_objComport.Close()

        GetResult()

        EnableControls()

        m_bPortOpened = False
    End Sub

    Private Sub buttonUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                Handles buttonUpdate.Click
        RetrieveSettings()

        m_objComport.UpdateCom()

        GetResult()
    End Sub

    Private Sub checkDTR_CheckedChanged(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles checkDTR.CheckedChanged
        If m_objComport.IsOpened Then
            m_objComport.RaiseDTR(checkDTR.Checked)
            GetResult()
        End If
    End Sub

    Private Sub checkRTS_CheckedChanged(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles checkRTS.CheckedChanged
        If m_objComport.IsOpened Then
            m_objComport.RaiseRTS(checkRTS.Checked)
            GetResult()
        End If
    End Sub

    Private Sub buttonSend_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles buttonSend.Click
        If m_objComport.IsOpened Then
            m_objComport.WriteString(textData.Text)
            listReceived.Items.Clear()
            GetResult()
        End If
    End Sub

    Private Sub buttonView_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles buttonView.Click
        If (System.IO.File.Exists(textLogfile.Text)) Then
            System.Diagnostics.Process.Start(textLogfile.Text)
        End If
    End Sub
End Class

There are many working samples included with the product. You can also find them on the ActiveXperts FTP site: ftp.activexperts-lab.com/samples/serial-port-component.