Skip to content
PHP Serial Port
PHP
Branch: master
Clone or download
lepiaf Merge pull request #8 from lepiaf/stty-mac
Create a configure for mac
Latest commit 8d7041c Sep 12, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example Fix example namespace Sep 12, 2018
src/lepiaf/SerialPort Create a configure for mac Sep 12, 2018
LICENSE.md update license Oct 31, 2016
README.md Create a configure for mac Sep 12, 2018
composer.json fully first version with example Oct 31, 2016

README.md

SerialPort

Connect to serial port with PHP

Inspired by PHP-Serial, I simplify it and include composer.json to install via composer.

Actually, it works on linux. This library is suitable for working with Arduino.

Install via composer

composer require "lepiaf/serialport"

How to use

Instantiate a new SerialPort object with a parser and configure tty.

<?php

use lepiaf\SerialPort\SerialPort;
use lepiaf\SerialPort\Parser\SeparatorParser;
use lepiaf\SerialPort\Configure\TTYConfigure;

$serialPort = new SerialPort(new SeparatorParser(), new TTYConfigure());

$serialPort->open("/dev/ttyACM0");
while ($data = $serialPort->read()) {
    echo $data."\n";

    if ($data === "OK") {
        $serialPort->write("1\n");
        $serialPort->close();
    }
}

For mac os, you must use TTYMacConfigure. It will use stty -f instead of stty -F.

<?php

use lepiaf\SerialPort\SerialPort;
use lepiaf\SerialPort\Parser\SeparatorParser;
use lepiaf\SerialPort\Configure\TTYMacConfigure;

$serialPort = new SerialPort(new SeparatorParser(), new TTYMacConfigure());

$serialPort->open("/dev/ttyACM0");
while ($data = $serialPort->read()) {
    echo $data."\n";

    if ($data === "OK") {
        $serialPort->write("1\n");
        $serialPort->close();
    }
}
You can’t perform that action at this time.