Contact Info

Crumbtrail

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

Using ActiveXperts Serial Port Component with PHP

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.

Step 1: Download and install 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 the ActiveXperts Serial Port Component object in PHP

You must use PHP script to declare and create the ActiveXperts Serial Port Component object.

Use the following PHP code to create the Comport object:

$objComport  = new COM ( "AxSerial.ComPort" );

Step 3: Send AT commands from a PHP script

You can now send and/or receive serial data.

The following PHP code shows how to initialize a modem with some AT commands:

<html>
   <head>
   <META HTTP-EQUIV="CONTENT-Type" CONTENT="text/html;CHARSET=utf-8" >
   <title>ActiveXperts Serial Port Component PHP Sample</title>
   </head>
   <body>
   <font face="sans-serif" size="2">
   <hr size="1" color="#707070">
   <font size="4">ActiveXperts Serial Port Component PHP Sample</font>
   <br>
   <br>
   <b>Initialize a Hayes compatible modem with some AT commands.</b>
   <br>
   <br>
   <hr size="1" color="#707070" >
   <br>

   <?php
   
   $objComport	= new COM ( "AxSerial.ComPort" );
  
   $objComport->Logfile     = "C:\\PhpSerialLog.txt";
   $objComport->Device      = "COM1";
   $objComport->Baudrate    = 9600;
   $objComport->ComTimeout  = 1000;

   $objComport->Open ();

   if ( $objComport->LastError == 0 )
   {     
        Echo "Sending 'ATZ'...<BR>";
        Echo "<BR>";
    

        $objComport->WriteString      ( "ATZ" );
        
        while ( $objComport->LastError == 0 )
        {
            Echo $objComport->ReadString  ();
            Echo "<BR>";
        }

        Echo "Sending 'ATI'...<BR>";
        Echo "<BR>";

        $objComport->WriteString      ( "ATI" );

        while ( $objComport->LastError == 0 )
        {
            Echo $objComport->ReadString  ();
            Echo "<BR>";
        }

        Echo "Sending 'AT&C0'...<BR>";
        Echo "<BR>";

        $objComport->WriteString      ( "AT&C0" );

        while ( $objComport->LastError == 0 )
        {
            Echo $objComport->ReadString  ();
            Echo "<BR>";
        }
   }
   else
   {
       $ErrorNum = $objComport->LastError;
       $ErrorDes = $objComport->GetErrorDescription ( $ErrorNum );

       Echo "Error sending commands: #$ErrorNum ($ErrorDes).";
   }
 
   Echo "Ready.";

   $objComport->Close ();
?>

   <br>
   <br>
   <hr size="1" color="#707070">
   <font size="1" face="Verdana">This demo uses ActiveXperts Serial Port Component</font>
   </body>
</html>

You can download the full samples here.