fgets on COM1

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I'm trying to do a script which write and read to/from COM1.
All works fine when something is connected on COM1, but when I disconnect the rs232 cable or it fails, the script try to do fgets and never stop. set_mode command works, write command works, but fgets don't say that nothing is connected, and the script continue trying to read from COM1.
I have to kill the php task to liberate COM1

How can I solve this problem?

Code:
//---------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------
//configuro el puerto
$set_mode = "MODE COM$numcom: BAUD=38400 PARITY=N DATA=8 STOP=1 TO=OFF XON=OFF ODSR=OFF OCTS=OFF DTR=OFF RTS=OFF IDSR=OFF";
exec($set_mode, $output, $result);
#compruebo el resultado del exec comando mode
switch ($result) {
     case 0:
              echo "COM$numcom configurado correctamente.<br>";
               break;
     default :
              echo "No se ha podido configurar el COM$numcom.<br>";
               $error_configuracion = true;
}
if (!$error_configuracion) {
      //abro el puerto
     $serial_port = fopen("COM$numcom", "wb+");

   if (isset($serial_port)) {
//si ha podido abrir la conexion ejecuto comandos y leo sus respuestas $comando = "MICOMANDO";
         //Ejecuto el comando
       $result = fwrite($serial_port, "* $comando\r\n");
       //echo $result;
       if (!$result) {
            //no puede escribir
            echo "Error escribiendo en COM$numcom <br>";
      } else {
         //voy leyendo el resultado de mi comando
          $buffer = fgets($serial_port, 1024);
           while (trim($buffer)!="") {
               //echo "BUFFER=$buffer<br>";
              $res .= ";$buffer";
               $buffer = fgets($serial_port, 1024);
           }
           //echo "*$res*<br>";
} }
}
//---------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------

thanks

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux