Shortcut Menu

Skip

Main Navigation

Choose your language


ActiveXperts Serial Port Component Add serial communication capabilities to any Windows or .NET application

Quicklinks


Using ActiveXperts Serial Port Component with Powershell

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:

  • Direct COM port support (like 'COM1')
  • TAPI (Windows Telephony Device) support (like 'Standard 56000 bps Modem');
  • Support for RS-232/RS422/RS485, up to 256 simultaneous ports;
  • Support for all types of Hayes compatible modems;
  • Support for serial cable as well as USB cable or Bluetooth connections;
  • Support for Virtual COM ports (i.e. COM ports redirected through the network);
  • Hardware flow control (RTS/CTS, DTR/DSR) and software flowcontrol (XON/XOFF) support;
  • Configurable baudrate/parity/stopbits, full buffered data transfer, text/binary data transfer.

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

Create a new Powershell file called DEMO.PS1.

Create the ActiveXperts Serial Port Component object like this:

$objComport = new-object -comobject AxSerial.ComPort

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

Write-Host "Serial Port Component Version: " $objComport.Version
Write-Host "Serial Port Component Build  : " $objComport.Build
Write-Host "License status               : " $objComport.LicenseStatus

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 Powershell code shows how to query a modem:

#################################################################################
# Serial Port Component - Powershell script
# © Copyright ActiveXperts Software B.V.
#
# For more information about Serial Port Component, please
# visit the online Serial Port Component page at:
# http://www.activexperts.com
#################################################################################
# Example:
# .\QueryDevice.ps1  / send commands and receive responses
#################################################################################

cls

#################################################################################
# Functions --------------------------------------------------------------------#
#################################################################################


#################################################################################
# AskDevice --------------------------------------------------------------------#
#################################################################################

function AskDevice($objComport)
{
  $strTitle = ""
  for ($i=1; $i -lt 5;$i++) 
  { 
    $strTitle = $strTitle + "  " + $i + ": COM" + $i + "`n" 
  }  
    
  for ($j=0; $j -lt $objComport.GetDeviceCount(); $j++)      
  {
    $strTitle = $strTitle + "  " + ($i + $j ) + ": " + 
    $objComport.GetDevice($j) + "`n"
  }  

  $strDevice = ""
  while($strDevice -eq "")
  {
    #$strInput = Read-Host $strTitle "Select device:" "1"
    $strInput = Read-Host $strTitle "Select device"
    if($strInput -eq "")
    {
      $strDevice = ""
    }  
    elseif([int]$strInput -lt $i)
    {
      $strDevice = "COM" + $strInput
    }  
    elseif([int]$strInput -lt $i + $j)
    {
      $strDevice = $objComport.GetDevice([int]
      $strInput - $i)
    }  
  }  

  Write-Host "Selected device: " $strDevice

  return $strDevice
}

#################################################################################
# Ask --------------------------------------------------------------------------#
#################################################################################

function Ask($strTitle, $strDefault, $bAllowEmpty)
  {
    do
      {
        $strInput = Read-Host $strTitle $strDefault
        if ($strInput -ne "")
          {
            $strReturn = $strInput
          }  
      }  
    until($strReturn -ne "" -or $bAllowEmpty)

    return $strReturn
  }

#################################################################################
# ReadResponse -----------------------------------------------------------------#
#################################################################################

function ReadResponse($objComport)
  {
    $str = "notempty"
    $objComport.Sleep(200)
    while ($str -ne "")
      {
        $str = $objComport.ReadString()
        if ($str -ne "")
          {
            Write-Host "  <- " $str
          }  
      }
  }

#################################################################################
# WriteCommand -----------------------------------------------------------------#
#################################################################################

function WriteCommand($objComport)
  {
    $str = Read-Host "Enter command (enter QUIT to stop the program)"
    $objComport.WriteString($str)
    if($objComport.LastError -eq 0)
      {
        Write-Host "  -> " $str
      }  
    else
      {
        Write-Host "Write failed, result: " $objComport.LastError " ("
        $objComport.GetErrorDescription($objComport.LastError) ")"
      }  

    if ($str -eq "QUIT")
      {
        return $false
      }  
    else
      {
        return $true
      }
  }


#################################################################################
# THE SCRIPT ITSELF ------------------------------------------------------------#
#################################################################################

$objComport       = new-object -comobject AxSerial.ComPort
Write-Host "Serial Port Component Version: " $objComport.Version
Write-Host "Serial Port Component Build  : " $objComport.Build
Write-Host "Serial Port Component Module : " $objComport.Module
Write-Host "License Status      : " $objComport.LicenseStatus

$objComport.Device = AskDevice($objComport)
# Is there a COM device attached to the PC
if($objComport.Device -eq "")
  {
    Write-Host "No COM device found"
    exit
  }
# Optionally override defaults for direct COM ports
if($objComport.Device.substring(0,3) -eq "COM")
  {
    $objComport.BaudRate  = Ask "Enter baud rate (no input means: default
    baud rate):" "9600" $true 
    # $objComport.HardwareFlowControl  = $true
    # $objComport.SoftwareFlowControl  = $false
  }

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

# Open the port
$objComport.Open()
Write-Host "Open, result:" $objComport.LastError " (" 
$objComport.GetErrorDescription($objComport.LastError) ")"

if($objComport.LastError -ne 0)
  {
    exit
  }

ReadResponse($objComport)

while (WriteCommand($objComport))
  {
    ReadResponse($objComport)
  }  
  
$objComport.Close()
Write-Host "Close, result: " $objComport.LastError " (" 
$objComport.GetErrorDescription($objComport.LastError) ")"
Write-Host "Ready."

To run the code, start Powershell and browse to the location of the file you just created. Enter .\Demo.ps1 to run the code. Notice that if the script is not working, you have to change the execution policy; you can do that with the following command:

Set-ExecutionPolicy -unrestricted

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