gregorj/serial-port 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gregorj/serial-port

Composer 安装命令:

composer require gregorj/serial-port

包简介

PHP interfaces and classes for serial port communication.

README 文档

README

License: MIT PHPStan level Code Style

PHP classes to connect to serial devices using streams or HTTP(S) gateways.

This library separates the command model from the transport:

  • Command defines command payload, terminators, timeout, and response mapping
  • Communication executes the command (StreamCommunication or HttpCommunication) via query() or write()
  • transport is provided by either Stream (for sockets) or HttpTransport (for gateways)

BasicStringCommand, BasicVoidCommand, and StringResponse are reference implementations for common command/response patterns.

Usage

You can either:

  1. bridge a serial device to TCP (for example with pySerial tcp_serial_redirect) and use TcpStream
  2. call an HTTP(S) serial gateway and use HttpCommunication with CurlTransport or StreamWrapperTransport

TCP stream communication

<?php

use GregorJ\SerialPort\Commands\BasicStringCommand;
use GregorJ\SerialPort\StreamCommunication;
use GregorJ\SerialPort\TcpStream;

$stream = new TcpStream('127.0.0.1', 5000);
$communication = new StreamCommunication($stream);

$command = new BasicStringCommand('HELLO', "\n", "\n");
$response = $command->invoke($communication);

echo $response?->get('response');

HTTP gateway communication

<?php

use GregorJ\SerialPort\Commands\BasicStringCommand;
use GregorJ\SerialPort\CurlTransport;
use GregorJ\SerialPort\HttpCommunication;

$communication = new HttpCommunication(
	new CurlTransport(),
	'https://example.com/query',
	'ttyUSB0',
	HttpCommunication::DEVICE_TYPE_WIRED
);

$command = new BasicStringCommand('HELLO', "\n", "\n");
$response = $command->invoke($communication);

echo $response?->get('response');

HttpCommunication::setTimeout() configures the serial-device response timeout and sends it to the gateway as deviceTimeoutMs. HTTP transport timeouts (connect and request) are configured separately in CurlTransport and StreamWrapperTransport.

Fire-and-forget commands

If your command does not expect a response, call Communication::write() directly (or use BasicVoidCommand, which now delegates to write()):

<?php

use GregorJ\SerialPort\Commands\BasicVoidCommand;
use GregorJ\SerialPort\StreamCommunication;
use GregorJ\SerialPort\TcpStream;

$communication = new StreamCommunication(new TcpStream('127.0.0.1', 5000));
$command = new BasicVoidCommand('AT+RESET', "\r\n");
$command->invoke($communication);

For custom command types, implement GregorJ\SerialPort\Interfaces\Command and decide per command whether to call query() or write().

For the expected HTTP JSON contract and fields, see src/Http/JsonSerialGatewayContract.php.

gregorj/serial-port 适用场景与选型建议

gregorj/serial-port 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 37 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 gregorj/serial-port 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gregorj/serial-port 我们能提供哪些服务?
定制开发 / 二次开发

基于 gregorj/serial-port 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 37
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 37
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-30