fawno/php-serial

Serial port access convenience class

2.0.0 2022-03-06 22:26 UTC

This package is auto-updated.

Last update: 2024-11-07 04:26:17 UTC


README

GitHub license GitHub release Packagist PHP

Serial port access convenience class

Requirements

  • PHP Pecl dio extension (>= 0.2.1) for SerialDio.

Installation

You can install this plugin into your application using composer:

  composer require fawno/php-serial

Usage

  require 'vendor/autoload.php';

  use Fawno\PhpSerial\SerialDio;
  use Fawno\PhpSerial\SerialConfig;
  use Fawno\PhpSerial\SerialBaudRates;
  use Fawno\PhpSerial\SerialStopBits;
  use Fawno\PhpSerial\SerialParity;
  use Fawno\PhpSerial\SerialDataBits;

  // Create default serial config
  $config = new SerialConfig;

  // Set Data Rate
  $config->setBaudRate(SerialBaudRates::B9600);

  // Set Data Bits
  $config->setDataBits(SerialDataBits::CS8);

  // Set Stop Bits
  $config->setStopBits(SerialStopBits::ONE);

  // Set Parity
  $config->setParity(SerialParity::NONE);

  // Set Flow Control
  $config->setFlowControl(true);

  // Create SerialDio object with COM3 as device
  $serial = new SerialDio('COM3', $config);

  // Open device
  $serial->open('r+b');

  // Set Blocking
  $serial->setBlocking(0);

  // Set Timeout
  $serial->setTimeout(0, 0);

  // Send data
  $serial->send($data);

  // Read data
  $data = $serial->read();