robinrosiers / serialport
Serial port access convenience class
1.2.7
2021-12-05 14:45 UTC
Requires
- php: >=7.4
Requires (Dev)
- php-mock/php-mock-phpunit: ^2.6
- phpunit/phpunit: ^9.4
This package is auto-updated.
Last update: 2025-04-06 16:51:47 UTC
README
Connect to serial port with PHP
Inspired by PHP-Serial, I simplify it and include composer.json to install via composer.
Actually, it works on linux. This library is suitable for working with Arduino.
Install via composer
composer require "robinrosiers/serialport"
How to use
You can check a full example in example folder. It contains a basic Arduino sketch and php file to read it.
Instantiate a new SerialPort object with a parser and configure tty.
<?php
use robinrosiers\SerialPort\SerialPort;
use robinrosiers\SerialPort\Parser\SeparatorParser;
use robinrosiers\SerialPort\Configure\TTYConfigure;
$serialPort = new SerialPort(new SeparatorParser(), new TTYConfigure());
$serialPort->open("/dev/ttyACM0");
while ($data = $serialPort->read()) {
echo $data."\n";
if ($data === "OK") {
$serialPort->write("1\n");
$serialPort->close();
}
}
For mac os, you must use TTYMacConfigure
. It will use stty -f
instead of stty -F
.