PhpSerial:没有可用的stty——似乎无法让它发挥作用


PhpSerial: No stty available -- cant seem to get it working

我正在进行一个项目,该项目涉及使用Raspberry Pi上的UART引脚对串行板进行读写。然而,我已经碰壁了。每当我尝试使用PhpSerial时,我总是会收到错误:

致命错误:没有可用的stty,无法运行。在第56行上的/var/www/PHP Serial/examples/PhpSerial.PHP中

我已经尝试了许多输入配置:

// 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("/dev/ttyAMA0");
// We can change the baud rate, parity, length, stop bits, flow control
$serial->confBaudRate(38400);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");

php/lighthttpd作为www数据运行,我尝试将/dev/ttyAMA0切换到该用户,并将拨号组添加到该用户。我在php.ini中看不到任何禁用功能或任何东西。我也没有按照wiki在pi上使用串行设备的标准设置,我可以使用在电路中读写数据

sudo minicom-b 38400-o-D/dev/ttyAMA0

以下是错误所指的行:

    if (substr($sysName, 0, 5) === "Linux") {
        $this->_os = "linux";
        if ($this->_exec("stty") === 0) {
            register_shutdown_function(array($this, "deviceClose"));
        } else {
            trigger_error(
                "No stty available, unable to run.",
                E_USER_ERROR
            );
        }

我听不懂,但其他人可能会。提前谢谢。

问题的解决方案如下:

您必须更改PhpSerial.php类中的以下代码行

FROM:

if ($this->_exec("stty") === 0) {

TO:

if ($this->_exec("stty --version") === 0) {

=>这因此解决了";没有可用的stty,无法运行"错误请参阅此线程:https://www.raspberrypi.org/forums/viewtopic.php?f=91&t=100481

我还应该补充一点,在我写串行数据之前,我不得不放置一个延迟,例如

<?php

error_reporting(E_ALL);ini_set('display_errors','1');

include "PhpSerial.php";//serial class: https://github.com/Xowap/PHP-Serial/blob/develop/examples/VS421CPNTA.php
$serial = new phpSerial;
//$serial->deviceSet("/dev/ttyAMA0");
$serial->deviceSet("/dev/ttyACM0");
$serial->confBaudRate(9600);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->deviceOpen();
sleep(3);//delay
$serial->sendMessage("1");
$serial->deviceClose();
echo "Serial message sent! 'n";

如您所见,PhpSerial需要stty实用程序来获取/设置baudrate、奇偶校验等串行参数。解决方案是通过Linux发行版安装stty

Rasbian上的STTY在exec上返回1,而不是0

不幸的是,如果您只是绕过这段代码,它就会挂在register_shutdown_function上。

目前,我正在将文件写入磁盘,并试图将它们发送到端口(因为它们是二进制文本而不是ascii,所以很困难)。如果你有ascii信息要发送,那么

    stty -F /dev/ttyAMA0 38400

    exec("cat filename.txt > //dev//ttyAMA0");