44 lines
1.2 KiB
PHP
44 lines
1.2 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace NoccyLabs\React\Serial;
|
||
|
|
|
||
|
|
use Evenement\EventEmitterTrait;
|
||
|
|
use React\EventLoop\Loop;
|
||
|
|
use React\Promise\Deferred;
|
||
|
|
use React\Promise\PromiseInterface;
|
||
|
|
use React\Stream\DuplexResourceStream;
|
||
|
|
use React\Stream\ReadableStreamInterface;
|
||
|
|
use React\Stream\WritableStreamInterface;
|
||
|
|
|
||
|
|
class SerialFactory
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
*
|
||
|
|
*
|
||
|
|
* @param string $device
|
||
|
|
* @param int $baud
|
||
|
|
* @param int $bits Bits per byte
|
||
|
|
* @param Parity $parity
|
||
|
|
* @param int $stop Stopbits
|
||
|
|
* @return PromiseInterface<DuplexResourceStream>
|
||
|
|
*/
|
||
|
|
public function open(string $device, int $baud = 9600, int $bits = 8, Parity $parity = Parity::NONE, int $stop = 1): PromiseInterface
|
||
|
|
{
|
||
|
|
$deferred = new Deferred();
|
||
|
|
|
||
|
|
Loop::futureTick(static function () use ($device, $baud, $bits, $parity, $stop, $deferred) {
|
||
|
|
$cmd = "stty -F ".escapeshellarg($device)." ".$baud." ".$parity->value." ".escapeshellarg("cs".$bits)." ".($stop==1?"-cstopb":"cstopb")." -echo cbreak min 0 time 0";
|
||
|
|
//echo $cmd."\n";
|
||
|
|
exec($cmd);
|
||
|
|
$fd = fopen($device, "a+");
|
||
|
|
$stream = new DuplexResourceStream($fd);
|
||
|
|
$deferred->resolve($stream);
|
||
|
|
});
|
||
|
|
|
||
|
|
return $deferred->promise();
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
|