Last active
December 20, 2015 05:09
-
-
Save kahagon/6076496 to your computer and use it in GitHub Desktop.
Basic usage of Gorilla.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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