PHPackages                             php-io-extensions/uart - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. php-io-extensions/uart

ActivePhp-ext[Utility &amp; Helpers](/categories/utility)

php-io-extensions/uart
======================

PHP-Controllable UART Extension

v0.1.2(3w ago)07MITCPHP &gt;=8.3

Since May 16Pushed 3w agoCompare

[ Source](https://github.com/php-io-extensions/uart)[ Packagist](https://packagist.org/packages/php-io-extensions/uart)[ RSS](/packages/php-io-extensions-uart/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependenciesVersions (4)Used By (0)

UART
====

[](#uart)

[![PHP](https://camo.githubusercontent.com/91e2ff786d2fba1edf015025006e0156a071320b3662eaf2c50f39d4bb4b2369/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e332d626c7565)](https://www.php.net)[![Zephir](https://camo.githubusercontent.com/5add3cf7b546a9aa97c8f3d9f5caeda8c9fdf1bd0f90151d5b3e8e25e591ed83/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7a65706869722d302e31392532422d6f72616e6765)](https://docs.zephir-lang.com/latest/en/installation)[![Platform](https://camo.githubusercontent.com/37c663164df6eec24d0327668403be1782ab3245cf57963be37d9b6873a823a8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f706c6174666f726d2d6c696e75782532302537432532306d61634f532d6c6967687467726579)](https://www.kernel.org)

PHP-controllable UART extension built with Zephir.

The UART extension gives PHP direct control over serial ports on Linux and macOS — configuring baud rate, framing, parity, flow control, and modem control lines, then performing raw reads and writes. Useful for microcontrollers, GPS modules, RS-232/RS-485 adapters, and any other device connected over a serial UART.

```
git clone https://github.com/DeptOfScrapyardRobotics/UART
cd UART
# Raspberry Pi / Debian Trixie:
bash install-debian-trixie.sh
# NVIDIA JetPack 6 (Jetson):
bash install-jetpack6.sh
# macOS:
bash install-macos.sh
```

Requirements
------------

[](#requirements)

- PHP 8.3+ with development headers (`php-dev` / `php-devel`)
- [Zephir](https://docs.zephir-lang.com/latest/en/installation) 0.19+
- [FD extension](https://github.com/DeptOfScrapyardRobotics/FD) — provides raw integer file descriptors via `Fd\FD::open()`
- Linux or macOS with a UART device (e.g. `/dev/ttyS0`, `/dev/ttyUSB0`, `/dev/ttyAMA0`, `/dev/tty.usbserial-*`)

> **macOS note:** Standard baud rates up to 230400 are available on all platforms. Linux-specific high rates (460800, 921600, and above) are only available on Linux.

Installation
------------

[](#installation)

Install Zephir if you haven't already:

```
composer global require phalcon/zephir
```

Install the [FD extension](https://github.com/DeptOfScrapyardRobotics/FD) first — UART requires raw integer file descriptors that PHP's stream layer cannot provide:

```
git clone https://github.com/DeptOfScrapyardRobotics/FD
cd FD && bash install.sh
```

Then clone and build this extension:

```
git clone https://github.com/DeptOfScrapyardRobotics/UART
cd UART
# Raspberry Pi / Debian Trixie:
bash install-debian-trixie.sh
# NVIDIA JetPack 6 (Jetson):
bash install-jetpack6.sh
# macOS:
bash install-macos.sh
```

The install script handles the full workflow: clean → build → copy `.so` → write `30-uart.ini` into all detected `conf.d` directories → verify `php -m` → reload php-fpm if present.

To use a specific Zephir binary:

```
ZEPHIR_BIN=/path/to/zephir bash install-debian-trixie.sh
```

API
---

[](#api)

All methods are static. File descriptors are plain integers. Use the [FD extension](https://github.com/DeptOfScrapyardRobotics/FD) to open and close them — PHP's stream-based `fopen` does not produce the raw integer FDs that termios requires.

```
use Fd\FD;
use Uart\UARTConfig;
use Uart\UARTUse;

$fd = FD::open('/dev/ttyUSB0', 2); // O_RDWR = 2
// configure, then read/write ...
FD::close($fd);
```

---

### `Uart\UARTConfig`

[](#uartuartconfig)

Terminal attribute configuration on a UART file descriptor. All setters call `tcsetattr(TCSANOW)` internally — changes take effect immediately.

---

#### `setRaw(int $fd): int`

[](#setrawint-fd-int)

Puts the terminal into raw mode: no input processing, no echo, no signal generation. Equivalent to `cfmakeraw()` followed by `VMIN=1, VTIME=0`. This is the correct starting point for binary protocols. Returns `0` on success, `-1` on failure.

---

#### `setBaudRate(int $fd, int $baud): int`

[](#setbaudrateint-fd-int-baud-int)

Sets both input and output baud rate. Supported values on all platforms: `50`, `75`, `110`, `134`, `150`, `200`, `300`, `600`, `1200`, `1800`, `2400`, `4800`, `9600`, `19200`, `38400`, `57600`, `115200`, `230400`. Linux-only: `460800`, `500000`, `576000`, `921600`, `1000000`, `1152000`, `1500000`, `2000000`, `2500000`, `3000000`, `3500000`, `4000000`. Returns `0` on success, `-1` on unrecognised rate or failure.

---

#### `setDataBits(int $fd, int $bits): int`

[](#setdatabitsint-fd-int-bits-int)

Sets data bits per character: `5`, `6`, `7`, or `8`. Returns `0` on success, `-1` on invalid value or failure.

---

#### `setStopBits(int $fd, int $bits): int`

[](#setstopbitsint-fd-int-bits-int)

Sets stop bits: `1` or `2`. Returns `0` on success, `-1` on invalid value or failure.

---

#### `setParity(int $fd, int $parity): int`

[](#setparityint-fd-int-parity-int)

Sets parity mode:

ValueMode`0`None`1`Even`2`OddReturns `0` on success, `-1` on invalid value or failure.

---

#### `setFlowControl(int $fd, int $flow): int`

[](#setflowcontrolint-fd-int-flow-int)

Sets flow control mode:

ValueMode`0`None`1`Hardware (RTS/CTS via `CRTSCTS`)`2`Software (XON/XOFF via `IXON`/`IXOFF`)Returns `0` on success, `-1` on invalid value or failure.

---

#### `getAttributes(int $fd): array`

[](#getattributesint-fd-array)

Reads the full `termios` struct and returns it as an associative array. Returns `[]` on failure.

KeyTypeDescription`iflag``int`Input mode flags (`c_iflag`)`oflag``int`Output mode flags (`c_oflag`)`cflag``int`Control mode flags (`c_cflag`)`lflag``int`Local mode flags (`c_lflag`)`ispeed``int`Input baud rate constant`ospeed``int`Output baud rate constant`cc``array`Control characters array, indexed `0`–`NCCS-1`---

#### `setAttributes(int $fd, array $attrs): int`

[](#setattributesint-fd-array-attrs-int)

Applies terminal attributes from an associative array via `tcsetattr(TCSANOW)`. Accepts the same keys returned by `getAttributes()`. Partial updates are supported — only keys present in `$attrs` are applied. Returns `0` on success, `-1` on failure.

---

### `Uart\UARTUse`

[](#uartuartuse)

Data transfer and line control on a UART file descriptor.

---

#### `read(int $fd, int $bytes_to_read): string`

[](#readint-fd-int-bytes_to_read-string)

Reads up to `$bytes_to_read` bytes from the device. Blocks until at least one byte is available (when in raw mode with `VMIN=1`). Returns the bytes as a binary string, or `""` on error.

---

#### `write(int $fd, string $data, int $bytes_to_write): int`

[](#writeint-fd-string-data-int-bytes_to_write-int)

Writes `$bytes_to_write` bytes to the device. Returns the number of bytes written, or `-1` on failure.

---

#### `drain(int $fd): int`

[](#drainint-fd-int)

Blocks until all data written to `$fd` has been physically transmitted. Returns `0` on success, `-1` on failure.

---

#### `flush(int $fd, int $queue_selector): int`

[](#flushint-fd-int-queue_selector-int)

Discards queued data that has not yet been read or transmitted.

Value`queue_selector` constantEffect`0``TCIFLUSH`Discard received but unread data`1``TCOFLUSH`Discard written but untransmitted data`2``TCIOFLUSH`Discard bothReturns `0` on success, `-1` on failure.

---

#### `flow(int $fd, int $action): int`

[](#flowint-fd-int-action-int)

Suspends or resumes data flow.

Value`action` constantEffect`0``TCOOFF`Suspend output`1``TCOON`Resume output`2``TCIOFF`Send STOP character, suspend input`3``TCION`Send START character, resume inputReturns `0` on success, `-1` on failure.

---

#### `getModemBits(int $fd): int`

[](#getmodembitsint-fd-int)

Returns the current modem control line status bitmask via `TIOCMGET`, or `-1` on failure. Compare against `TIOCM_*` values:

ConstantValueLine`TIOCM_LE``0x001`Line Enable`TIOCM_DTR``0x002`Data Terminal Ready`TIOCM_RTS``0x004`Request To Send`TIOCM_CTS``0x020`Clear To Send`TIOCM_CAR``0x040`Carrier Detect`TIOCM_RNG``0x080`Ring Indicator`TIOCM_DSR``0x100`Data Set Ready---

#### `setModemBits(int $fd, int $bits): int`

[](#setmodembitsint-fd-int-bits-int)

Replaces the full modem control line state via `TIOCMSET`. Returns `0` on success, `-1` on failure.

---

#### `setBits(int $fd, int $bits): int`

[](#setbitsint-fd-int-bits-int)

Asserts (raises) specific modem control lines without affecting others, via `TIOCMBIS`. Returns `0` on success, `-1` on failure.

---

#### `clearBits(int $fd, int $bits): int`

[](#clearbitsint-fd-int-bits-int)

Deasserts (lowers) specific modem control lines without affecting others, via `TIOCMBIC`. Returns `0` on success, `-1` on failure.

---

Usage
-----

[](#usage)

```
use Fd\FD;
use Uart\UARTConfig;
use Uart\UARTUse;
```

---

### Basic send/receive at 115200 8N1

[](#basic-sendreceive-at-115200-8n1)

```
$fd = FD::open('/dev/ttyUSB0', 2); // O_RDWR

UARTConfig::setRaw($fd);
UARTConfig::setBaudRate($fd, 115200);
UARTConfig::setDataBits($fd, 8);
UARTConfig::setStopBits($fd, 1);
UARTConfig::setParity($fd, 0);       // none
UARTConfig::setFlowControl($fd, 0);  // none

UARTUse::write($fd, "AT\r\n", 4);
UARTUse::drain($fd);

$response = UARTUse::read($fd, 64);

FD::close($fd);
```

---

### Hardware flow control (RTS/CTS)

[](#hardware-flow-control-rtscts)

```
$fd = FD::open('/dev/ttyS0', 2);

UARTConfig::setRaw($fd);
UARTConfig::setBaudRate($fd, 115200);
UARTConfig::setFlowControl($fd, 1); // hardware

UARTUse::write($fd, $payload, strlen($payload));
UARTUse::drain($fd);

FD::close($fd);
```

---

### Flush and inspect attributes

[](#flush-and-inspect-attributes)

```
$fd = FD::open('/dev/ttyUSB0', 2);

UARTConfig::setRaw($fd);
UARTConfig::setBaudRate($fd, 9600);

UARTUse::flush($fd, 2); // TCIOFLUSH — clear both queues before use

$attrs = UARTConfig::getAttributes($fd);
printf("cflag=0x%08x  ispeed=%d  ospeed=%d\n",
    $attrs['cflag'], $attrs['ispeed'], $attrs['ospeed']);

FD::close($fd);
```

---

### Assert DTR, check CTS

[](#assert-dtr-check-cts)

```
$fd = FD::open('/dev/ttyS0', 2);

UARTUse::setBits($fd, 0x002);    // TIOCM_DTR — raise DTR
$bits = UARTUse::getModemBits($fd);
$cts = ($bits & 0x020) !== 0;    // TIOCM_CTS
echo $cts ? "CTS asserted\n" : "CTS not asserted\n";

UARTUse::clearBits($fd, 0x002);  // lower DTR
FD::close($fd);
```

---

License
-------

[](#license)

Copyright © Project Saturn Studios, LLC. All rights reserved.

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance95

Actively maintained with recent releases

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

3

Last Release

24d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/10563160?v=4)[Angel Gonzalez](/maintainers/projectsaturnstudios)[@projectsaturnstudios](https://github.com/projectsaturnstudios)

---

Top Contributors

[![projectsaturnstudios](https://avatars.githubusercontent.com/u/10563160?v=4)](https://github.com/projectsaturnstudios "projectsaturnstudios (4 commits)")

### Embed Badge

![Health badge](/badges/php-io-extensions-uart/health.svg)

```
[![Health](https://phpackages.com/badges/php-io-extensions-uart/health.svg)](https://phpackages.com/packages/php-io-extensions-uart)
```

###  Alternatives

[kreait/gcp-metadata

Get the metadata from a Google Cloud Platform environment.

654.9M1](/packages/kreait-gcp-metadata)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
