Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从串行端口到PHP文件是否可能被命中_Php_Port - Fatal编程技术网

从串行端口到PHP文件是否可能被命中

从串行端口到PHP文件是否可能被命中,php,port,Php,Port,我的机器通过串行端口连接到我的系统。当机器通过串口触发一条消息时,我必须点击一个PHP文件,这可能吗?dioPHP扩展是您需要的。您可以这样安装它 sudo pecl install dio-0.0.7 这是一个代码示例: <?php //-- settings --// //brainboxes serial ports //on 'nix start with cu.usbserial- //on windows starts with com : must be lower c

我的机器通过串行端口连接到我的系统。当机器通过串口触发一条消息时,我必须点击一个PHP文件,这可能吗?

dio
PHP扩展是您需要的。您可以这样安装它

sudo pecl install dio-0.0.7
这是一个代码示例:

<?php

//-- settings --//

//brainboxes serial ports
//on 'nix start with cu.usbserial-
//on windows starts with com : must be lower case in windows and end with a colon
$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 //'nix
    {
        $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);
} 

?>

dio
php扩展是您所需要的。您可以这样安装它

sudo pecl install dio-0.0.7
这是一个代码示例:

<?php

//-- settings --//

//brainboxes serial ports
//on 'nix start with cu.usbserial-
//on windows starts with com : must be lower case in windows and end with a colon
$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 //'nix
    {
        $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);
} 

?>

但是在这种情况下需要手动触发php文件,对吗?要使其自动,您需要实现javascript代码来调用能够定期读取的php代码。但是在这种情况下需要手动触发php文件,对吗?要使其自动,您需要实现javascript代码来调用能够定期读取的php代码可以定期阅读。