Download the PHP package sanchescom/php-serial without Composer

On this page you can find all versions of the php package sanchescom/php-serial. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-serial

php-serial

CI Latest Version Downloads PHP Version

Serial port for PHP 8.2+: open, configure, write, read with a timeout. Linux, macOS, Windows. No extensions, no dependencies.

One object opens the device and applies the line settings (stty on Linux and macOS, mode on Windows); after that it is write() and readLine(timeout: …). The timeout is the point: every read call says how long it may wait, TimeoutException is a real exception, and bytes that arrived before the deadline are kept for the next call. The suite runs on real pseudo-terminals through socat, and every release is verified on a Raspberry Pi UART before it is tagged; the transcript is in docs/verified-on.md.

Loopback on a Raspberry Pi

Run over SSH on the maintainer's Pi as an unprivileged user (femus, member of dialout), GPIO14 jumpered to GPIO15, full transcript in docs/verified-on.md:

The second run shows what a timeout looks like against a device that never answers: the loopback echoes AT back, then readLine(timeout: 2.0) throws after two seconds instead of hanging.

Install

Usage

The constructor opens the device and applies the line settings; everything after that is reading and writing bytes. This is examples/at-command.php:

Named arguments keep a full configuration readable (Parity, StopBits and FlowControl live in the same namespace):

The reading methods differ only in when they stop waiting:

Settings

Parameter Type Default Values
$device string — /dev/ttyUSB0, /dev/cu.usbserial-1410, COM3 (also COM3: and \\.\COM3)
$baudRate int 57600 any positive integer the driver accepts: 300, 1200, 9600, 19200, 38400, 57600, 115200, …
$dataBits int 8 5, 6, 7, 8
$parity Parity Parity::None Parity::None, Parity::Even, Parity::Odd
$stopBits StopBits StopBits::One StopBits::One, StopBits::Two
$flowControl FlowControl FlowControl::None FlowControl::None, FlowControl::RtsCts, FlowControl::XonXoff

A baud rate below 1 or a data-bits value outside 5–8 throws an \InvalidArgumentException. A device that cannot be opened, or a stty/mode call that fails, throws a SerialException.

Timeouts

Timeouts are per call, in seconds, and accept fractions (0.25 is 250 ms). A negative timeout throws an \InvalidArgumentException.

Finding ports

SerialPortLocator::candidates() returns a sorted, unique list of devices that look openable. It is a candidate list, not a detector: nothing is opened and nothing is probed.

Entries containing Bluetooth-Incoming, debug or wlan are dropped, because they are never the port you meant.

Platforms

Platform Configured with Device name Status
Linux stty -F <device> … /dev/ttyUSB0, /dev/serial0 Verified on a Raspberry Pi, see docs/verified-on.md
macOS stty -f <device> … /dev/cu.usbserial-1410 Verified through socat pseudo-terminals in the test suite
Windows mode COMn BAUD=… … COM3, opened as \\.\COM3 Written and unit-tested, not yet verified on hardware; read timeouts are not enforced

On Windows PHP cannot stream_select() a file handle and non-blocking mode is not implemented for one, so read(), readUntil() and readLine() do not enforce their $timeout there: fread() blocks according to the COM port's own timeouts. readUntil() and readLine() stop at the deadline once fread() returns; read() blocks inside fread() for as long as the driver takes. Everything else — opening, configuring, writing, buffering — works the same way on all three platforms.

How it works

The device is opened with fopen() first and configured second, because on macOS the termios settings applied by stty are discarded once the device is fully closed; holding the handle open while stty runs is what makes them stick. The same order is used on Linux, where it is harmless.

The stty command starts with raw -echo and puts the explicit flags after it, so the explicit flags win. This matters on both systems for different reasons: on macOS raw is cfmakeraw(), which would otherwise reset cs8 and the parity bits, and on GNU stty it resets ixon/ixoff, which would otherwise undo the requested flow control.

On Windows the port is opened as \\.\COMn and configured with mode COMn BAUD=… PARITY=… DATA=… STOP=… xon=… octs=… rts=… dtr=on to=off.

No PHP extension is required: the library uses plain stream functions plus one external command per port. That command runs through exec(), so on a hardened installation where exec() is in disable_functions the constructor throws a SerialException saying exec() is disabled instead of dying with a fatal error.

Upgrading from 2.x

3.0 is a rewrite with a different API. See UPGRADE.md for the method-by-method mapping.

Origin and license

Versions 1.x and 2.x were a refactoring of Xowap/PHP-Serial and were licensed under GPL-3.0. Version 3.0 was written from scratch and is licensed under the MIT License, see LICENSE.md. No code from 2.x or from PHP-Serial remains.

Contributing

See CONTRIBUTING.md. Before sending a pull request:

The pseudo-terminal tests need socat (brew install socat / apt install socat); they are skipped when it is missing.


All versions of php-serial with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package sanchescom/php-serial contains the following files

Loading the files please wait ...