Contact Info

Crumbtrail

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

Using ActiveXperts Serial Port Component with VBScript

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 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 script

Create a new script using your favorite editor. You can simply use notepad. However, a VBScript editor is recommended, so you can browse through objects, objects properties and object functions.

You're now able to write a more advanced script to communicate using the ActiveXperts Serial Port Component.

Step 3: Create the ActiveXperts Serial Port Component object in VBScript

Create a new VBScript file called DEMO.VBS. It is recommended to insert the following line on top of your code:

Option Explicit

This statement requires that all variable names be defined (with the Dim statement), to avoid simple typos that can cause incredible headaches and long debugging sessions for something that should have never happened.

Now, declare the ActiveXperts Serial Port Component object:

Dim objComPort

Create the ActiveXperts Serial Port Component object like this:

Set objComPort      = CreateObject( "AxSerial.ComPort" )

Now, add the following lines to the file to have your fist Serial Port Component VBScript program:

WScript.Echo "Version: " & objComPort.Version
WScript.Echo "License Status: " & objComPort.License Status

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

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

The following VBScript code shows how to query a modem:

Option Explicit

Dim numDevices, strDevices, i, str, objComport


Set objComport = CreateObject( "AxSerial.ComPort" )
Wscript.Echo "Serial Port Component " & objComport.Version & " demo."
Wscript.Echo "License Status: " & objComport.LicenseStatus & vbCrLf

' Set Device property
numDevices  = objComport.GetDeviceCount()
strDevices  = "*** Enter one of the following device names *** " & vbCrLf & vbCrLf
For i = 0 To numDevices - 1
    strDevices = strDevices & objComport.GetDevice( i )
    strDevices = strDevices & vbCrLf 
Next
strDevices = strDevices & "COM1" & vbCrLf & "COM2" & vbCrLf & "COM ..." & vbCrLf
Do
    objComport.Device = InputBox(  strDevices, "Input" )
Loop until objComport.Device <> ""

objComport.BaudRate             = 9600
objComport.HardwareFlowControl  = True
objComport.SoftwareFlowControl  = False

' Set Logging - for troubleshooting purposes
objComport.LogFile = "C:\Serial Port Component.log"

' Open the port
objComport.Open
If( objComport.LastError <> 0 ) Then
    Wscript.Echo "Open failed, Error #" & objComport.LastError & " : " & _
        objComport.GetErrorDescription( objComport.LastError )
    WScript.Echo "Ready."
    WScript.Quit
End If

' Write command, and wait for the response
WriteStr objComport, "atz"
ReadStr  objComport

' Write command, and wait for the response
WriteStr objComport, "at&f"
ReadStr  objComport

' Close the port
objComport.Close
Set objComport = Nothing
WScript.Echo "Ready."

' ********************************************************************
' Sub Routines
' ********************************************************************

Sub WriteStr( obj, str )
    obj.WriteString str
    WScript.Echo "-> " & str
End Sub

' ********************************************************************

Sub ReadStr( obj )
    str = "notempty"
    obj.Sleep 200
    Do While str <> ""
        str = obj.ReadString
        If( str <> "" ) Then
            WScript.Echo "<- " & str
        End If
    Loop
End Sub

' ********************************************************************

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.