PHP code example of tomio2480 / php-serial

1. Go to this page and download the library: Download tomio2480/php-serial library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tomio2480 / php-serial example snippets




hpSerial\Configuration;
use PhpSerial\SerialPort;

$config = new Configuration(
    baudRate: 9600,
    dataBits: 8,
    parity: Configuration::PARITY_NONE,
    stopBits: Configuration::STOP_BITS_1
);

$port = new SerialPort('/dev/ttyUSB0', $config);
$port->open();

// データ送信
$port->write("Hello, Arduino!\n");

// データ受信
$response = $port->readLine(timeout: 2000);
echo "Received: {$response}\n";

$port->close();

$config = new Configuration(baudRate: 115200);

use PhpSerial\Configuration;

// パリティなし
$config = new Configuration(parity: Configuration::PARITY_NONE);

// 奇数パリティ
$config = new Configuration(parity: Configuration::PARITY_ODD);

// 偶数パリティ
$config = new Configuration(parity: Configuration::PARITY_EVEN);

$config = new Configuration(dataBits: 8);

use PhpSerial\Configuration;

$config = new Configuration(stopBits: Configuration::STOP_BITS_1);
// または
$config = new Configuration(stopBits: Configuration::STOP_BITS_2);

$config = new Configuration();
$config
    ->setBaudRate(115200)
    ->setDataBits(8)
    ->setParity(Configuration::PARITY_NONE)
    ->setStopBits(Configuration::STOP_BITS_1);

$port = new SerialPort('COM3');

$port = new SerialPort('/dev/ttyUSB0');
// または
$port = new SerialPort('/dev/ttyACM0');

$port = new SerialPort('/dev/tty.usbserial');
bash
composer 
bash
composer 
ini
extension=ffi
ffi.enable=true