1. Go to this page and download the library: Download zeran/async-serial-port 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/ */
zeran / async-serial-port example snippets
use React\EventLoop\Loop;
use Zeran\SerialPort\AsyncSerialPort;
use Zeran\SerialPort\SerialPortConfiguration;
use Zeran\SerialPort\Enum\BaudRateEnum;
use Zeran\SerialPort\Enum\EventEnum;
// Create event loop
$loop = Loop::get();
// Configure serial port
$config = new SerialPortConfiguration(
device: '/dev/ttyUSB0', // Change to your device path
platform: PHP_OS,
baud: BaudRateEnum::BAUD_115200
);
// Optional: Configure additional parameters
$config->dataBits = \Zeran\SerialPort\Enum\BitsEnum::DATA_BITS_8;
$config->stopBits = \Zeran\SerialPort\Enum\BitsEnum::STOP_BITS_1;
$config->parity = \Zeran\SerialPort\Enum\ParityEnum::PARITY_NONE;
$config->xon = false;
$config->octs = false;
$config->rts = false;
$config->dtr = false;
$config->chunkSize = 256;
// Create serial port with event handler
$serialPort = new AsyncSerialPort($config, $loop, function (EventEnum $event, $data) {
switch ($event) {
case EventEnum::DATA->value:
echo "Received data: " . bin2hex($data) . PHP_EOL;
break;
case EventEnum::ERROR->value:
echo "Error: " . $data->getMessage() . PHP_EOL;
break;
case EventEnum::CLOSE->value:
echo "Port closed" . PHP_EOL;
break;
case EventEnum::WRITE->value:
echo "Data written successfully" . PHP_EOL;
break;
}
});
try {
// Open the serial port
$serialPort->open();
// Write data to the port
$serialPort->write("AT\r\n");
// Run the event loop
$loop->run();
} catch (\Zeran\SerialPort\Exception\SerialPortException $e) {
echo "Serial port error: " . $e->getMessage() . PHP_EOL;
}
use Zeran\SerialPort\Enum\BaudRateEnum;
$config = new SerialPortConfiguration(
device: '/dev/ttyUSB0',
platform: PHP_OS,
baud: BaudRateEnum::BAUD_9600
);
use Psr\Log\LoggerInterface;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// Create a logger
$logger = new Logger('serial_port');
$logger->pushHandler(new StreamHandler('path/to/your.log', Logger::DEBUG));
// Pass it to the serial port
$serialPort = new AsyncSerialPort($config, $loop, $callback);
$serialPort->setLogger($logger);
$logger = new Logger('serial_port');
$logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));
$serialPort->setLogger($logger);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.