marabesi / arduino-php-wrapper
该包的最新版本(v1.0.0)没有可用的许可证信息。
v1.0.0
2017-08-20 02:01 UTC
Requires (Dev)
- mikey179/vfsstream: ~1
- phpunit/phpunit: ~4.8
This package is auto-updated.
Last update: 2024-09-07 02:38:06 UTC
README
如果您想知道如何通过PHP控制Arduino串行端口,这里就是解决方案。Arduino://包装器是一个简单直接的方法来读写Arduino数据。
安装
composer require marabesi/arduino-php-wrapper
用法
要从Arduino串行端口读取数据,只需使用PHP中的常规I/O函数,如fread或file_get_contents
\Arduino\Wrapper::register(); //reads data from Arduino $resource = fopen('arduino://ttyUSB0', 'r+'); print fread($resource, 1024);
或者,如果您愿意,您也可以使用file_get_contents并得到相同的结果
print file_get_contents('arduino://ttyUSB0');
将数据写入Arduino串行端口就像它可能的那样简单
\Arduino\Wrapper::register(); //writes data to Arduino $resource = fopen('arduino://ttyUSB0', 'r+'); print fwrite('hello Arduino');
\Arduino\Wrapper::register(); print file_put_contents('arduino://hello Arduino');
面向对象编程(OOP)
您可以在项目中以OOP风格使用它
发送数据
$writer = new Arduino\Writer(new Arduino\Wrapper()); $bytes = $writer->out('ttyUSB0', 'from oop');
读取数据
$arduino = new \Arduino\Wrapper(); $writer = new \Arduino\Reader($arduino); while (true) { print $writer->from('/dev/cu.usbmodem1411'); }
改进
如您所见,它非常简单,随着传感器的识别,我们可以对其进行更多改进。
- 防止Arduino在每次通过PHP请求时重新加载