我已经在 fedora 13 中安装了 xampp.我正在尝试使用 php 串行类通过串行端口与微控制器通信。我的代码是示例.php
include("php_serial.class.php");
$serial = new phpSerial();
$serial->deviceSet("0");
$serial->confBaudRate(9600); //Baud rate: 9600
$serial->confParity("none"); //Parity (this is the "N" in "8-N-1")
$serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1")
$serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1")
$serial->confFlowControl("none"); //Device does not support flow control of any kind, so set it to none.
//Now we "open" the serial port so we can write to it
$serial->deviceOpen();
$serial->sendMessage("*1" ); //sleep(1); // echo "hi"; $serial->deviceClose();
?>
php脚本被执行,但给出以下警告。
警告:指定的串行端口在第 147 行的/opt/lampp/htdocs/xampp/php_serial.class.php 中无效警告:无法设置波特率:设备未设置或在 241 行的/opt/lampp/htdocs/xampp/php_serial.class.php 中打开警告:无法设置奇偶校验:设备未设置,或在 295 行的/opt/lampp/htdocs/xampp/php_serial.class.php 中打开
。我使用命令:chmod 0777/dev/ttyUSB0 来授予权限。 我还尝试使用以下命令将 apache 用户"prudhvi"添加到拨出组: $ usermod -a -G dialout prudhvi
但它不起作用. 当我使用以下命令直接从终端发送命令时:echo 1>/dev/ttyUSB0 它可以工作,并且"1"被传输到串行端口。但是使用 php 我收到上述警告.
我使用"$whoami"检查用户名,并将该用户"prudhvi"添加到拨出组。它仍然不起作用.请帮助我伙计们。
我曾经用 Debian 用 PHP 脚本控制 Arduino 板,最初遇到了同样的问题。
在 Debian 中,您需要将 Apache 用户添加到拨出组中,以便允许它发出串行连接请求。我认为 Fedora 也是如此。
在 Debian 中,命令是:
useradd -G dialout www-data
然而,我相信Fedora将Apache用户命名为apache。我没有 Fedora 机器可以测试,但我认为您需要运行的命令是:
useradd -G dialout apache
然后,您将需要重新启动xampp服务器。
请参阅以下内容以供参考:
http://www.cyberciti.biz/faq/howto-linux-add-user-to-group/http://fedoraproject.org/wiki/Administration_Guide_Draft/Apache#Apache_File_Security
尼尔
你能在第 147 行发布与"/opt/lampp/htdocs/xampp/php_serial.class.php"相关的行吗?
我怀疑您正在尝试错误地设置设备(如马克所示(。该端口或该端口已在您同时进行的其他测试中使用。我不确定您正在运行的脚本是否提供了特定于您尝试附加到已在使用的端口的错误。
首先测试一个hello world类型的php脚本来测试你的基本安装。
然后验证Web服务器/php引擎是否以允许用户访问与串行端口对应的适用/dev/ttyANYTHING设备文件的用户身份运行。 如果默认情况下这是真的,那将是令人惊讶的 - 您可能必须将其添加到"拨出"或类似组中。
在代码中添加一些错误检查/报告。
归功于 Marc B 的评论让我查找这个,他死了:http://www.phpclasses.org/browse/file/17926.html
function deviceSet ($device)
{
if ($this->_dState !== SERIAL_DEVICE_OPENED)
{
if ($this->_os === "linux")
{
if (preg_match("@^COM('d+):?$@i", $device, $matches))
{
$device = "/dev/ttyS" . ($matches[1] - 1);
}
if ($this->_exec("stty -F " . $device) === 0)
{
$this->_device = $device;
$this->_dState = SERIAL_DEVICE_SET;
return true;
}
}
elseif ($this->_os === "windows")
{
if (preg_match("@^COM('d+):?$@i", $device, $matches) and $this->_exec(exec("mode " . $device)) === 0)
{
$this->_windevice = "COM" . $matches[1];
$this->_device = "''.'com" . $matches[1];
$this->_dState = SERIAL_DEVICE_SET;
return true;
}
}
trigger_error("Specified serial port is not valid", E_USER_WARNING);
return false;
}
else
{
trigger_error("You must close your device before to set an other one", E_USER_WARNING);
return false;
}
}
我相信调用$serial->deviceSet("/dev/ttyUSB0");
会修复它,但您可能需要修改php_serial.class.php
源才能处理/dev/ttyUSB
而不是/dev/ttyS
。