链接串口demo
链接串口demo
<https://www.phpclasses.org/package/3679-PHP-Communicate-with-a-serial-port.html
&quot;require&quot;: {
......
&quot;phpclasses/php_serial&quot;: &quot;&gt;=1.8&quot;
},
&quot;repositories&quot;:
[
{
&quot;type&quot;: &quot;composer&quot;,
&quot;url&quot;: &quot;https:\/\/www.phpclasses.org\/&quot;
}
],
composer update
Package: PHP Serial
Repository type: Composer
User: siuhoy
Password: 494599d5a302dfbe4788c2f1516ee085
SerialController.php
&lt;?php
namespace App\Http\Controllers;
class SerialController extends Controller
{
public function index()
{
require_once('php_serial.class.php');
$serial = new \phpSerial();
$serial-&gt;deviceSet(&quot;COM1&quot;); // 设置串口设备为 &quot;COM1&quot;
$serial-&gt;confBaudRate(9600); // 设置波特率为2400
//$serial-&gt;confParity(8); // 设置奇偶校验位为8
$serial-&gt;confCharacterLength(8); // 设置字符长度为8位。
$serial-&gt;confStopBits(1); // 设置停止位为1。
$serial-&gt;confFlowControl(&quot;none&quot;); // 设置流控制为 &quot;none&quot;
$serial-&gt;deviceOpen(); // 打开设备进行串口通信。
$serial-&gt;sendMessage(&quot;Hello tbrj!&quot;); // 通过串口发送消息
$read = $serial-&gt;readPort(); // 从串口读取数据
$serial-&gt;deviceClose(); // 关闭串口设备,结束通信。
// 返回接收到的数据
return response()-&gt;json(['data_received' =&gt; $read]);
}
}
文档实例 Example
vendor/phpclasses/php_serial/example.php
&lt;?php
include 'PhpSerial.php';
// Let's start the class
$serial = new PhpSerial;
// 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-&gt;deviceSet(&quot;COM1&quot;);
// We can change the baud rate, parity, length, stop bits, flow control
$serial-&gt;confBaudRate(2400);
$serial-&gt;confParity(&quot;none&quot;);
$serial-&gt;confCharacterLength(8);
$serial-&gt;confStopBits(1);
$serial-&gt;confFlowControl(&quot;none&quot;);
// Then we need to open it
$serial-&gt;deviceOpen();
// To write into
$serial-&gt;sendMessage(&quot;Hello !&quot;);
State of the project
State of the project
--------------------