Your Location is: Home > Php

Warning: fopen(COM3:): failed to open stream: Permission denied. COM ports wont connect with anything

From: Belgium View: 2757 Nokinsu 

Question

I found a php script that takes the serialized data from the COM3 port and it worked yesterday, no problems at all. Next day I tried it again and I get the error:

Warning: fopen(COM3:): failed to open stream: Permission denied.

I use a microbit v1.5 that connects with the port.

I've tried severral solutions from the internet that didn't work:

  • Remove the COM port from device manager
  • Update device drivers
  • Finding out if its connected with an other device
  • Removed recent windows updates from the day before.

I thought maybe the Arduino application runs something in the background that uses the COM port but got on hold with that.

I'm pretty much out of options now I hope someone knows whats going on with this problem?

php code:


<html>
<head>
<title>Duinomite terminal program</title>
</head>
<body>

<?php
  // function to get parameters from command line ($par = parameter; $def = default value; $val = result value)
  function getp($par,$def){
    $val=$def;
    if(isset($_GET[$par]) && $_GET[$par]!=''){
      $val=$_GET[$par];
    };
    return $val;
  };

  // function to read out the data during either $period seconds or to a maximum of $lines whichever comes first
  function readdata(){
    global $fp,$period,$lines;
    $starttime=time();
    // use a non proportional font
    echo "<FONT face='courier new'>";
    $actline=0; // line counter
    while(((time()-$starttime) < $period) && ($actline < $lines)){
      $char=fread($fp,1); // read character by character
      // if you try to read a complete line a lot of 0-characters are in between;
      // now we also read a lot of 0's, but they are skipped; only printable characters are echoed
      if(ord($char)>32 && ord($char)<127){
        // a printable ASCII character
        echo $char;
      };
      if(ord($char)==13){
        // the carriage return means new line
        echo "<BR>";
        $actline++;
      };
      if(ord($char)==32){
        // in HTML we have to use the non-breakable space
        echo "&nbsp;";
      };
      if(ord($char)==9){
        // a tab is replaced by 8 spaces
        for($loop=0;$loop<8;$loop++) echo "&nbsp;";
      };
    };
    echo "</FONT>";
  };

  // First get the parameters from the command line
  $period=getp('p',2); // the read period
  $lines=getp('l',''); // the maximum no of lines
  $cmd=getp('c',''); // the command
  $serial=getp('s',''); // the serial COM port

  // if $period or $lines are 0 or above maximum set them to maximum
  if($period < 1 || $period > 180) $period=180;
  if($lines < 1 || $lines > 2000) $lines=2000;
  
  // if called without command and without serial port start the initialisation
  if($cmd=="" && $serial==""){
    echo "<H1>CBB Terminal program</H1>\r";
    echo "Testing connections ...";
    $noc="No connection on: "; // variable with no connection
    $com="Communication on: "; // variable with active ports
    $no=0; // no of active ports
    // check all COM ports from 1 to 9; unfortunately PHP does not recognise COM10 and higher
    for($loop=1;$loop<10;$loop++){
      $port="COM".$loop.":";
      $fp = fopen ($port,"wb");
      if (!$fp) {
        // no connection
        $noc.=$port." ";
      }else{
        // active port
        fclose($fp);
        $com.=$port." ";
        $no++;
      };
    };
    echo "<BR><BR>".$noc."<BR><BR>".$com."<BR>"; // listing of ports not-available and active
    echo "<FORM><SELECT><option value='NONE'>NONE"; // selector of active ports
    for($loop=0;$loop<$no;$loop++){
      // if an active port is chosen a CTRL-C is send to that port (lines=0; period=2)
      echo "<option value='".substr($com,(18+6*$loop),5)."' onClick='location.href=\"duinoterm.php?s=".substr($com,(18+6*$loop),5)."&l=0&p=2&c=CTRL-C\"'>".substr($com,(18+6*$loop),5);
    };
    echo "</SELECT></FORM>";
  }elseif($cmd=="CTRL-C"){
    // CTRL-C is special command to stop a running program; also sent with first initialisation of a port
    $fp = fopen ($serial, "rb+");
    if (!$fp) {
      // if port is not available
      echo "No connection";
    }else{
      // write ASCII value 3
      fwrite($fp,"\x03");
      // read the response
      readdata();
      // close the connection again
      fclose($fp);
    };
  }else{
    // the same as above however now with the actual BASIC command
    // Plse be aware that some characters need to be %-encoded in the URL, e.g. %=%25, &=%26, etc.
    $fp = fopen ($serial, "rb+");
    if (!$fp) {
      echo "No connection";
    }else{
      fwrite($fp,$cmd."\r");
      readdata();
      fclose($fp);
    };
  };
  // End of the PHP program
?>
</body>
</html>


Best answer