Quicklinks
ActiveComport 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.
ActiveComport 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, USB cable or Bluetooth connections, support for GSM/GPRS modems, support for Virtual COM ports (i.e. COM ports redirected through the network), hardware flow control (RTS/CTS, DTR/DSR), software flowcontrol (XON/XOFF), configurable baudrate/parity/stopbits, full buffered data transfer, text/binary data transfer.
ActiveComport can be well integrated into Powershell environments.
This document describes how ActiveComport can be integrated into Powershell projects.
Download ActiveComport from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
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 ActiveComport Toolkit.
Create a new Powershell file called DEMO.PS1.
Create the ActiveComport object like this:
$objComport = new-object -comobject ActiveXperts.Comport
Now, add the following lines to the file to have your fist ActiveComport Powershell program:
Write-Host "ActiveComport Version: " $objComport.Version Write-Host "ActiveComport Build : " $objComport.Build Write-Host "Expiration date : " $objComport.ExpirationDate
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:
################################################################################# # ActiveComport - Powershell script # © Copyright ActiveXperts Software B.V. # # For more information about ActiveComport, please # visit the online ActiveComport 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 ActiveXperts.Comport Write-Host "ActiveComport Version: " $objComport.Version Write-Host "ActiveComport Build : " $objComport.Build Write-Host "ActiveComport Module : " $objComport.Module Write-Host "Expiration date : " $objComport.ExpirationDate $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:\ActiveComport.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.