Skip to content

Instantly share code, notes, and snippets.

@kahagon
Last active December 20, 2015 05:09
Show Gist options
  • Select an option

  • Save kahagon/6076496 to your computer and use it in GitHub Desktop.

Select an option

Save kahagon/6076496 to your computer and use it in GitHub Desktop.
Basic usage of Gorilla.
<?php
// $device = 'COM4'; // on Windows
$device = '/dev/ttyUSB0'; // on Linux
/*
* create new instance
*/
$port = new SerialPort();
try {
/*
* open the port
*/
$port->open($device);
/*
* configure baud rate
*
* you can specify baud rate as integer,
* or other class constants like SerialPort::BAUD_RATE_*
*/
$port->setBaudRate(SerialPort::BAUD_RATE_9600);
/*
* configure flow control
*
* any other options are below.
* SerialPort::FLOW_CONTROL_SOFT is software flow control.
* SerialPort::FLOW_CONTROL_HARD is hardware flow control.
*/
$port->setFlowControl(SerialPort::FLOW_CONTROL_NONE);
/*
* configure canonical mode
*
* canonical mode is for text-based communication.
* non-canonical mode is binary-safe.
* more detail information about VMIN and VTIME,
* see http://www.unixwiz.net/techtips/termios-vmin-vtime.html
*/
$port->setCanonical(false)
->setVTime(1)->setVMin(0);
/*
* read data from port.
* you can get size of actual read data with strlen($data) .
*/
$data = $port->read(256);
/*
* send data.
*/
$port->write($data);
} catch (Exception $e) {
print $e->getMessage() . PHP_EOL;
$status = 2;
}
if ($port->isOpen()) $port->close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment