1. Go to this page and download the library: Download microscrap/uart library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
microscrap / uart example snippets
use Microscrap\Bindings\UART\Enums\BaudRate;
use Microscrap\Bindings\UART\Enums\TermiosQueue;
// Open /dev/ttyUSB0 at 115200 baud, configured for raw byte I/O.
$port = uart_open('/dev/ttyUSB0', BaudRate::B115200);
if (is_null($port)) {
exit("Failed to open port\n");
}
// Discard stale data before starting.
uart_flush($port, TermiosQueue::TCIOFLUSH);
// Send a command.
uart_write($port, "AT\r\n");
// Block until all bytes have been clocked out.
uart_drain($port);
// Wait briefly then read the response.
usleep(50_000);
$response = uart_read($port, 128);
echo "Response: " . trim($response) . "\n";
uart_close($port);
use Microscrap\Bindings\UART\Enums\BaudRate;
// Plain integers use the Linux BOTHER / termios2 path inside ext-posi.
$port = uart_open('/dev/ttyUSB0', 256000);
if (is_null($port)) {
exit("Failed to open port\n");
}
uart_set_baud_rate($port, BaudRate::B115200);
uart_close($port);
use Microscrap\Bindings\UART\Enums\BaudRate;
use Microscrap\Bindings\UART\Enums\ControlChar;
use Microscrap\Bindings\UART\Enums\ControlFlag;
use Microscrap\Bindings\UART\Enums\TermiosAction;
$port = uart_open('/dev/ttyAMA0', BaudRate::B9600);
// Read, mutate, and reapply termios attributes.
$t = uart_tcgetattr($port);
// Enable hardware flow control.
$t['c_cflag'] |= ControlFlag::CRTSCTS->value;
// Require at least 4 bytes per read() call.
$t['c_cc'][ControlChar::VMIN->value] = 4;
uart_tcsetattr($port, $t, TermiosAction::TCSADRAIN);
uart_close($port);
final readonly class UARTPort {
public int $fd; // open file descriptor
public string $path; // device path (/dev/ttyUSB0)
public int $baud; // numeric rate (e.g. 115200), not BaudRate::value
}