Uploaded From CV. Swandhana Server

This commit is contained in:
Duidev Software House
2025-01-27 08:16:55 +07:00
commit 6b3be42361
15186 changed files with 2328862 additions and 0 deletions
+24
View File
@@ -0,0 +1,24 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[*.bat]
end_of_line = crlf
[*.yml]
indent_style = space
indent_size = 2
[*.twig]
insert_final_newline = false
[Makefile]
indent_style = tab
+51
View File
@@ -0,0 +1,51 @@
# Define the line ending behavior of the different file extensions
# Set default behavior, in case users don't have core.autocrlf set.
* text=auto
* text eol=lf
# Explicitly declare text files we want to always be normalized and converted
# to native line endings on checkout.
*.php text
*.default text
*.ctp text
*.sql text
*.md text
*.po text
*.js text
*.css text
*.ini text
*.properties text
*.txt text
*.xml text
*.svg text
*.yml text
.htaccess text
# Declare files that will always have CRLF line endings on checkout.
*.bat eol=crlf
# Declare files that will always have LF line endings on checkout.
*.pem eol=lf
# Denote all files that are truly binary and should not be modified.
*.dbf binary
*.prj binary
*.shp binary
*.shx binary
*.sld binary
*.png binary
*.jpg binary
*.gif binary
*.ico binary
*.mo binary
*.pdf binary
*.phar binary
*.woff binary
*.woff2 binary
*.ttf binary
*.otf binary
*.eot binary
*.gz binary
*.bz2 binary
*.7z binary
*.zip binary
+46
View File
@@ -0,0 +1,46 @@
# CakePHP specific files #
##########################
/config/app.php
/config/.env
/logs/*
/tmp/*
/vendor/*
/webroot/files/*
# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
Icon?
ehthumbs.db
Thumbs.db
*.zip
# Tool specific files #
#######################
/.vscode
/composer.lock
/.phpdoc
# vim
*~
*.swp
*.swo
# sublime text & textmate
*.sublime-*
*.stTheme.cache
*.tmlanguage.cache
*.tmPreferences.cache
# Eclipse
.settings/*
# JetBrains, aka PHPStorm, IntelliJ IDEA
.idea/*
# NetBeans
nbproject/*
# Visual Studio Code
.vscode
# Sass preprocessor
.sass-cache/
data
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020 Fernando Herrero
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+69
View File
@@ -0,0 +1,69 @@
# PHP-Serial
[![GitHub license](https://img.shields.io/github/license/fawno/PHP-Serial)](https://github.com/fawno/PHP-serial/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/fawno/PHP-Serial)](https://github.com/fawno/PHP-serial/releases)
[![Packagist](https://img.shields.io/packagist/v/fawno/php-serial)](https://packagist.org/packages/fawno/php-serial)
[![PHP](https://img.shields.io/packagist/php-v/fawno/php-serial)](https://php.net)
Serial port access convenience class
## Requirements
- PHP Pecl dio extension (>= 0.2.1) for SerialDio.
## Installation
You can install this plugin into your application using
[composer](https://getcomposer.org):
```
composer require fawno/php-serial
```
## Usage
```php
require 'vendor/autoload.php';
use Fawno\PhpSerial\SerialDio;
use Fawno\PhpSerial\SerialConfig;
use Fawno\PhpSerial\SerialBaudRates;
use Fawno\PhpSerial\SerialStopBits;
use Fawno\PhpSerial\SerialParity;
use Fawno\PhpSerial\SerialDataBits;
// Create default serial config
$config = new SerialConfig;
// Set Data Rate
$config->setBaudRate(SerialBaudRates::B9600);
// Set Data Bits
$config->setDataBits(SerialDataBits::CS8);
// Set Stop Bits
$config->setStopBits(SerialStopBits::ONE);
// Set Parity
$config->setParity(SerialParity::NONE);
// Set Flow Control
$config->setFlowControl(true);
// Create SerialDio object with COM3 as device
$serial = new SerialDio('COM3', $config);
// Open device
$serial->open('r+b');
// Set Blocking
$serial->setBlocking(0);
// Set Timeout
$serial->setTimeout(0, 0);
// Send data
$serial->send($data);
// Read data
$data = $serial->read();
```
+28
View File
@@ -0,0 +1,28 @@
<?php
/**
* PHP-Serial: Serial port access convenience class (https://github.com/fawno/PHP-serial)
* Copyright (c) Fernando Herrero (https://github.com/alpha)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*
* @copyright Fernando Herrero (https://github.com/alpha)
* @link https://github.com/fawno/PHP-serial PHP-Serial
* @since 0.0.1
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
require __DIR__ . '/src/Config/BaudRates.php';
require __DIR__ . '/src/Config/DataBits.php';
require __DIR__ . '/src/Config/Parity.php';
require __DIR__ . '/src/Config/StopBits.php';
require __DIR__ . '/src/SerialConfig.php';
require __DIR__ . '/src/Serial.php';
require __DIR__ . '/src/SerialException.php';
require __DIR__ . '/src/SerialDio.php';
//require __DIR__ . '/src/File/Windows.php';
//require __DIR__ . '/src/File/Linux.php';
//require __DIR__ . '/src/File/Darwin.php';
require __DIR__ . '/src/SerialFile.php';
+27
View File
@@ -0,0 +1,27 @@
{
"name": "fawno/php-serial",
"version": "2.0.0",
"description": "Serial port access convenience class",
"authors": [
{
"name": "Fawno",
"homepage": "https://github.com/fawno/PHP-Serial/graphs/contributors"
}
],
"support": {
"issues": "https://github.com/fawno/PHP-Serial/issues",
"source": "https://github.com/fawno/PHP-Serial"
},
"require": {
"php": ">=7.4.0"
},
"suggest": {
"ext-dio": "Needed for SerialDio (>=0.2.1)"
},
"license": "MIT",
"autoload": {
"psr-4": {
"Fawno\\PhpSerial\\": "src/"
}
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
require __DIR__ . '/autoload.php';
use Fawno\PhpSerial\Config\BaudRates;
use Fawno\PhpSerial\Config\StopBits;
use Fawno\PhpSerial\Config\Parity;
use Fawno\PhpSerial\Config\DataBits;
use Fawno\PhpSerial\SerialConfig;
use Fawno\PhpSerial\SerialDio;
$config = new SerialConfig;
$config->setBaudRate(BaudRates::B9600);
$config->setDataBits(DataBits::CS8);
$config->setStopBits(StopBits::ONE);
$config->setParity(Parity::NONE);
$config->setFlowControl(true);
$serial = new SerialDio('COM4', $config);
$serial->open('r+b');
$serial->setBlocking(false);
$serial->setTimeout(0, 100000);
$serial->send('at+ver' . "\r\n");
$data = $serial->read();
echo $data;
+235
View File
@@ -0,0 +1,235 @@
<?php
/**
* @link https://www.php.net/manual/en/ref.dio.php
* Direct IO Functions
* Table of Contents
* dio_close — Closes the file descriptor given by fd
* dio_fcntl — Performs a c library fcntl on fd
* dio_open — Opens a file (creating it if necessary) at a lower level than the C library input/ouput stream functions allow
* dio_read — Reads bytes from a file descriptor
* dio_seek — Seeks to pos on fd from whence
* dio_stat — Gets stat information about the file descriptor fd
* dio_tcsetattr — Sets terminal attributes and baud rate for a serial port
* dio_truncate — Truncates file descriptor fd to offset bytes
* dio_write — Writes data to fd with optional truncation at length
* dio_raw - Opens a raw direct IO stream.
* dio_serial - Opens a serial direct IO stream.
*/
/**
* Closes the file descriptor given by fd.
*
* dio_close ( resource $fd ) : void
*
* @link https://www.php.net/manual/en/function.dio-close.php
* @param resource $fd
* The file descriptor returned by dio_open()
* @return void
*/
function dio_close($fd) : void {}
/**
* The dio_fcntl() function performs the operation specified by cmd on the file descriptor fd.
*
* dio_fcntl ( resource $fd , int $cmd [, mixed $args ] ) : mixed
*
* Some commands require additional arguments args to be supplied.
* @link https://www.php.net/manual/en/function.dio-fcntl.php
* @param resource $fd
* The file descriptor returned by dio_open().
* @param int $cmd
* The file descriptor returned by dio_open().
* @param mixed ...$args
* args is an associative array, when cmd is **F_SETLK** or **F_SETLLW**, with the following keys:
* - *start* - offset where lock begins
* - *length* - size of locked area. zero means to end of file
* - *whence* - Where l_start is relative to: can be **SEEK_SET**, **SEEK_END** and **SEEK_CUR**
* - *type* - type of lock: can be **F_RDLCK** (read lock), **F_WRLCK** (write lock) or **F_UNLCK** (unlock)
* @return mixed Returns the result of the C call.
*/
function dio_fcntl($fd, int $cmd, ...$args) {}
/**
* Opens a file (creating it if necessary) at a lower level than theC library input/ouput stream functions allow
*
* dio_open ( string $filename , int $flags [, int $mode = 0 ] ) : resource
*
* @link https://www.php.net/manual/en/function.dio-open.php
* @param string $filename The pathname of the file to open.
* @param int $flags
* The flags parameter is a bitwise-ORed value comprising flags from the following list.
* This value must include one of **O_RDONLY**, **O_WRONLY**, or **O_RDWR**.
* Additionally, it may include any combination of the other flags from this list.
* - **O_RDONLY** - opens the file for read access.
* - **O_WRONLY** - opens the file for write access.
* - **O_RDWR** - opens the file for both reading and writing.
* - **O_CREAT** - creates the file, if it doesn't already exist.
* - **O_EXCL** - if both O_CREAT and O_EXCL are set and the file already exists, dio_open() will fail.
* - **O_TRUNC** - if the file exists and is opened for write access, the file will be truncated to zero length.
* - **O_APPEND** - write operations write data at the end of the file.
* - **O_NONBLOCK** - sets non blocking mode.
* - **O_NOCTTY** - prevent the OS from assigning the opened file as the process's controllingterminal when opening a TTY device file.
* @param int $mode
* If flags contains **O_CREAT**, mode will set the permissions of the file (creation permissions).
* mode is required for correct operation when **O_CREAT** is specified in flags and is ignored otherwise.
* The actual permissions assigned to the created file will be affected by the process's umask setting as per usual.
* @return resource|false A file descriptor or FALSE on error.
*/
function dio_open(string $filename, int $flags, int $mode = 0) {}
/**
* Reads bytes from a file descriptor.
*
* dio_read ( resource $fd [, int $len = 1024 ] ) : string
*
* @param resource $fd
* The file descriptor returned by dio_open().
* @param int $len
* The number of bytes to read. If not specified, dio_read() reads 1k sized block.
* @return string The bytes read from fd.
* @link https://www.php.net/manual/en/function.dio-read.php
*/
function dio_read($fd, int $len = 1024) { }
/**
* Seeks to pos on fd from whence
*
* dio_seek ( resource $fd , int $pos [, int $whence = SEEK_SET ] ): int
*
* @param resource $fd
* The file descriptor returned by dio_open().
* @param int $pos
* The new position.
* @param int $whence
* Specifies how the position pos should be interpreted:
* - **SEEK_SET** - (Default) Specifies that pos is specified from the beginning of the file.
* - **SEEK_CUR** - Specifies that pos is a count of characters from the current file position.
* This count may be positive or negative.
* - **SEEK_END** - Specifies that pos is a count of characters from the end of the file.
* A negative count specifies a position within the current extent of the file;
* a positive count specifies a position past the current end.
* If you set the position past the current end, and actually write data,
* you will extend the file with zeros up to that position.
* @return int
* @link https://www.php.net/manual/en/function.dio-seek.php
*/
function dio_seek($fd, int $pos, int $whence = SEEK_SET) {}
/**
* Gets stat information about the file descriptor fd
*
* dio_stat ( resource $fd ) : array
*
* @param resource $fd
* The file descriptor returned by dio_open().
* @return array|null
* Returns an associative array with the following keys:
* - *device* - device
* - *inode* - inode
* - *mode* - mode
* - *nlink* - number of hard links
* - *uid* - user id
* - *gid* - group id
* - *device_type* - device type (if inode device)
* - *size* - total size in bytes
* - *blocksize* - blocksize
* - *blocks* - number of blocks allocated
* - *atime* - time of last access
* - *mtime* - time of last modification
* - *ctime* - time of last change
*
* On error dio_stat() returns NULL.
* @link https://www.php.net/manual/en/function.dio-stat.php
*/
function dio_stat($fd) {}
/**
* Sets terminal attributes and baud rate for a serial port
*
* dio_tcsetattr ( resource $fd , array $options ) : bool
*
* @param resource $fd
* The file descriptor returned by dio_open().
* @param array $options
* The currently available options are:
* - *baud* - baud rate of the port - can be 38400,19200,9600,4800,2400,1800, 1200,600,300,200,150,134,110,75 or 50, default value is 9600.
* - *bits* - data bits - can be 8,7,6 or 5. Default value is 8.
* - *stop* - stop bits - can be 1 or 2. Default value is 1.
* - *parity* - can be 0,1 or 2. Default value is 0.
* @return void
* @link https://www.php.net/manual/en/function.dio-tcsetattr.php
*/
function dio_tcsetattr($fd, array $options) {}
/**
* Truncates a file to at most offset bytes in size.
*
* dio_truncate ( resource $fd , int $offset ) : bool
*
* If the file previously was larger than this size, the extra data is lost.
* If the file previously was shorter, it is unspecified whether the file is left unchanged or is extended.
* In the latter case the extended part reads as zero bytes.
* @param resource $fd The file descriptor returned by dio_open().
* @param int $offset The offset in bytes.
* @return bool Returns TRUE on success or FALSE on failure.
* @link https://www.php.net/manual/en/function.dio-truncate.php
*/
function dio_truncate($fd, int $offset) {}
/**
* Writes data to fd with optional truncation at length
*
* dio_write ( resource $fd , string $data [, int $len = 0 ] ) : int
*
* @link https://www.php.net/manual/en/function.dio-write.php
* @param resource $fd
* The file descriptor returned by dio_open().
* @param string $data
* The written data.
* @param int $len
* The length of data to write in bytes. If not specified, the function writes all the data to the specified file.
* @return int Returns the number of bytes written to fd.
*/
function dio_write($fd, string $data, int $len = 0) {}
/**
* Opens a raw direct IO stream.
*
* dio_raw ( string filename , string mode [, array options] ) : ?resource
*
* @param string $filename
* The pathname of the file to open.
* @param string $mode
* The mode parameter specifies the type of access you require to the stream (as *fopen()*).
* @param array|null $options
* The currently available options are:
* - *data_rate* - baud rate of the port - can be 75, 110, 134, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, 9600, 14400, 19200, 38400, 57600, 115200, 56000, 128000 or 256000 default value is 9600.
* - *data_bits* - can be 8, 7, 6 or 5. Default value is 8.
* - *stop_bits* - can be 1 or 2. Default value is 1.
* - *parity* - can be 0, 1 or 2. Default value is 0.
* - *flow_control* - can be 0 or 1. Default value is 1.
* - *is_canonical* - can be 0 or 1. Default value is 1.
* @return resource|null A stream resource or null on error.
*/
function dio_raw(string $filename, string $mode, ?array $options) {}
/**
* Opens a serial direct IO stream.
*
* dio_serial ( string $filename , string $mode [, array $options = null] ) : ?resource
*
* @param string $filename
* The pathname of the file to open.
* @param string $mode
* The mode parameter specifies the type of access you require to the stream (as *fopen()*).
* @param array|null $options
* The currently available options are:
* - *data_rate* - baud rate of the port - can be 75, 110, 134, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, 9600, 14400, 19200, 38400, 57600, 115200, 56000, 128000 or 256000 default value is 9600.
* - *data_bits* - can be 8, 7, 6 or 5. Default value is 8.
* - *stop_bits* - can be 1 or 2. Default value is 1.
* - *parity* - can be 0, 1 or 2. Default value is 0.
* - *flow_control* - can be 0 or 1. Default value is 1.
* - *is_canonical* - can be 0 or 1. Default value is 1.
* @return resource|null A stream resource or null on error.
*/
function dio_serial(string $filename, string $mode, ?array $options) {}
+141
View File
@@ -0,0 +1,141 @@
<?php
/**
* @link https://www.php.net/manual/en/dio.constants.php
* Direct IO Constants
* Table of Contents
* O_RDONLY - opens the file for read access.
* O_WRONLY - opens the file for write access.
* O_RDWR - opens the file for both reading and writing.
* O_CREAT - creates the file, if it doesn't already exist.
* O_EXCL - if both O_CREAT and O_EXCL are set and the file already exists, dio_open() will fail.
* O_TRUNC - if the file exists and is opened for write access, the file will be truncated to zero length.
* O_APPEND - write operations write data at the end of the file.
* O_NONBLOCK - sets non blocking mode.
* O_NOCTTY - prevent the OS from assigning the opened file as the process's controlling terminal when opening a TTY device file.
* F_SETLK - Lock is set or cleared. If the lockis held by someone else dio_fcntl() returns-1.
* F_SETLKW - like F_SETLK,but in case the lock is held by someone else, dio_fcntl() waits until the lock is released.
* F_GETLK - dio_fcntl() returns an associative array (as described below) if someone elseprevents lock. If there is no obstruction key "type" will setto F_UNLCK.
* F_DUPFD - finds the lowest numbered availablefile descriptor greater than or equal to argsand returns them.
* F_SETFL - Sets the file descriptors flags tothe value specified by args, which can be O_APPEND, O_NONBLOCK or O_ASYNC. To use O_ASYNC you will need to use the PCNTL extension.
* O_NDELAY -
* O_SYNC -
* O_ASYNC -
* S_IRWXU -
* S_IRUSR -
* S_IWUSR -
* S_IXUSR -
* S_IRWXG -
* S_IRGRP -
* S_IWGRP -
* S_IXGRP -
* S_IRWXO -
* S_IROTH -
* S_IWOTH -
* S_IXOTH -
* F_GETFD -
* F_GETFL -
* F_SETOWN -
* F_GETOWN -
* F_UNLCK -
* F_RDLCK -
* F_WRLCK -
*/
/**
*/
/**
* O_RDONLY - opens the file for read access.
*/
define('O_RDONLY', 0);
/**
* O_WRONLY - opens the file for write access.
*/
define('O_WRONLY', 1);
/**
* O_RDWR - opens the file for both reading and writing.
*/
define('O_RDWR', 2);
/**
* O_CREAT - creates the file, if it doesn't already exist.
*/
define('O_CREAT', 64);
/**
* O_EXCL - if both O_CREAT and O_EXCL are set and the file already exists, dio_open() will fail.
*/
define('O_EXCL', 128);
/**
* O_TRUNC - if the file exists and is opened for write access, the file will be truncated to zero length.
*/
define('O_TRUNC', 512);
/**
* O_APPEND - write operations write data at the end of the file.
*/
define('O_APPEND', 1024);
/**
* O_NONBLOCK - sets non blocking mode.
*/
define('O_NONBLOCK', 2048);
define('O_NDELAY', 2048);
define('O_SYNC', 1052672);
define('O_ASYNC', 8192);
/**
* O_NOCTTY - prevent the OS from assigning the opened file as the process's controlling terminal when opening a TTY device file.
*/
define('O_NOCTTY', 256);
define('S_IRWXU', 448);
define('S_IRUSR', 256);
define('S_IWUSR', 128);
define('S_IXUSR', 64);
define('S_IRWXG', 56);
define('S_IRGRP', 32);
define('S_IWGRP', 16);
define('S_IXGRP', 8);
define('S_IRWXO', 7);
define('S_IROTH', 4);
define('S_IWOTH', 2);
define('S_IXOTH', 1);
/**
* F_DUPFD - finds the lowest numbered availablefile descriptor greater than or equal to argsand returns them.
*/
define('F_DUPFD', 0);
define('F_GETFD', 1);
define('F_GETFL', 3);
/**
* F_SETFL - Sets the file descriptors flags tothe value specified by args, which can be O_APPEND, O_NONBLOCK or O_ASYNC. To use O_ASYNC you will need to use the PCNTL extension.
*/
define('F_SETFL', 4);
/**
* F_GETLK - dio_fcntl() returns an associative array (as described below) if someone elseprevents lock. If there is no obstruction key "type" will setto F_UNLCK.
*/
define('F_GETLK', 5);
/**
* F_SETLK - Lock is set or cleared. If the lockis held by someone else dio_fcntl() returns-1.
*/
define('F_SETLK', 6);
/**
* F_SETLKW - like F_SETLK,but in case the lock is held by someone else, dio_fcntl() waits until the lock is released.
*/
define('F_SETLKW', 7);
define('F_SETOWN', 8);
define('F_GETOWN', 9);
define('F_UNLCK', 2);
define('F_RDLCK', 0);
define('F_WRLCK', 1);
+41
View File
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Fawno\PhpSerial\Config;
use ReflectionClass;
class BaudRates {
//public const B50 = 50; //posix
public const B75 = 75;
public const B110 = 110;
public const B134 = 134;
public const B150 = 150;
//public const B200 = 200; //posix
public const B300 = 300;
public const B600 = 600;
public const B1200 = 1200;
public const B1800 = 1800;
public const B2400 = 2400;
public const B4800 = 4800;
//public const B7200 = 7200; //win
public const B9600 = 9600;
//public const B14400 = 14400; //win
public const B19200 = 19200;
public const B38400 = 38400;
//public const B56000 = 56000; //win
public const B57600 = 57600;
public const B115200 = 115200;
//public const B128000 = 128000; //win
//public const B256000 = 256000; //win
//public const B230400 = 230400; //posix
//public const B460800 = 460800; //posix
static function getConstants () : array {
return (new ReflectionClass(__CLASS__))->getConstants();
}
static function checkValue (int $const) : bool {
return in_array($const, self::getConstants());
}
}
+22
View File
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Fawno\PhpSerial\Config;
use ReflectionClass;
class DataBits {
//public const CS4 = 4; //win
public const CS5 = 5;
public const CS6 = 6;
public const CS7 = 7;
public const CS8 = 8;
static function getConstants () : array {
return (new ReflectionClass(__CLASS__))->getConstants();
}
static function checkValue (int $const) : bool {
return in_array($const, self::getConstants());
}
}
+20
View File
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Fawno\PhpSerial\Config;
use ReflectionClass;
class Parity {
public const NONE = 0;
public const ODD = 1;
public const EVEN = 2;
static function getConstants () : array {
return (new ReflectionClass(__CLASS__))->getConstants();
}
static function checkValue (int $const) : bool {
return in_array($const, self::getConstants());
}
}
+20
View File
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Fawno\PhpSerial\Config;
use ReflectionClass;
class StopBits {
//public const NONE = 0;
public const ONE = 1;
public const TWO = 2;
static function getConstants () : array {
return (new ReflectionClass(__CLASS__))->getConstants();
}
static function checkValue (int $const) : bool {
return in_array($const, self::getConstants());
}
}
+89
View File
@@ -0,0 +1,89 @@
<?php
/**
* PHP-Serial: Serial port access convenience class (https://github.com/fawno/PHP-serial)
* Copyright (c) Fernando Herrero (https://github.com/alpha)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*
* @copyright Fernando Herrero (https://github.com/alpha)
* @link https://github.com/fawno/PHP-serial PHP-Serial
* @since 0.1.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Fawno\PhpSerial\File;
use Fawno\PhpSerial\Serial;
use Fawno\PhpSerial\SerialException;
/**
* Darwing class provides serial connection using file stream in OSX OS
*
* @package Fawno\PhpSerial
* @uses Fawno\PhpSerial\Serial Provides general serial methods
* @uses Fawno\PhpSerial\SerialException Provides custom exception
* @used-by Fawno\PhpSerial\SerialFile
*/
class Darwing extends Serial {
/**
* Sets and prepare the port for conection.
*
* @return Darwing
* @throws SerialException
*/
protected function setPortOptions () : Darwing {
$params = ['device' => $this->_device] + $this->_options;
$param_formats = [
'stop_bits' => [1 => '-cstopb', 2 => 'cstopb'],
'parity' => [0 => '-parenb', 1 => 'parenb parodd', 2 => 'parenb -parodd'],
'flow_control' => [0 => 'clocal -crtscts -ixon -ixoff', 1 => '-clocal -crtscts ixon ixoff'],
];
foreach ($param_formats as $param => $values) {
$params[$param] = $values[$params[$param]];
}
$command = 'stty -f %s %s cs%s %s %s %s';
$command = sprintf($command, ...array_values($params));
$message = exec($command, $output, $result_code);
if ($result_code) {
throw new SerialException(utf8_encode($message), $result_code);
}
return $this;
}
/**
* Binds a named resource, specified by setDevice, to a stream.
*
* @param string $mode
* The mode parameter specifies the type of access you require to the stream (as `fopen()`).
*
* @return Darwing
* @throws SerialException
*/
public function open (string $mode = 'r+b') : Darwing {
$this->setPortOptions();
parent::open($mode);
error_clear_last();
$this->_serial = @fopen($this->_device, $mode);
if (!is_resource($this->_serial)) {
$error = error_get_last();
if (is_array($error)) {
$error = new SerialException($error['message'], 0, $error['type'], $error['file'], $error['line']);
}
throw new SerialException(sprintf('Unable to open device %s', $this->_device), 0, E_ERROR, null, null, $error);
}
return $this;
}
}
+89
View File
@@ -0,0 +1,89 @@
<?php
/**
* PHP-Serial: Serial port access convenience class (https://github.com/fawno/PHP-serial)
* Copyright (c) Fernando Herrero (https://github.com/alpha)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*
* @copyright Fernando Herrero (https://github.com/alpha)
* @link https://github.com/fawno/PHP-serial PHP-Serial
* @since 0.1.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Fawno\PhpSerial\File;
use Fawno\PhpSerial\Serial;
use Fawno\PhpSerial\SerialException;
/**
* Linux class provides serial connection using file stream in Linux OS
*
* @package Fawno\PhpSerial
* @uses Fawno\PhpSerial\Serial Provides general serial methods
* @uses Fawno\PhpSerial\SerialException Provides custom exception
* @used-by Fawno\PhpSerial\SerialFile
*/
class Linux extends Serial {
/**
* Sets and prepare the port for conection.
*
* @return Linux
* @throws SerialException
*/
protected function setPortOptions () : Linux {
$params = ['device' => $this->_device] + $this->_options;
$param_formats = [
'stop_bits' => [1 => '-cstopb', 2 => 'cstopb'],
'parity' => [0 => '-parenb', 1 => 'parenb parodd', 2 => 'parenb -parodd'],
'flow_control' => [0 => 'clocal -crtscts -ixon -ixoff', 1 => '-clocal -crtscts ixon ixoff'],
];
foreach ($param_formats as $param => $values) {
$params[$param] = $values[$params[$param]];
}
$command = 'stty -F %s %s cs%s %s %s %s';
$command = sprintf($command, ...array_values($params));
$message = exec($command, $output, $result_code);
if ($result_code) {
throw new SerialException(utf8_encode($message), $result_code);
}
return $this;
}
/**
* Binds a named resource, specified by setDevice, to a stream.
*
* @param string $mode
* The mode parameter specifies the type of access you require to the stream (as `fopen()`).
*
* @return Linux
* @throws SerialException
*/
public function open (string $mode = 'r+b') : Linux {
$this->setPortOptions();
parent::open($mode);
error_clear_last();
$this->_serial = @fopen($this->_device, $mode);
if (!is_resource($this->_serial)) {
$error = error_get_last();
if (is_array($error)) {
$error = new SerialException($error['message'], 0, $error['type'], $error['file'], $error['line']);
}
throw new SerialException(sprintf('Unable to open device %s', $this->_device), 0, E_ERROR, null, null, $error);
}
return $this;
}
}
+88
View File
@@ -0,0 +1,88 @@
<?php
/**
* PHP-Serial: Serial port access convenience class (https://github.com/fawno/PHP-serial)
* Copyright (c) Fernando Herrero (https://github.com/alpha)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*
* @copyright Fernando Herrero (https://github.com/alpha)
* @link https://github.com/fawno/PHP-serial PHP-Serial
* @since 0.1.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Fawno\PhpSerial\File;
use Fawno\PhpSerial\Serial;
use Fawno\PhpSerial\SerialException;
/**
* Windows class provides serial connection using file stream in Windows OS
*
* @package Fawno\PhpSerial
* @uses Fawno\PhpSerial\Serial Provides general serial methods
* @uses Fawno\PhpSerial\SerialException Provides custom exception
* @used-by Fawno\PhpSerial\SerialFile
*/
class Windows extends Serial {
/**
* Sets and prepare the port for conection.
*
* @return Windows
* @throws SerialException
*/
protected function setPortOptions () : Windows {
$params = ['device' => $this->_device] + $this->_options;
$param_formats = [
'parity' => [0 => 'n', 1 => 'o', 2 => 'e'],
'flow_control' => [0 => 'off', 1 => 'on'],
];
foreach ($param_formats as $param => $values) {
$params[$param] = $values[$params[$param]];
}
$command = 'mode %s baud=%s data=%s stop=%s parity=%s xon=%s';
$command = sprintf($command, ...array_values($params));
$message = exec($command, $output, $result_code);
if ($result_code) {
throw new SerialException(utf8_encode($message), $result_code);
}
return $this;
}
/**
* Binds a named resource, specified by setDevice, to a stream.
*
* @param string $mode
* The mode parameter specifies the type of access you require to the stream (as `fopen()`).
*
* @return Windows
* @throws SerialException
*/
public function open (string $mode = 'r+b') : Windows {
$this->setPortOptions();
parent::open($mode);
error_clear_last();
$this->_serial = @fopen($this->_device, $mode);
if (!is_resource($this->_serial)) {
$error = error_get_last();
if (is_array($error)) {
$error = new SerialException($error['message'], 0, $error['type'], $error['file'], $error['line']);
}
throw new SerialException(sprintf('Unable to open device %s', $this->_device), 0, E_ERROR, null, null, $error);
}
return $this;
}
}
+176
View File
@@ -0,0 +1,176 @@
<?php
/**
* PHP-Serial: Serial port access convenience class (https://github.com/fawno/PHP-serial)
* Copyright (c) Fernando Herrero (https://github.com/alpha)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*
* @copyright Fernando Herrero (https://github.com/alpha)
* @link https://github.com/fawno/PHP-serial PHP-Serial
* @since 0.0.1
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Fawno\PhpSerial;
use Fawno\PhpSerial\SerialException;
use Fawno\PhpSerial\SerialConfig;
/**
* Serial provides general methods for serial communication.
*
* @package Fawno\PhpSerial
* @uses Fawno\PhpSerial\SerialException Provides custom exception
* @used-by Fawno\PhpSerial\SerialDio
* @used-by Fawno\PhpSerial\File\Darwing
* @used-by Fawno\PhpSerial\File\Linux
* @used-by Fawno\PhpSerial\File\Windows
*/
class Serial {
public const SERIAL_CANONICAL = [0, 1];
protected $_serial = null;
protected $_device = null;
protected array $_options = [];
/**
* Construct the serial interface. Sets the device path and register shutdown function.
*
* @param string|null $device
* @return void
*/
public function __construct (string $device, SerialConfig $config) {
$this->_device = $device;
$this->_options = $config->__toArray();
register_shutdown_function([$this, 'close']);
}
/**
* Sets blocking/non-blocking mode
*
* @param bool $enable
* If mode is FALSE, the given stream will be switched to non-blocking mode, and if TRUE, it will be switched to blocking mode. This affects calls like fgets and fread that read from the stream. In non-blocking mode an fgets call will always return right away while in blocking mode it will wait for data to become available on the stream.
*
* @return Serial
* @throws SerialException
*/
public function setBlocking (bool $enable) : Serial {
if (!is_resource($this->_serial)) {
throw new SerialException('Device must be opened to set blocking');
}
if (!stream_set_blocking($this->_serial, $enable)) {
throw new SerialException('Setting blocking error');
}
return $this;
}
/**
* Sets timeout period
*
* @param int $seconds
* The seconds part of the timeout to be set.
*
* @param int $microseconds
* The microseconds part of the timeout to be set.
*
* @return Serial
* @throws SerialException
*/
public function setTimeout (int $seconds = 0, int $microseconds = 0) : Serial {
if (!is_resource($this->_serial)) {
throw new SerialException('Device must be opened to set timeout');
}
if (!stream_set_timeout($this->_serial, $seconds, $microseconds)) {
throw new SerialException('Setting timeout error');
}
return $this;
}
/**
* Binds a named resource, specified by setDevice, to a stream.
*
* @param string $mode
* The mode parameter specifies the type of access you require to the stream (as `fopen()`).
*
* @return void
* @throws SerialException
*/
public function open (string $mode = 'r+b') {
if (is_resource($this->_serial)) {
throw new SerialException('The device is already opened');
}
if (empty($this->_device)) {
throw new SerialException('The device must be set before to be open');
}
if (!preg_match('~^[raw]\+?b?$~', $mode)) {
throw new SerialException(sprintf('Invalid opening mode: %s. Use fopen() modes.', $mode));
}
}
/**
* Closes the serial stream.
*
* @return void
* @throws SerialException
*/
public function close () {
if (is_resource($this->_serial)) {
if (!fclose($this->_serial)) {
throw new SerialException(sprintf('Unable to close the device %s', $this->_device));
}
}
$this->_serial = null;
}
/**
* Writes data to the serial stream.
*
* @param string $data
* The written data.
*
* @return int|false
* Returns the number of bytes written, or `false` on error.
*
* @throws SerialException
*/
public function send (string $data) {
if (!is_resource($this->_serial)) {
throw new SerialException('Device must be opened to write it');
}
return fwrite($this->_serial, $data);
}
/**
* Reads remainder of the serial stream into a string.
*
* @param int $length
* The maximum bytes to read. Defaults to -1 (read all the remainingbuffer).
*
* @param int $offset
* Seek to the specified offset before reading. If this number is negative,no seeking will occur and reading will start from the current position.
*
* @return string|false
* Returns a string or `false` on failure.
*
* @throws SerialException
*/
public function read (int $length = -1, int $offset = -1) {
if (!is_resource($this->_serial)) {
throw new SerialException('Device must be opened to read it');
}
return stream_get_contents($this->_serial, $length, $offset);
}
}
+126
View File
@@ -0,0 +1,126 @@
<?php
declare(strict_types=1);
namespace Fawno\PhpSerial;
use Fawno\PhpSerial\SerialException;
use Fawno\PhpSerial\Config\BaudRates;
use Fawno\PhpSerial\Config\StopBits;
use Fawno\PhpSerial\Config\Parity;
use Fawno\PhpSerial\Config\DataBits;
class SerialConfig {
protected int $data_rate = BaudRates::B9600;
protected int $data_bits = DataBits::CS8;
protected int $stop_bits = StopBits::ONE;
protected int $parity = Parity::NONE;
protected int $flow_control = 1;
protected int $is_canonical = 1;
/**
* Sets the data rate.
*
* @param int $data_rate
* @return SerialConfig
* @throws SerialException
*/
public function setBaudRate (int $data_rate) {
if (!BaudRates::checkValue($data_rate)) {
throw new SerialException(sprintf('Invalid data_rate value (%d)', $data_rate), SerialException::ERROR_DATA_RATE);
}
$this->data_rate = $data_rate;
return $this;
}
/**
* Sets the number of data bits.
*
* @param int $data_bits
* @return SerialConfig
* @throws SerialException
*/
public function setDataBits (int $data_bits) {
if (!DataBits::checkValue($data_bits)) {
throw new SerialException(sprintf('Invalid data_bits value (%d)', $data_bits), SerialException::ERROR_DATA_BITS);
}
$this->data_bits = $data_bits;
return $this;
}
/**
* Sets the number of stop bits.
*
* @param int $stop_bits
* @return SerialConfig
* @throws SerialException
*/
public function setStopBits (int $stop_bits) {
if (!StopBits::checkValue($stop_bits)) {
throw new SerialException(sprintf('Invalid stop_bits value (%d)', $stop_bits), SerialException::ERROR_STOP_BITS);
}
$this->stop_bits = $stop_bits;
return $this;
}
/**
* Sets the parity.
*
* @param int $parity
* @return SerialConfig
* @throws SerialException
*/
public function setParity (int $parity) {
if (!Parity::checkValue($parity)) {
throw new SerialException(sprintf('Invalid parity value (%d)', $parity), SerialException::ERROR_PARITY);
}
$this->parity = $parity;
return $this;
}
/**
* Sets the flow control.
*
* @param bool $flow_control
* @return SerialConfig
* @throws SerialException
*/
public function setFlowControl (bool $flow_control) {
if (!is_bool($flow_control)) {
throw new SerialException(sprintf('Invalid flow_control value (%d)', $flow_control));
}
$this->flow_control = $flow_control ? 1 : 0;
return $this;
}
/**
* Sets the canonical option of serial port.
*
* @param bool $canonical
*
* @return SerialConfig
* @throws SerialException
*/
public function setCanonical (bool $canonical) {
if (!is_bool($canonical)) {
throw new SerialException(sprintf('Invalid canonical value (%d)', $canonical));
}
$this->is_canonical = $canonical ? 1 : 0;
}
public function __toArray () : array {
return [
'data_rate' => $this->data_rate,
'data_bits' => $this->data_bits,
'stop_bits' => $this->stop_bits,
'parity' => $this->parity,
'flow_control' => $this->flow_control,
'is_canonical' => $this->is_canonical,
];
}
}
+94
View File
@@ -0,0 +1,94 @@
<?php
/**
* PHP-Serial: Serial port access convenience class (https://github.com/fawno/PHP-serial)
* Copyright (c) Fernando Herrero (https://github.com/alpha)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*
* @copyright Fernando Herrero (https://github.com/alpha)
* @link https://github.com/fawno/PHP-serial PHP-Serial
* @since 0.0.1
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Fawno\PhpSerial;
use Fawno\PhpSerial\Serial;
use Fawno\PhpSerial\SerialException;
/**
* SerialDio class provides serial connection using php-dio extension
*
* @package Fawno\PhpSerial
* @uses Fawno\PhpSerial\Serial Provides general serial methods
* @uses Fawno\PhpSerial\SerialException Provides custom exception
* @used-by Fawno\PhpSerial\SerialFile
*/
function dio_serial(string $filename, string $mode, ?array $options) {
}
function dio_raw(string $filename, string $mode, ?array $options) {
}
class SerialDio extends Serial {
/**
* Binds a named resource, specified by setDevice, to a stream.
*
* @param string $mode
* The mode parameter specifies the type of access you require to the stream (as `fopen()`).
*
* @return SerialDio
* @throws SerialException
*/
public function open (string $mode = 'r+b') : SerialDio {
parent::open($mode);
error_clear_last();
$this->_serial = dio_serial($this->_device, $mode, $this->_options);
if (!is_resource($this->_serial)) {
$error = error_get_last();
if (is_array($error)) {
$error = new SerialException($error['message'], 0, $error['type'], $error['file'], $error['line']);
throw new SerialException(sprintf('Unable to open the device %s', $this->_device), 0, E_ERROR, null, null, $error);
}
throw new SerialException(sprintf('Unable to open the device %s', $this->_device));
}
return $this;
}
/**
* Binds a named resource, specified by setDevice, to a raw stream.
*
* @param string $mode
* The mode parameter specifies the type of access you require to the stream (as `fopen()`).
*
* @return SerialDio
* @throws SerialException
*/
public function open_raw (string $mode = 'r+b') : SerialDio {
parent::open($mode);
error_clear_last();
$this->_serial = dio_raw($this->_device, $mode, $this->_options);
if (!is_resource($this->_serial)) {
$error = error_get_last();
if (is_array($error)) {
$error = new SerialException($error['message'], 0, $error['type'], $error['file'], $error['line']);
throw new SerialException(sprintf('Unable to open the device %s', $this->_device), 0, E_ERROR, null, null, $error);
}
throw new SerialException(sprintf('Unable to open the device %s', $this->_device));
}
return $this;
}
}
+90
View File
@@ -0,0 +1,90 @@
<?php
/**
* PHP-Serial: Serial port access convenience class (https://github.com/fawno/PHP-serial)
* Copyright (c) Fernando Herrero (https://github.com/alpha)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*
* @copyright Fernando Herrero (https://github.com/alpha)
* @link https://github.com/fawno/PHP-serial PHP-Serial
* @since 1.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Fawno\PhpSerial;
use \Throwable;
use \Exception;
/**
* Provides custom exception class.
*
* @package Fawno\PhpSerial
* @used-by Fawno\PhpSerial\Serial
* @used-by Fawno\PhpSerial\SerialDio
* @used-by Fawno\PhpSerial\SerialFile
* @used-by Fawno\PhpSerial\SerialFileDawing
* @used-by Fawno\PhpSerial\SerialFileLinux
* @used-by Fawno\PhpSerial\SerialFileWindows
*/
class SerialException extends Exception {
public const ERROR_INDETERMINATE = 0;
public const ERROR_DATA_RATE = 1;
public const ERROR_DATA_BITS = 2;
public const ERROR_STOP_BITS = 3;
public const ERROR_PARITY = 4;
protected int $severity;
/**
* Construct the exception. Note: The message is NOT binary safe.
*
* @param string $message
* [optional] The Exception message to throw.
*
* @param int $code
* [optional] The Exception code.
*
* @param int $severity
* [optional] The Exception severity.
* Note:
* While the severity can be any int value, it is intended that the error constants be used.
*
* @param string|null $filename
* [optional] The filename where the exception is thrown.
*
* @param int|null $line
* [optional] The line number where the exception is thrown.
*
* @param Throwable|null $previous
* [optional] The previous throwable used for the exception chaining.
*
* @return void
*/
public function __construct (string $message = '', int $code = self::ERROR_INDETERMINATE, int $severity = E_ERROR, string $filename = null, int $line = null , Throwable $previous = null) {
$this->severity = $severity;
if (!is_null($filename)) {
$this->filename = $filename;
}
if (!is_null($line)) {
$this->line = $line;
}
parent::__construct($message, $code, $previous);
}
/**
* Gets the exception severity
*
* @return int
* Returns the severity of the exception.
*/
public function getSeverity () : int {
return $this->severity;
}
}
+110
View File
@@ -0,0 +1,110 @@
<?php
/**
* PHP-Serial: Serial port access convenience class (https://github.com/fawno/PHP-serial)
* Copyright (c) Fernando Herrero (https://github.com/alpha)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*
* @copyright Fernando Herrero (https://github.com/alpha)
* @link https://github.com/fawno/PHP-serial PHP-Serial
* @since 0.1.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Fawno\PhpSerial;
use Fawno\PhpSerial\SerialException;
use Fawno\PhpSerial\SerialConfig;
use Fawno\PhpSerial\File\Windows;
use Fawno\PhpSerial\File\Linux;
use Fawno\PhpSerial\File\Darwing;
$sysName = php_uname();
$osName = preg_replace('~^.*(Linux|Darwing|Windows).*$~', '$1', $sysName);
$classFile = __DIR__ . '/File/' . $osName . '.php';
if (is_file($classFile)) {
include_once $classFile;
}
if (!class_exists('Fawno\\PhpSerial\\File\\' . $osName)) {
throw new SerialException(sprintf('Host OS "%s" is unsupported', $osName));
}
switch ($osName) {
case 'Windows':
{
/**
* SerialFile class provides serial connection using file stream.
* Is a wrapper for specific OS class.
*
* @package Fawno\PhpSerial
* @uses Fawno\PhpSerial\File\Windows Provides serial connection using file stream in Windows OS
* @uses Fawno\PhpSerial\SerialException
*/
class SerialFile extends Windows {
/**
* Construct the serial interface. Sets the device path and register shutdown function.
*
* @param string|null $device
* @return void
*/
public function __construct (string $device = null, SerialConfig $config) {
parent::__construct($device, $config);
unset($this->_options['is_canonical']);
}
}
}
break;
case 'Linux':
{
/**
* SerialFile class provides serial connection using file stream.
* Is a wrapper for specific OS class.
*
* @package Fawno\PhpSerial
* @uses Fawno\PhpSerial\SerialFileLinux Provides serial connection using file stream in Linux OS
* @uses Fawno\PhpSerial\SerialException
*/
class SerialFile extends Linux {
/**
* Construct the serial interface. Sets the device path and register shutdown function.
*
* @param string|null $device
* @return void
*/
public function __construct (string $device = null, SerialConfig $config) {
parent::__construct($device, $config);
unset($this->_options['is_canonical']);
}
}
}
break;
case 'Darwing':
{
/**
* SerialFile class provides serial connection using file stream.
* Is a wrapper for specific OS class.
*
* @package Fawno\PhpSerial
* @uses Fawno\PhpSerial\SerialFileDarwing Provides serial connection using file stream in OSX OS
* @uses Fawno\PhpSerial\SerialException
*/
class SerialFile extends Darwing {
/**
* Construct the serial interface. Sets the device path and register shutdown function.
*
* @param string|null $device
* @return void
*/
public function __construct (string $device = null, SerialConfig $config) {
parent::__construct($device, $config);
unset($this->_options['is_canonical']);
}
}
}
break;
}