July 13, 2024, 12:33:48 AM

PHP terminal program

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

Previous topic - Next topic


Herewith a PHP program as terminal for the Duinomite.

<title>Duinomite terminal program</title>

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

// function to read out the data during either $period seconds or to a maximum of $lines whichever comes first
function readdata(){
// 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;
// the carriage return means new line
echo "<BR>";
// in HTML we have to use the non-breakable space
echo "&nbsp;";
// a tab is replaced by 8 spaces
for($loop=0;$loop<8;$loop++) echo "&nbsp;";

// 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;
$lines || $lines 2000$lines=2000;
// if called without command and without serial port start the initialisation
if($cmd=="" && $serial==""){
"<H1>CBB Terminal program</H1>\r";
"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
$fp fopen ($port,"wb");
      if (!
$fp) {
// no connection
$noc.=$port." ";
// active port
$com.=$port." ";
"<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
// 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);
// 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";
// write ASCII value 3
// read the response
// close the connection again
// 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) {
"No connection";
// End of the PHP program


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.