title: PHP - Reading COM Port from Windows
body: <p>The following is a library for serial communications via PHP: <a href="http://www.phpclasses.org/package/3679-PHP-Communicate-with-a-serial-port.html">http://www.phpclasses.org/package/3679-PHP-Communicate-with-a-serial-port.html</a>. The problem is that the method readPort is not fully implemented. It can read in a *nix environment, but apparently not in a Windows environment. The method:</p>
  
  <pre><code>/**
   * Reads the port until no new datas are availible, then return the content.
   *
   * @pararm int $count number of characters to be read (will stop before
   *  if less characters are in the buffer)
   * @return string
   */
  function readPort ($count = 0)
  {
      if ($this-&gt;_dState !== SERIAL_DEVICE_OPENED)
      {
          trigger_error("Device must be opened to read it", E_USER_WARNING);
          return false;
      }
  
      if ($this-&gt;_os === "linux")
      {
          $content = ""; $i = 0;
  
          if ($count !== 0)
          {
              do {
                  if ($i &gt; $count) $content .= fread($this-&gt;_dHandle, ($count - $i));
                  else $content .= fread($this-&gt;_dHandle, 128);
              } while (($i += 128) === strlen($content));
          }
          else
          {
              do {
                  $content .= fread($this-&gt;_dHandle, 128);
              } while (($i += 128) === strlen($content));
          }
  
          return $content;
      }
      elseif ($this-&gt;_os === "windows")
      {
          /* Do nohting : not implented yet */
      }
  
      trigger_error("Reading serial port is not implemented for Windows", E_USER_WARNING);
      return false;
  }
  </code></pre>
  
  <p>The author states:</p>
  
  <blockquote>
    <p>==> /!\ WARNING /!\ : it's working with linux for r/w, but with windows i've only been able to make write working. If you're a windows
    user, try to access the serial port through network with serproxy
    instead.</p>
  </blockquote>
  
  <p>The limitation of the PHP class library has been mentioned in SO several times already. I haven't found a decent solution. Reading is imperative for my application.</p>
  
  <p><strong>Does anyone here know what to do?</strong></p>