title: PHP Serial Port Access
body: <p>I am trying to implement this code below, I have a wavecom gsm modem connected to COM6 <a href="http://imgur.com/QwkwfMc" rel="nofollow">http://imgur.com/QwkwfMc</a> it works on 3rd party sms gateway software but it is very expensive.</p>
  
  <p>I am getting these errors</p>
  
  <blockquote>
    <p>Fatal error: Uncaught exception 'Exception' with message 'Unable to setup COM port, check it is correct' in C:\wamp\www\sms\index.php on line 53
    ( ! ) Exception: Unable to setup COM port, check it is correct in C:\wamp\www\sms\index.php on line 53</p>
  </blockquote>
  
  <p>What do i need to do to make this work?</p>
  
  <pre><code>&lt;?php
  
  //SMS via GSM Modem - A PHP class to send SMS messages via a GSM modem attached to the computers serial port.
  
  //Windows only (tested on XP with PHP 5.2.6)
  //Tested with the EZ863 (Telit GE863) GSM modem
  //Requires that PHP has permission to access "COM" system device, and system "mode" command
  
  error_reporting(E_ALL);
  
  //Example
  
  $gsm_send_sms = new gsm_send_sms();
  $gsm_send_sms-&gt;debug = false;
  $gsm_send_sms-&gt;port = 'COM6';
  $gsm_send_sms-&gt;baud = 9600;
  $gsm_send_sms-&gt;init();
  
  $status = $gsm_send_sms-&gt;send('+642102844012', 'testing 123');
  if ($status) {
      echo "Message sent\n";
  } else {
      echo "Message not sent\n";
  }
  
  $gsm_send_sms-&gt;close();
  
  
  
  
  
  
  
  //Send SMS via serial SMS modem
  class gsm_send_sms {
  
      public $port = 'COM6';
      public $baud = 9600;
  
      public $debug = false;
  
      private $fp;
      private $buffer;
  
      //Setup COM port
      public function init() {
  
          $this-&gt;debugmsg("Setting up port: \"{$this-&gt;port} @ \"{$this-&gt;baud}\" baud");
  
          exec("MODE {$this-&gt;port}: BAUD={$this-&gt;baud} PARITY=N DATA=8 STOP=1", $output, $retval);
  
          if ($retval != 0) {
              throw new Exception('Unable to setup COM port, check it is correct');
          }
  
          $this-&gt;debugmsg(implode("\n", $output));
  
          $this-&gt;debugmsg("Opening port");
  
          //Open COM port
          $this-&gt;fp = fopen($this-&gt;port . ':', 'r+');
  
          //Check port opened
          if (!$this-&gt;fp) {
              throw new Exception("Unable to open port \"{$this-&gt;port}\"");
          }
  
          $this-&gt;debugmsg("Port opened");
          $this-&gt;debugmsg("Checking for responce from modem");
  
          //Check modem connected
          fputs($this-&gt;fp, "AT\r");
  
          //Wait for ok
          $status = $this-&gt;wait_reply("OK\r\n", 5);
  
          if (!$status) {
              throw new Exception('Did not receive responce from modem');
          }
  
          $this-&gt;debugmsg('Modem connected');
  
          //Set modem to SMS text mode
          $this-&gt;debugmsg('Setting text mode');
          fputs($this-&gt;fp, "AT+CMGF=1\r");
  
          $status = $this-&gt;wait_reply("OK\r\n", 5);
  
          if (!$status) {
              throw new Exception('Unable to set text mode');
          }
  
          $this-&gt;debugmsg('Text mode set');
  
      }
  
      //Wait for reply from modem
      private function wait_reply($expected_result, $timeout) {
  
          $this-&gt;debugmsg("Waiting {$timeout} seconds for expected result");
  
          //Clear buffer
          $this-&gt;buffer = '';
  
          //Set timeout
          $timeoutat = time() + $timeout;
  
          //Loop until timeout reached (or expected result found)
          do {
  
              $this-&gt;debugmsg('Now: ' . time() . ", Timeout at: {$timeoutat}");
  
              $buffer = fread($this-&gt;fp, 1024);
              $this-&gt;buffer .= $buffer;
  
              usleep(200000);//0.2 sec
  
              $this-&gt;debugmsg("Received: {$buffer}");
  
              //Check if received expected responce
              if (preg_match('/'.preg_quote($expected_result, '/').'$/', $this-&gt;buffer)) {
                  $this-&gt;debugmsg('Found match');
                  return true;
                  //break;
              } else if (preg_match('/\+CMS ERROR\:\ \d{1,3}\r\n$/', $this-&gt;buffer)) {
                  return false;
              }
  
          } while ($timeoutat &gt; time());
  
          $this-&gt;debugmsg('Timed out');
  
          return false;
  
      }
  
      //Print debug messages
      private function debugmsg($message) {
  
          if ($this-&gt;debug == true) {
              $message = preg_replace("%[^\040-\176\n\t]%", '', $message);
              echo $message . "\n";
          }
  
      }
  
      //Close port
      public function close() {
  
          $this-&gt;debugmsg('Closing port');
  
          fclose($this-&gt;fp);
  
      }
  
      //Send message
      public function send($tel, $message) {
  
          //Filter tel
          $tel = preg_replace("%[^0-9\+]%", '', $tel);
  
          //Filter message text
          $message = preg_replace("%[^\040-\176\r\n\t]%", '', $message);
  
          $this-&gt;debugmsg("Sending message \"{$message}\" to \"{$tel}\"");
  
          //Start sending of message
          fputs($this-&gt;fp, "AT+CMGS=\"{$tel}\"\r");
  
          //Wait for confirmation
          $status = $this-&gt;wait_reply("\r\n&gt; ", 5);
  
          if (!$status) {
              //throw new Exception('Did not receive confirmation from modem');
              $this-&gt;debugmsg('Did not receive confirmation from modem');
              return false;
          }
  
          //Send message text
          fputs($this-&gt;fp, $message);
  
          //Send message finished indicator
          fputs($this-&gt;fp, chr(26));
  
          //Wait for confirmation
          $status = $this-&gt;wait_reply("OK\r\n", 180);
  
          if (!$status) {
              //throw new Exception('Did not receive confirmation of messgage sent');
              $this-&gt;debugmsg('Did not receive confirmation of messgage sent');
              return false;
          }
  
          $this-&gt;debugmsg("Message sent");
  
          return true;
  
      }
  
  }
  
  ?&gt;
  </code></pre>