如何向串行连接发送关于输出的特定答案


How to send specific answers to serial connection regarding output?

我有一个串行控制台会话,需要回答设备中的特定问题。如果我输入命令"setup",设备将启动一个安装向导,该向导会问我一些问题。我希望尽可能自动化此设置,以自动填充有关问题的值/答案。

这些问题与操作系统版本总是有点不同——也是问题的顺序。正则表达式在这里可能会有所帮助。

向串行连接的设备发送数据并读取其输出的最佳方式是什么?我试图用以下脚本解决这个问题,但它在Windows Web服务器下无法读取控制台日志:

    <!DOCTYPE html>
    <html>
        <header>
            <meta charset="utf8">
        </header>
    <body>
    <?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 = 'COM35';
    $baudRate = 9600;
    $bits = 8;
    $spotBit = 1;
    ?>
    Serial Port Test<br>
    ================<br>
    <br>
    <?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}<br><br>" );
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') 
        { 
            $bbSerialPort = dio_open('''''.'COM35', 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('''''.'COM35', 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}<br><br>");
            exit;
        }
        // send data
        $dataToSend = "version'n";
        echoFlush( "Writing to serial port data: '"{$dataToSend}'"<br>" );
        if($bytesSent = dio_write($bbSerialPort, $dataToSend )) {
            echoFlush( "Sent: {$bytesSent} bytes<br><br>" );
            echoFlush(  "Closing Port" );
            dio_close($bbSerialPort);
        }
    }
    ?>
    </body>
    </html>

我希望有人能帮助我。

注意:该设备是NetApp FAS头。

您有设备的文档吗?您应该检查一下它需要什么设置。打开实例化对象时,您需要验证以下各项的值是否正确:端口名、波特率、奇偶校验位、数据位和停止位,以及握手、换行、RtsEnable、DtrEnable和编码属性是否设置正确,然后才能进行通信。这些参数和属性中的许多都有默认值,这些默认值在大多数情况下都有效,但您的设备可能需要与默认值不同的设置。特别是,Handshake属性必须设置为正确的值,否则您将无法进行通信。如果你的设备使用硬件握手,那么在它工作之前,你将有许多项目需要验证。