December 08, 2024, 01:33:45 AM

PHP terminal program

Started by KeesZagers, January 31, 2013, 03:27:19 PM

Previous topic - Next topic

KeesZagers

Herewith a PHP program as terminal for the Duinomite.


<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 || $period 180$period=180;
  if(
$lines || $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>



I have it running under Windows XP with an Apache WEB server and PHP 5. I don't believe it will be a problem to have it running under Windows Vista, 7 or 8.
The program will check the COM ports 1 to 9 for communication. One of the active ports can be selected and the default parameters are filled for the next command.

There are a few restrictions:

1. COM ports can not be higher than 9. PHP does not recognise ports 10 and higher
2. After (re-)initialisation of the communication on a COM port, there should be first some communication with some standard terminal program (Teraterm, hyperterminal, etc). This will set the read timeout to some milliseconds and this will stay after closing of the terminal program. You should do that otherwise the port cannot be used by this program. (Still looking for some utility to do this within the PHP program; MODE COMx can set timeout to ON, but then it will be 1 minute, which will not work).
3. The HTML code will showed when the PHP code is finished. Therefore we have to open and close the port everytime we want to show something. So we can only show blocks of data and not streaming data like on a terminal.

I will use this program within a hidden <IFRAME> and use the results for graphic presentation in the main window, but ofcourse all kind of other applications can be built around it.

I'm open for further suggestions.

Kees