roozgar
roozgar

Reputation: 394

listen to serial port on ubuntu with php

i need to get some data like live temperature and wind speed from a device that connected to my ubuntu server serial port

also i need to send some command to device too ,for this i found a script like this:

<?php
$portName = 'com9:';
$baudRate = 9600;
$bits = 8;
$spotBit = 1;
header( 'Content-type: text/plain; charset=utf-8' ); 
?>
Serial Port Test
================
<?php
function echoFlush($string){
    echo $string . "\n";
    flush();
    ob_flush();
}
if(!extension_loaded('dio')){
    echoFlush( "PHP Direct IO does not appear to be installed for more info see: http://www.php.net/manual/en/book.dio.php" );
    exit;
}
try{
    //the serial port resource
    $bbSerialPort;
    echoFlush(  "Connecting to serial port: {$portName}" );
    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'){
        $bbSerialPort = dio_open($portName, O_RDWR );
        //we're on windows configure com port from command line
        exec("mode {$portName} baud={$baudRate} data={$bits} stop={$spotBit} parity=n xon=on");
    }else{
        $bbSerialPort = dio_open($portName, O_RDWR | O_NOCTTY | O_NONBLOCK );
        dio_fcntl($bbSerialPort, F_SETFL, O_SYNC);
        //we're on 'nix configure com from php direct io function
        dio_tcsetattr($bbSerialPort, array(
            'baud' => $baudRate,
            'bits' => $bits,
            'stop'  => $spotBit,
            'parity' => 0
        ));
    }
    if(!$bbSerialPort){
        echoFlush( "Could not open Serial port {$portName} ");
        exit;
    }
    // send data
    $dataToSend = "HELLO WORLD!";
    echoFlush( "Writing to serial port data: \"{$dataToSend}\"" );
    $bytesSent = dio_write($bbSerialPort, $dataToSend );
    echoFlush( "Sent: {$bytesSent} bytes" );
    //date_default_timezone_set ("Europe/London");
    $runForSeconds = new DateInterval("PT10S"); //10 seconds
    $endTime = (new DateTime())->add($runForSeconds);
    echoFlush(  "Waiting for {$runForSeconds->format('%S')} seconds to recieve data on serial port" );
    while (new DateTime() < $endTime) {
        $data = dio_read($bbSerialPort, 256); //this is a blocking call
        if ($data){
            echoFlush(  "Data Recieved: ". $data );
        }
    }
    echoFlush(  "Closing Port" );
    dio_close($bbSerialPort);
} 
catch (Exception $e){
    echoFlush(  $e->getMessage() );
    exit(1);
}
?>

this will send a command to serial port and immanently read any update from port

but i need to listen to port any time i mean each second i will receive any update from device and i must possess it and i cant wait for next update cycle

is there any way for real time port listening or i must write a wrapper with c o python for my Apache web-server?

Upvotes: 2

Views: 1365

Answers (1)

peiman F.
peiman F.

Reputation: 1658

is there any way for real time port listening or i must write a wrapper with c o python for my Apache web-server?

actually yes,its better to write a wrapper with c or python for jobs like this. python have good tools for handle exceptions that needed for listening a port

see the link for complete information: Full examples of using pySerial package

Upvotes: 1

Related Questions