Update readme

This commit is contained in:
Chris 2025-03-21 13:49:37 +01:00
parent fe25005107
commit 43002de579

View File

@ -17,7 +17,7 @@ that will resolve with a duplex stream for the port.
```php ```php
$serial = new SerialFactory(); $serial = new SerialFactory();
$serial->open("/dev/ttyUSB0", 115200) $serial->open("/dev/ttyUSB0", 115200)
->then(...); ->then(function (DuplexStreamInterface $stream) { ... });
``` ```
You may find that you need line buffering, in which case you should wrap the stream in a You may find that you need line buffering, in which case you should wrap the stream in a
@ -28,16 +28,17 @@ event whenever a matching prompt is detected, and with `$bufferOutput` you will
an `output` event with all the `line`s emitted since the last `prompt`. an `output` event with all the `line`s emitted since the last `prompt`.
```php ```php
$serial->open("/dev/ttyUSB0", 115200)
->then(function (DuplexStreamInterface $stream) {
// Wrap our stream and use '#> ' as the prompt pattern. // Wrap our stream and use '#> ' as the prompt pattern.
$shell = new LineBufferedDuplexStream($stream, promptPattern: '/\#\> /', bufferOutput:true); $shell = new LineBufferedDuplexStream($stream, promptPattern: '/\#\> /', bufferOutput:true);
// Prepare the commands to execute, in order
// Prepare the commands to execute, in order. These are the commands you would use
// on an OpenWRT router to configure the WiFi password for the "main" WiFi interface.
$commands = [ $commands = [
'set first="value"', 'uci set wireless.main.key="pennyisafreeloader"',
'set second="something"', 'uci commit wireless',
'apply', 'wifi restart',
]; ];
// Execute the next command once we get a prompt // Execute the next command once we get a prompt
$shell->on("prompt", function () use ($shell, &$commands) { $shell->on("prompt", function () use ($shell, &$commands) {
$command = array_shift($commands); $command = array_shift($commands);
@ -46,18 +47,15 @@ $serial->open("/dev/ttyUSB0", 115200)
$shell->close(); $shell->close();
return; return;
} }
echo "# \e[1m".$command."\e[0m\n"; echo $command."\n";
$shell->write($command."\n"); $shell->write($command."\n");
}); });
// Receives each complete line
$shell->on("line", function ($line) {
// ...
});
// Receives all the output lines from the last command. // Receives all the output lines from the last command.
$shell->on("output", function(array $lines) { $shell->on("output", function(array $lines) {
// ... echo " ".join("\n ",$lines)."\n";
});
// write "config" to our fictive device to enable config mode
$shell->write("config\n");
}); });
// write "echo" to the shell on our fictive device to get a fresh prompt
$shell->write("echo\n");
``` ```