PHP code example of sanchescom / php-serial

1. Go to this page and download the library: Download sanchescom/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/ */

    

sanchescom / php-serial example snippets




use Sanchescom\Serial\Serial;

try {
    $serial = new Serial();
    
    // First we must specify the device. This works on both linux and windows (if
    // your linux serial device is /dev/ttyS0 for COM1, etc)
    $device = $serial->setDevice('COM5');

    // We can change the baud rate, parity, length, stop bits, flow control
    $device->setBaudRate(2400);
    $device->setParity("none");
    $device->setCharacterLength(8);
    $device->setStopBits(1);
    $device->setFlowControl("none");

    // Then we need to open it
    $device->open();

    // To write into
    $device->send('Hello!');

    // Or to read from
    $read = $device->read();

    // If you want to change the configuration, the device must be closed
    $device->close();

    // We can change the baud rate
    $device->setBaudRate(2400);
} catch (Exception $e) {

}
 bash
$ composer