-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
82 additions
and
64 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
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" | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace lepiaf\SerialPort\Exception; | ||
|
||
class DeviceNotAvailable extends \Exception | ||
{ | ||
|
||
} |
This file contains 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
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); | ||
} | ||
} |