Skip to content

Commit

Permalink
Add first version
Browse files Browse the repository at this point in the history
  • Loading branch information
lepiaf committed Oct 31, 2016
1 parent 39843ab commit cb61553
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 64 deletions.
17 changes: 0 additions & 17 deletions .gitattributes

This file was deleted.

47 changes: 0 additions & 47 deletions .gitignore

This file was deleted.

22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "lepiaf/serialport",
"description": "Serial port access convenience class",
"license": "MIT",
"authors": [
{
"name": "Thierry Thuon",
"email": "[email protected]"
}
],
"minimum-stability": "stable",
"autoload": {
"psr-4": {
"lepiaf\\SerialPort\\": "src/lepiaf/SerialPort"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
}
}
8 changes: 8 additions & 0 deletions src/lepiaf/SerialPort/Exception/DeviceNotAvailable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace lepiaf\SerialPort\Exception;

class DeviceNotAvailable extends \Exception
{

}
52 changes: 52 additions & 0 deletions src/lepiaf/SerialPort/SerialPort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace lepiaf\SerialPort;

use lepiaf\SerialPort\Exception\DeviceNotAvailable;

/**
* @author Thierry Thuon <[email protected]>
* @copyright MIT
*/
class SerialPort
{
private $fd;

public function open($device)
{
// configure tty port
exec('stty -F '.$device.' cs8 9600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts');
$this->fd = fopen($device, 'w+b');

if (false !== $this->fd) {
stream_set_blocking($this->fd, false);

return true;
}

throw new DeviceNotAvailable($device);
}

public function write($data)
{
return fwrite($this->fd, $data);
}

public function read($seperator)
{
$chars = [];
$char = 0;

do {
$char = fread($this->fd, 1);
$chars[] = $char;
} while ($char != $seperator);

return substr(join('', $chars), 0, -1);
}

public function close()
{
return fclose($this->fd);
}
}

0 comments on commit cb61553

Please sign in to comment.