Using ActiveXperts Serial Port Component with ColdFusion
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 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 ColdFusion document
Create a new blank webdocument with the ".cfm" extension. First of all we are going to build the HTML form where commands and properties of the device can be filled in. Then we are going to make a source code that connects to the device.
Type the following code to create and style the form:
<!---Begin of the script---> <!---The stylesheet is typed down in the header---> <!---The form is typed down in the body---> <cfoutput> <html> <head> <title>Active Comport Cold Fusion Sample</title> <style> <!-- body{ font-family:verdana; font-weight:normal; font-size:xx-small; color:navy; text-align:center; } table{ font-family:verdana; font-weight:normal; font-size:xx-small; color:navy; border: 1px solid navy; } td{ font-family:verdana; font-weight:normal; font-size:xx-small; color:navy; border:1pxsolidnavy; } .form{ font-family:verdana; font-weight:normal; font-size:xx-small; color:navy; border:1pxsolidnavy; background-color:white; } //--> </style> </head> <body> <h2>Active Comport Coldfusion Sample</h2> <h3>Querya com device:</h3> <formn ame=getcommand method=get> ___________________________________________________</br> <br> <b>Command:</b><br> <input type=text style="width:300px;"class=form name="Commando"><br> <b>Comport:</b><br> <select name=cbComport class=form style="width:300px;"> </cfoutput> <!---If the form is submitted,the entered values will be filled in again automaticly---> <cfif IsDefined("URL.cbComport")> <cfset strComport = URL.cbComport> <cfoutput> <option value="#strComport#">#strComport#</option> </cfoutput> </cfif> <cfoutput> <option value="COM1">Com1</option> <option value="COM2">Com2</option> <option value="COM3">Com3</option> <option value="COM4">Com4</option> <option value="COM5">Com5</option> <option value="COM6">Com6</option> <option value="COM7">Com7</option> <option value="COM8">Com8</option> <option value="COM9">Com9</option> </select><br> <b>Baudrate</b><br> <select name=cbBaudrate class=form style="width: 300px;"> </cfoutput> <!---If the form is submitted, the entered values will be filled in again automaticly---> <cfif IsDefined("URL.cbBaudrate")> <cfsetstr Baudrate=URL.cbBaudrate> <cfoutput> <option value="#strBaudrate#">#strBaudrate#</option> </cfoutput> </cfif> <cfoutput> <option value="0">Default</option> <option value="110">110</option> <option value="300">300</option> <option value="600">600</option> <option value="1200">1200</option> <option value="2400">2400</option> <option value="4800">4800</option> <option value="9600">9600</option> <option value="14400">14400</option> <option value="19200">19200</option> <option value="38400">38400</option> <option value="57600">57600</option> <option value="64000">64000</option> <option value="115200">115200</option> <option value="128000">128000</option> <option value="256000">256000</option> </select><br> <b>Logfile:</b><br> </cfoutput> <!---If the form is submitted,the entered values will be filled in again automaticly---> <cfif IsDefined("URL.txtLogfile")> <cfset txtLogfile=URL.txtLogfile> <cfoutput> <input type=text style="width:300px;"class=form name="txtLogfile" value=#txtLogfile#> </cfoutput> <cfelse> <cfoutput> <input type=text style="width:300px;" class=form name="txtLogfile" value="C:\logfile.txt"> </cfoutput> </cfif> <cfoutput> <br> <input type=submit value="Submitcommand!"class=form><br> ___________________________________________________</br> </form> </cfoutput>
Step 3: Send an AT command to a connected Hayes compatible modem
Once you have entered the commands you want to send to the modem, we can send the command. To do this we need to create an object in the sourcecode. The object comport. In coldfusion an object is created with the tag <cfobject>. So to create the object we need to add the following code:
<cfobject class="AxSerial.Comport" type="com" name="objComport" Action="Create">
The next step is to open the device and send the command. When the command is sent we need to read the device's reply. And last, but not least, we need to close the device. You can see in the source below that the <cfobject> tag is used:
The following code shows how to query a modem:
<!--- Before executing the given command, we are going to make sure if there IS any command ---> <cfif isDefined("URL.Commando")> <!--- Below the command is executed ---> <!--- The baudrate is being set ---> <!--- The right comport is going to be filled in ---> <!--- The logfile is being set ---> <!--- The command is being executed ---> <!--- The result is going to be written to an array ---> <cfobject class="AxSerial.Comport" type="com" name="objComport" Action="Create"> <cfscript> objComport.baudrate = URL.cbBaudrate; objComport.Device = URL.cbComport; objComport.LogFile = URL.txtLogfile; objComport.Open(); objComport.WriteString(URL.Commando); objComport.Sleep(1000); arrReply = ArrayNew(1); i = 0; Do { i = i + 1; arrReply[i] = objComport.ReadString(); } While (objComport.Lasterror eq 0); objComport.Close(); </cfscript> <!--- The results are going to be echo'd ---> <!--- The result array is being sequentialy traversed ---> <cfoutput> <b>Reply:</b><br> </cfoutput> <cfloop from="1" to="#ArrayLen(arrReply)#" index="i"> <cfoutput>#arrReply[i]#<br></cfoutput> </cfloop> <!--- Possible errors are being displayed ---> <cfset intError = objComport.LastError> <cfset strError = objComport.GetErrorDescription(objComport.LastError)> <cfoutput> ___________________________________________________</br> <br> <b>Results:</b><br> Error: #intError# : #strError#! </cfoutput> <!--- The if statement is being closed ---> </cfif> <!--- the body and the html tag are being closed ---> <cfoutput> </body> </html> </cfoutput>
You can download the full samples here.