3

I'm working on a project which involves reading and writing to a Serial board, using the UART pins on my Raspberry Pi. However, I have hit a brick wall already. Any time I try use PhpSerial I always get the error:

Fatal error: No stty available, unable to run. in /var/www/PHP-Serial/examples/PhpSerial.php on line 56

I've tried numerous configurations with the input:

// 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 is running as www-data, Ive tried chowning the /dev/ttyAMA0 to that user, and I've added the dialout group to said user. I cant see any disable functions or anything in my php.ini. I've also don't the standard setup for using serial devices on the pi as per the wiki, and I am able to read/write data to and from the circuit using

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

Here are the line(s) that the error is referring to:

    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
            );
        }

I can't make sense of it but someone else might. Thanks in advance.

4

3 回答 3

4

您的问题的解决方案如下:

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

从:

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

至:

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

=> 因此,这解决了“没有可用的 stty,无法运行...”错误。看到这个线程:https ://www.raspberrypi.org/forums/viewtopic.php?f=91&t=100481

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

<?php

错误报告(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";
于 2015-08-31T14:29:41.620 回答
0

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

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

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

    stty -F /dev/ttyAMA0 38400

    exec("cat filename.txt > //dev//ttyAMA0");
于 2014-09-08T14:53:48.967 回答
0

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

于 2014-04-28T09:39:14.117 回答