PHP comm port checker

Words
232
Reading
2 min
9y
Listen to this post
~2 min

Hello to everyone my name is eresbos,

Coded for ;

  • I encoded it so that port errors could be resolved faster.

Comm port checker coded for your computer ports testing.If an error occurs when a device is added.You can solve it more quickly. If port is off you see this mesagge 'Your comm-port not active.Check your connection'. You can check the following port list :

  • '2400',
  • '4800',
  • '9600',
  • '19200',
  • '38400',
  • '57600',
  • '115200',
  • '230400',


-Used functions ;

  • shell_exec : Execute command via shell and return the complete output as a string
  • substr_count : Count the number of substring occurrences
  • explode : Split a string by string
  • count : Count all elements in an array, or something in an object
  • str_replace : Replace all occurrences of the search string with the replacement string
  • substr : Return part of a string
  • implode : Join array elements with a string
  • trim : Strip whitespace (or other characters) from the beginning and end of a string
  • isset : Determine if a variable is set and is not NULL


  • index.php
<?php

function comm_list() {

        $commport = shell_exec('mode');

        if(substr_count($commport,'COM')<1) {
            $commport_list[0] = 'None';
        } else {

            $conn = explode(' ',$commport);
            $count = count($conn);
            for($i=0;$i<$count;$i++) {
                if(substr_count($conn[$i],'COM')<1) {
                    $commport_list[$i] = '';
                } else {
                    $commport_list[$i] = str_replace(':','',substr($conn[$i],0,5)).'-';
                }
            }

        }

        $commport = implode('',$commport_list);
        $commport = trim($commport);
        $commport = trim(str_replace('-',' ',$commport));
        $commport_list = explode(' ',$commport);

        return $commport_list;
    }

    function baud_list() {

        $baud_list = array(
            '2400',
            '4800',
            '9600',
            '19200',
            '38400',
            '57600',
            '115200',
            '230400',
        );

        return $baud_list;
    }

if(isset($_POST['comm'])) {

    if($_POST['comm']=='None') {
        echo 'Your comm-port not active.Check your connection<br />
        (My computer right click - Properties - Hardware - Device Manager - Ports (COM & LPT))<br />
        If your comm-port is active make sure that it is not being used';
    } else {
        echo 'Port = '.$_POST['comm'].'<br /> Baud Rate = '.$_POST['baud']."<br/>Port Open.";

    }


} else {

    $commport_list = comm_list();
    $baud_list = baud_list();

    $count = count($commport_list);
    $count2 = count($baud_list);

    echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">
    Port <select name="comm">';
    for($i=0;$i<$count;$i++) {
        echo '<option value="'.$commport_list[$i].'">'.$commport_list[$i].'</option>';  
    }
    echo '</select> Baud Rate <select name="baud">';
    for($i=0;$i<$count2;$i++) {
        echo '<option value="'.$baud_list[$i].'">'.$baud_list[$i].'</option>';  
    }
    echo '</select> <input type="submit" value="Select" />
    </form>
    <hr />';

}

?>
  • Controllable port list

1.png

  • If port 2400 is open

2.png

  • If port 4800 is open

3.png

  • If port 9600 is open

4.png

  • If pot 19200 is open

5.png

  • If port 38400 is open

6.png

  • If port 57600 is open

7.png

  • If port 115200 is open

8.png

  • If port 230400 is open

9.png

Github url : https://github.com/eresbos/commportchecker



Posted on Utopian.io - Rewarding Open Source Contributors