Initial commit
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
<?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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user