dsl36367 2017-05-04 09:12
浏览 95

PHP dio_write()函数在发送后关闭

I am trying to send data to my serial port using the php_dio.dll extention below code manage to send "HELLO WORLD" to serial com port 11 (com11) and returned the bytes, now i cannot close the serial port after it received my data.

On my com11 i have an arduino mega attached.

PHP code

$portName = '\\\\.\COM11';
$baudRate = 9600;
$bits = 8;
$spotBit = 1;

header( 'Content-type: text/plain; charset=utf-8' ); 
?>
Serial Port Test
================
<?php


function echoFlush($string)
{
    echo $string . "
";
    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);
} 

Arduino Code

char incomingByte; 

void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();

                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte);
        }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教