警告:指定的串行端口在第127行的php_serial.class.php中无效

问题描述 投票:1回答:2

我试图使用PHP与串口通信,我的代码是,

<?php error_reporting(E_ALL); ini_set('display_errors', '1'); ?>
<?php
include "php_serial.class.php";
use phpSerial\phpSerial;
$serial = new phpSerial();
$serial->deviceSet("COM1");
$serial->confBaudRate(2400);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");
$serial->deviceOpen();
$serial->sendMessage("Hello !");
$read = $serial->readPort();
$serial->deviceClose();
$serial->confBaudRate(2400);

我需要在我的问题中包含我的php_serial.class.php吗?我的代码有什么问题?

php port
2个回答
1
投票

如果您使用的是linux,则必须使用其他设备:

$serial->deviceSet("/dev/ttyS0"); // or /dev/ttyS1, ...

0
投票

有时即使您正确输入串行端口,也可能在Linux上出现此错误。在这种情况下,您应该允许通过输入sudo chmod 0777 /dev/YOUR_SERIAL_PORT来读取终端中的此端口,其中YOUR_SERIAL_PORT是您的串行端口,例如:ttyS0。另外,不要忘记输入sudo usermod -a -G dialout www-data来授予PHP使用串口。

© www.soinside.com 2019 - 2024. All rights reserved.