繁体   English   中英

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

[英]is it possible to get hit from serial port to PHP file

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

dio php extension是您需要的。您可以这样安装

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串行存储库。

在这里您可以看到示例代码:

<?php
include 'PhpSerial.php';

// Let's start the class
$serial = new PhpSerial;

// First we must specify the device. This works on both linux and windows (if
// your linux serial device is /dev/ttyS0 for COM1, etc)
$serial->deviceSet("COM1");

// We can change the baud rate, parity, length, stop bits, flow control
$serial->confBaudRate(2400);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");

// Then we need to open it
$serial->deviceOpen();

// To write into
$serial->sendMessage("Hello !");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM