天宝软件

天宝软件


链接串口demo

链接串口demo

<https://www.phpclasses.org/package/3679-PHP-Communicate-with-a-serial-port.html

"require": {
    ......
    "phpclasses/php_serial": ">=1.8"
},
"repositories":
[
    {
        "type": "composer",
        "url": "https:\/\/www.phpclasses.org\/"
    }
],
composer update
Package:    PHP Serial
Repository type:    Composer
User:   siuhoy
Password:   494599d5a302dfbe4788c2f1516ee085

SerialController.php

<?php

namespace App\Http\Controllers;

class SerialController extends Controller
{
    public function index()
    {
        require_once('php_serial.class.php');
        $serial = new \phpSerial();
        $serial->deviceSet("COM1");            // 设置串口设备为 "COM1"
        $serial->confBaudRate(9600);           // 设置波特率为2400
        //$serial->confParity(8);                // 设置奇偶校验位为8
        $serial->confCharacterLength(8);       // 设置字符长度为8位。
        $serial->confStopBits(1);              // 设置停止位为1。
        $serial->confFlowControl("none");      // 设置流控制为 "none"
        $serial->deviceOpen();                 // 打开设备进行串口通信。
        $serial->sendMessage("Hello tbrj!");   // 通过串口发送消息
        $read = $serial->readPort();           // 从串口读取数据
        $serial->deviceClose();                // 关闭串口设备,结束通信。

        // 返回接收到的数据
        return response()->json(['data_received' => $read]);
    }
}

文档实例 Example

vendor/phpclasses/php_serial/example.php

<?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->deviceSet("COM1");

// We can change the baud rate, parity, length, stop bits, flow control
$serial->confBaudRate(2400);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");

// Then we need to open it
$serial->deviceOpen();

// To write into
$serial->sendMessage("Hello !");

State of the project



State of the project
--------------------

页面列表

ITEM_HTML