Download the PHP package machinateur/phpduino without Composer
On this page you can find all versions of the php package machinateur/phpduino. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download machinateur/phpduino
More information about machinateur/phpduino
Files in machinateur/phpduino
Package phpduino
Short Description A user-land stream wrapper implementation for PHP to Arduino communication via USB serial.
License MIT
Informations about the package phpduino
phpduino
A user-land stream wrapper implementation for PHP to Arduino communication via USB serial.
Concept
This package defines an arduino://
protocol handler using
a streamWrapper
implementation.
It also provides some byte processing utility functions (byte_pack()
and byte_unpack()
).
Please note that this library is still in development, consider it unstable until version 1.0
.
Until then, pretty much anything may change without notice, even with patch releases.
Requirements
This package requires at least PHP 8.1 to work. No dependencies.
The Arduino IDE is still required to upload sketches to the connected device.
Usage
You can supply options to the connection, using a stream context:
Click here for parameter explanation for the options below.
Installation
Docs
Relevant links
Here are some links to relevant documentation, articles and forum threads... (click)
- https://docs.arduino.cc/learn/built-in-libraries/software-serial#begin - https://www.arduino.cc/reference/en/language/functions/communication/serial/begin/ - https://unix.stackexchange.com/a/138390 - https://stackoverflow.com/a/8632603 - https://playground.arduino.cc/Interfacing/LinuxTTY/ - https://forum.arduino.cc/t/linux-serial-io/38934/2 - https://web.archive.org/web/20110228183102/https://anealkhimani.com/2010/02/08/web-enabled-pantilt-webcam-with-arduino-and-php-part-1/ - https://web.archive.org/web/20110217155443/http://anealkhimani.com/2010/02/20/web-enabled-pantilt-web-came-with-arduino-and-php-part-2/ - https://web.archive.org/web/20110217070336/http://anealkhimani.com/2010/02/21/web-enabled-pantilt-camera-with-arduino-and-php-part-3/ - https://github.com/Xowap/PHP-Serial/blob/develop/src/PhpSerial.php - https://man7.org/linux/man-pages/man1/stty.1.html - https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/mode - https://unix.stackexchange.com/questions/242778/what-is-the-easiest-way-to-configure-serial-port-on-linux - https://www.php.net/manual/en/class.streamwrapper.php - https://stackoverflow.com/a/9616217 - https://stackoverflow.com/a/59549518 - https://stackoverflow.com/questions/32569611/linux-stty-command-lag-help-needed-on-serial-usb - https://forum.arduino.cc/t/arduino-auto-resets-after-opening-serial-monitor/850915 - https://forum.arduino.cc/t/using-php-to-control-the-arduino-over-usb-serial-connection/134478/9 - https://raspberrypi.stackexchange.com/questions/36490/stty-command-lag-and-queue-issue - https://raspberrypi.stackexchange.com/questions/9695/disable-dtr-on-ttyusb0 - https://stackoverflow.com/a/957416Sleep timer
That is, the time to sleep before opening the device handle in seconds. The option in the stream context is usleep_s
.
For windows, this timeout is applied before the device is opened internally by the stream wrapper, but after it was configured, while on Mac / linux the timeout occurs after the device has been opened internally, but before it was configured using the command.
In my tests, I could go as low as 1.618119
, but below that the success rate started to drop randomly. The cable and
port used could also play a role here.
The timeout is required, as on linux the handle has to be occupied, to avoid a premature reset of the configuration. And likewise, on windows, the configuration can only be applied before the handle is occupied, hence the device is busy after that.
Stream chunk size
As it turns out, this is the key, as for serial (com) ports on windows represented as file stream:
If the stream is read buffered, and it does not represent a plain file, at most one read of up to a number of bytes equal to the chunk size (usually
8192
) is made; depending on the previously buffered data, the size of the returned data may be larger than the chunk size.
This undocumented behaviour of the streamWrapper API may be observed when removing this call and monitoring the value
of $count
in streamWrapperAbstract::stream_read()
when called during \fread($fd, 1)
. The $count
will be 8192
!
Due to the always-blocking nature of streams on windows, you'll have to use \stream_select()
to determine if there is
more data to read or write. See the docs for instructions.
Otherwise, the read operation could block indefinitely, as it seems, given the device does not send any data.
So any script reading responses will have to be very careful, to avoid blocking calls.
Auto-restart on connect
According to several sources it is possible to disable DTR (Data Transmission Ready) line reset, which apparently is a hangup signal. In my tests I couldn't make these options work on neither windows nor mac.
For applications which require accurate and non-resetting continuous functionality, it might be an option to use a hardware solution to secure that.
Example
You can find an easy example that works with the below code in ./example/echo
on windows and Mac.
There is also another more complex example included, involving binary data transmission,
which can be found in ./example/echo/echo-binary.php
.
Yet another example, found in ./example/echo-nonblocking
, demonstrates the usage of \stream_select()
to avoid
blocking read/write calls to the connected device.
Arduino sketch
PHP script
Open a terminal and run the example, like php ./echo.php
.
License
It's MIT.