Contact Info

Crumbtrail

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

Using ActiveXperts Serial Port Component with VBA (Visual Basic for Applications)

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:

Step 1: Download and install the ActiveXperts Serial Port Component

Download 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 Excel document

Create the form displayed in the image below. To create the buttons, textarea's and drop down menu's, click "View", "Toolbars", "Control toolbox".

Visual Basic for Applications

(Click on the picture to enlarge)

Step 3: Fill in the dropdown-menu's

Wen you're finished setting up the form, you need to fill in the dropdown menu's. You need to load the content of your submenu when you open your workbook.You can do this using a visual basic code. To edit your visual basic code you need to be in design mode. To get in design mode, click the button "Design mode" Design mode on the control toolbox. You can edit the visual basic code behind your form in Excel by clicking the button "View code" View code below "Design mode" on the control toolbox.

Open "ThisWorkbook".

Work book

(Click on the picture to enlarge)

Type the following code in private sub workbook_open():

Make sure you're directing to the right sheet (worksheets("sheet1")).

'load the dropdown menu's
With Worksheets("Sheet1").cbComport
    .AddItem "COM1"
    .AddItem "COM2"
    .AddItem "COM3"
    .AddItem "COM4"
    .AddItem "COM5"
    .AddItem "COM6"
    .AddItem "COM7"
    .AddItem "COM8"
End With

With Worksheets("Sheet1").cbSpeed
    .AddItem "110"
    .AddItem "300"
    .AddItem "600"
    .AddItem "1200"
    .AddItem "2400"
    .AddItem "4800"
    .AddItem "9600"
    .AddItem "14400"
    .AddItem "19200"
    .AddItem "38400"
    .AddItem "57600"
    .AddItem "64000"
    .AddItem "115200"
    .AddItem "128000"
    .AddItem "256000"
End With
    
With Worksheets("Sheet1").cbDataFormat
    .AddItem "8,n,1"
    .AddItem "7,e,1"
End With
    
With Worksheets("Sheet1").cbHWFlowControl
    .AddItem "Disable"
    .AddItem "Enable"
End With
    
With Worksheets("Sheet1").cbSWFlowControl
    .AddItem "Disable"
    .AddItem "Enable"
End With

Worksheets("Sheet1").btnClose.Enabled = False
Worksheets("Sheet1").btnOpen.Enabled = True

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

In this step the code to send and recieve information from the modem will be written. Type the code below down to sheet1. Make sure the buttons, comboboxes and textarea's have the right names.

The following code shows how to query a modem:

Option Explicit

'set comport as object
Public objComport As Object


'******************************************************
'                   sub open button
'******************************************************

Private Sub btnOpen_Click()

'empty the textbox that displays the errors
txtOutput.Text = ""
    
'Create the object Comport
Set objComport = CreateObject("AxSerial.ComPort")
    
'Create logfile
objComport.LogFile = txtOutputfile.Text
    
'Set the comport
objComport.Device = cbComport.Text
    
'Set the baudrate
If (cbSpeed.Text = "Default") Then
    objComport.BaudRate = 0
Else
    objComport.BaudRate = cbSpeed.Text
End If
    
'set the flowcontrol
If (cbHWFlowControl.Text = "Default") Then
    objComport.HardwareFlowControl = 0
ElseIf (cbHWFlowControl.Text = "Disabled") Then
    objComport.HardwareFlowControl = 1
Else
    objComport.HardwareFlowControl = 2
End If
    
'set the software flowcontrol
If (cbSWFlowControl.Text = "Default") Then
   objComport.SoftwareFlowControl = 0
ElseIf (cbSWFlowControl.Text = "Disabled") Then
   objComport.SoftwareFlowControl = 1
Else
   objComport.SoftwareFlowControl = 2
End If
    
'Set the dataformat
If (cbDataFormat.Text = "Default") Then
   objComport.DataBits = objComport.asDATABITS_DEFAULT
   objComport.StopBits = objComport.asSTOPBITS_DEFAULT
   objComport.Parity = objComport.asPARITY_DEFAULT
End If
        
If (cbDataFormat.Text = "8,n,1") Then
   objComport.DataBits = objComport.asDATABITS_8
   objComport.StopBits = objComport.asSTOPBITS_1
   objComport.Parity = objComport.asPARITY_NONE
End If
    
If (cbDataFormat.Text = "7,e,1") Then
   objComport.DataBits = objComport.asDATABITS_7
   objComport.StopBits = objComport.asSTOPBITS_1
   objComport.Parity = objComport.asPARITY_EVEN
End If
    
'open the device
objComport.Open
   
'check for errors
GetResult
        
End Sub



'******************************************************
'               Sub Show results
'******************************************************

Private Sub GetResult()
Cells(1, 1) = objComport.LastError

If objComport.LastError = 0 Then
   txtOutput = "SUCCESS"
        
   'the connection with the device was succesfull, 
   'disable the open button and enable the close button
   btnOpen.Enabled = False
   btnClose.Enabled = True
Else
   'say out any error
   'for more information about errors visit: 
   'https://www.activexperts.com/support/errorcodes/
    txtOutput = "ERROR " & objComport.LastError & " ( " & _
      objComport.GetErrorDescription(objComport.LastError) & " )"
End If
    
End Sub

'*******************************************************
'             Sub Close button
'*******************************************************

Private Sub btnClose_Click()
objComport.Close
    
'Display the errors
GetResult
    
'clear the reply field
txtReply = ""
    
'enable the openbutton and disable the close button
btnOpen.Enabled = True
btnClose.Enabled = False
    
End Sub


'******************************************************
'             Sub execute your commands
'******************************************************

Private Sub btnExecute_Click()
    
'before displaying the result of the commands given, clear the fields
txtOutput.Text = ""
txtReply = ""
        
'execute the given command
objComport.WriteString (txtExecute)
    
'check for errors
GetResult
    
'set time out, so the device wil have enough time to 
'execute the command before displaying the result
objComport.Sleep (1000)
    
'say out the results
txtReply.Text = objComport.ReadString
    
End Sub

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