From bcabe5b24150308c13d928a88d65be7656ef4ebe Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 1 Apr 2020 21:27:45 +0300 Subject: [PATCH 1/9] First commit --- composer.json | 6 +- examples/sms.php | 11 +- src/Interfaces/BaudInterface.php | 19 ++ src/Interfaces/ParityInterface.php | 10 + src/PhpSerial.php | 427 +++++++++++++---------------- 5 files changed, 225 insertions(+), 248 deletions(-) create mode 100644 src/Interfaces/BaudInterface.php create mode 100644 src/Interfaces/ParityInterface.php diff --git a/composer.json b/composer.json index 1b53508..7431911 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "hyperthese/php-serial", + "name": "zzepish/php-serial", "description": "Multi-platform serial port access convenience class", "license": "GNU GPLv2", "authors": [ @@ -10,7 +10,9 @@ ], "minimum-stability": "stable", "autoload": { - "classmap": ["src/"] + "psr-4": { + "PhpSerial\\": "src/" + } }, "extra": { "branch-alias": { diff --git a/examples/sms.php b/examples/sms.php index a877de6..925e331 100644 --- a/examples/sms.php +++ b/examples/sms.php @@ -1,8 +1,11 @@ deviceOpen('w+'); // We may need to return if nothing happens for 10 seconds -stream_set_timeout($serial->_dHandle, 10); +stream_set_timeout($serial->dHandle, 10); // We can change the baud rate -$serial->confBaudRate(9600); +$serial->confBaudRate(BaudInterface::RATE_9600); // SMS inbox query - mode command and list command $serial->sendMessage("AT",1); diff --git a/src/Interfaces/BaudInterface.php b/src/Interfaces/BaudInterface.php new file mode 100644 index 0000000..c7e8b1e --- /dev/null +++ b/src/Interfaces/BaudInterface.php @@ -0,0 +1,19 @@ +_os = "linux"; - + $this->os = "linux"; if ($this->_exec("stty") === 0) { - register_shutdown_function(array($this, "deviceClose")); + register_shutdown_function([$this, "deviceClose"]); } else { trigger_error( "No stty availible, unable to run.", @@ -56,14 +52,16 @@ public function PhpSerial() ); } } elseif (substr($sysName, 0, 6) === "Darwin") { - $this->_os = "osx"; - register_shutdown_function(array($this, "deviceClose")); + $this->os = "osx"; + register_shutdown_function([$this, "deviceClose"]); } elseif (substr($sysName, 0, 7) === "Windows") { - $this->_os = "windows"; - register_shutdown_function(array($this, "deviceClose")); + $this->os = "windows"; + register_shutdown_function([$this, "deviceClose"]); } else { - trigger_error("Host OS is neither osx, linux nor windows, unable " . - "to run.", E_USER_ERROR); + trigger_error( + "Host OS is neither osx, linux nor windows, unable " . "to run.", + E_USER_ERROR + ); exit(); } } @@ -71,58 +69,52 @@ public function PhpSerial() // // OPEN/CLOSE DEVICE SECTION -- {START} // - /** * Device set function : used to set the device name/address. * -> linux : use the device address, like /dev/ttyS0 * -> osx : use the device address, like /dev/tty.serial * -> windows : use the COMxx device name, like COM1 (can also be used * with linux) - * - * @param string $device the name of the device to be used - * @return bool */ - public function deviceSet($device) + public function deviceSet(string $device): bool { - if ($this->_dState !== SERIAL_DEVICE_OPENED) { - if ($this->_os === "linux") { + if ($this->dState !== SERIAL_DEVICE_OPENED) { + if ($this->os === "linux") { if (preg_match("@^COM(\\d+):?$@i", $device, $matches)) { $device = "/dev/ttyS" . ($matches[1] - 1); } - if ($this->_exec("stty -F " . $device) === 0) { - $this->_device = $device; - $this->_dState = SERIAL_DEVICE_SET; + $this->device = $device; + $this->dState = SERIAL_DEVICE_SET; return true; } - } elseif ($this->_os === "osx") { + } elseif ($this->os === "osx") { if ($this->_exec("stty -f " . $device) === 0) { - $this->_device = $device; - $this->_dState = SERIAL_DEVICE_SET; + $this->device = $device; + $this->dState = SERIAL_DEVICE_SET; return true; } - } elseif ($this->_os === "windows") { - if (preg_match("@^COM(\\d+):?$@i", $device, $matches) - and $this->_exec( - exec("mode " . $device . " xon=on BAUD=9600") - ) === 0 - ) { - $this->_winDevice = "COM" . $matches[1]; - $this->_device = "\\.com" . $matches[1]; - $this->_dState = SERIAL_DEVICE_SET; + } elseif ($this->os === "windows") { + if (preg_match("@^COM(\\d+):?$@i", $device, $matches) and $this->_exec( + exec("mode " . $device . " xon=on BAUD=9600") + ) === 0) { + $this->winDevice = "COM" . $matches[1]; + $this->device = "\\.com" . $matches[1]; + $this->dState = SERIAL_DEVICE_SET; return true; } } - trigger_error("Specified serial port is not valid", E_USER_WARNING); return false; } else { - trigger_error("You must close your device before to set an other " . - "one", E_USER_WARNING); + trigger_error( + "You must close your device before to set an other " . "one", + E_USER_WARNING + ); return false; } @@ -130,19 +122,15 @@ public function deviceSet($device) /** * Opens the device for reading and/or writing. - * - * @param string $mode Opening mode : same parameter as fopen() - * @return bool */ - public function deviceOpen($mode = "r+b") + public function deviceOpen(string $mode = "r+b"): bool { - if ($this->_dState === SERIAL_DEVICE_OPENED) { + if ($this->dState === SERIAL_DEVICE_OPENED) { trigger_error("The device is already opened", E_USER_NOTICE); return true; } - - if ($this->_dState === SERIAL_DEVICE_NOTSET) { + if ($this->dState === SERIAL_DEVICE_NOTSET) { trigger_error( "The device must be set before to be open", E_USER_WARNING @@ -150,26 +138,22 @@ public function deviceOpen($mode = "r+b") return false; } - if (!preg_match("@^[raw]\\+?b?$@", $mode)) { trigger_error( - "Invalid opening mode : ".$mode.". Use fopen() modes.", + "Invalid opening mode : " . $mode . ". Use fopen() modes.", E_USER_WARNING ); return false; } - - $this->_dHandle = @fopen($this->_device, $mode); - - if ($this->_dHandle !== false) { - stream_set_blocking($this->_dHandle, 0); - $this->_dState = SERIAL_DEVICE_OPENED; + $this->dHandle = @fopen($this->device, $mode); + if ($this->dHandle !== false) { + stream_set_blocking($this->dHandle, 0); + $this->dState = SERIAL_DEVICE_OPENED; return true; } - - $this->_dHandle = null; + $this->dHandle = null; trigger_error("Unable to open the device", E_USER_WARNING); return false; @@ -177,22 +161,18 @@ public function deviceOpen($mode = "r+b") /** * Closes the device - * - * @return bool */ - public function deviceClose() + public function deviceClose(): bool { - if ($this->_dState !== SERIAL_DEVICE_OPENED) { + if ($this->dState !== SERIAL_DEVICE_OPENED) { return true; } - - if (fclose($this->_dHandle)) { - $this->_dHandle = null; - $this->_dState = SERIAL_DEVICE_SET; + if (fclose($this->dHandle)) { + $this->dHandle = null; + $this->dState = SERIAL_DEVICE_SET; return true; } - trigger_error("Unable to close the device", E_USER_ERROR); return false; @@ -201,63 +181,57 @@ public function deviceClose() // // OPEN/CLOSE DEVICE SECTION -- {STOP} // - // // CONFIGURE SECTION -- {START} // - /** * Configure the Baud Rate * Possible rates : 110, 150, 300, 600, 1200, 2400, 4800, 9600, 38400, * 57600 and 115200. - * - * @param int $rate the rate to set the port in - * @return bool */ - public function confBaudRate($rate) + public function confBaudRate(int $rate): bool { - if ($this->_dState !== SERIAL_DEVICE_SET) { - trigger_error("Unable to set the baud rate : the device is " . - "either not set or opened", E_USER_WARNING); + if ($this->dState !== SERIAL_DEVICE_SET) { + trigger_error( + "Unable to set the baud rate : the device is " . "either not set or opened", + E_USER_WARNING + ); return false; } - - $validBauds = array ( - 110 => 11, - 150 => 15, - 300 => 30, - 600 => 60, - 1200 => 12, - 2400 => 24, - 4800 => 48, - 9600 => 96, - 19200 => 19, - 38400 => 38400, - 57600 => 57600, - 115200 => 115200 - ); - + $validBauds = [ + BaudInterface::RATE_110 => 11, + BaudInterface::RATE_150 => 15, + BaudInterface::RATE_300 => 30, + BaudInterface::RATE_600 => 60, + BaudInterface::RATE_1200 => 12, + BaudInterface::RATE_2400 => 24, + BaudInterface::RATE_4800 => 48, + BaudInterface::RATE_9600 => 96, + BaudInterface::RATE_19200 => 19, + BaudInterface::RATE_38400 => 38400, + BaudInterface::RATE_57600 => 57600, + BaudInterface::RATE_115200 => 115200 + ]; if (isset($validBauds[$rate])) { - if ($this->_os === "linux") { + if ($this->os === "linux") { $ret = $this->_exec( - "stty -F " . $this->_device . " " . (int) $rate, + "stty -F " . $this->device . " " . (int)$rate, $out ); - } elseif ($this->_os === "osx") { + } elseif ($this->os === "osx") { $ret = $this->_exec( - "stty -f " . $this->_device . " " . (int) $rate, + "stty -f " . $this->device . " " . (int)$rate, $out ); - } elseif ($this->_os === "windows") { + } elseif ($this->os === "windows") { $ret = $this->_exec( - "mode " . $this->_winDevice . " BAUD=" . $validBauds[$rate], + "mode " . $this->winDevice . " BAUD=" . $validBauds[$rate], $out ); } else { return false; } - if ($ret !== 0) { trigger_error( "Unable to set baud rate: " . $out[1], @@ -276,13 +250,10 @@ public function confBaudRate($rate) /** * Configure parity. * Modes : odd, even, none - * - * @param string $parity one of the modes - * @return bool */ - public function confParity($parity) + public function confParity(string $parity): bool { - if ($this->_dState !== SERIAL_DEVICE_SET) { + if ($this->dState !== SERIAL_DEVICE_SET) { trigger_error( "Unable to set parity : the device is either not set or opened", E_USER_WARNING @@ -290,40 +261,35 @@ public function confParity($parity) return false; } - - $args = array( - "none" => "-parenb", - "odd" => "parenb parodd", - "even" => "parenb -parodd", - ); - + $args = [ + ParityInterface::NONE => "-parenb", + ParityInterface::ODD => "parenb parodd", + ParityInterface::EVEN => "parenb -parodd", + ]; if (!isset($args[$parity])) { trigger_error("Parity mode not supported", E_USER_WARNING); return false; } - - if ($this->_os === "linux") { + if ($this->os === "linux") { $ret = $this->_exec( - "stty -F " . $this->_device . " " . $args[$parity], + "stty -F " . $this->device . " " . $args[$parity], $out ); - } elseif ($this->_os === "osx") { + } elseif ($this->os === "osx") { $ret = $this->_exec( - "stty -f " . $this->_device . " " . $args[$parity], + "stty -f " . $this->device . " " . $args[$parity], $out ); } else { $ret = $this->_exec( - "mode " . $this->_winDevice . " PARITY=" . $parity{0}, + "mode " . $this->winDevice . " PARITY=" . $parity[0], $out ); } - if ($ret === 0) { return true; } - trigger_error("Unable to set parity : " . $out[1], E_USER_WARNING); return false; @@ -332,48 +298,45 @@ public function confParity($parity) /** * Sets the length of a character. * - * @param int $int length of a character (5 <= length <= 8) - * @return bool + * $length length of a character (5 <= length <= 8) */ - public function confCharacterLength($int) + public function confCharacterLength(int $length): bool { - if ($this->_dState !== SERIAL_DEVICE_SET) { - trigger_error("Unable to set length of a character : the device " . - "is either not set or opened", E_USER_WARNING); + if ($this->dState !== SERIAL_DEVICE_SET) { + trigger_error( + "Unable to set length of a character : the device " . "is either not set or opened", + E_USER_WARNING + ); return false; } - - $int = (int) $int; - if ($int < 5) { - $int = 5; - } elseif ($int > 8) { - $int = 8; + $length = (int)$length; + if ($length < 5) { + $length = 5; + } elseif ($length > 8) { + $length = 8; } - - if ($this->_os === "linux") { + if ($this->os === "linux") { $ret = $this->_exec( - "stty -F " . $this->_device . " cs" . $int, + "stty -F " . $this->device . " cs" . $length, $out ); - } elseif ($this->_os === "osx") { + } elseif ($this->os === "osx") { $ret = $this->_exec( - "stty -f " . $this->_device . " cs" . $int, + "stty -f " . $this->device . " cs" . $length, $out ); } else { $ret = $this->_exec( - "mode " . $this->_winDevice . " DATA=" . $int, + "mode " . $this->winDevice . " DATA=" . $length, $out ); } - if ($ret === 0) { return true; } - trigger_error( - "Unable to set character length : " .$out[1], + "Unable to set character length : " . $out[1], E_USER_WARNING ); @@ -383,25 +346,21 @@ public function confCharacterLength($int) /** * Sets the length of stop bits. * - * @param float $length the length of a stop bit. It must be either 1, + * $length the length of a stop bit. It must be either 1, * 1.5 or 2. 1.5 is not supported under linux and on * some computers. - * @return bool */ - public function confStopBits($length) + public function confStopBits(float $length): bool { - if ($this->_dState !== SERIAL_DEVICE_SET) { - trigger_error("Unable to set the length of a stop bit : the " . - "device is either not set or opened", E_USER_WARNING); + if ($this->dState !== SERIAL_DEVICE_SET) { + trigger_error( + "Unable to set the length of a stop bit : the " . "device is either not set or opened", + E_USER_WARNING + ); return false; } - - if ($length != 1 - and $length != 2 - and $length != 1.5 - and !($length == 1.5 and $this->_os === "linux") - ) { + if ($length != 1 and $length != 2 and $length != 1.5 and !($length == 1.5 and $this->os === "linux")) { trigger_error( "Specified stop bit length is invalid", E_USER_WARNING @@ -409,30 +368,25 @@ public function confStopBits($length) return false; } - - if ($this->_os === "linux") { + if ($this->os === "linux") { $ret = $this->_exec( - "stty -F " . $this->_device . " " . - (($length == 1) ? "-" : "") . "cstopb", + "stty -F " . $this->device . " " . (($length == 1) ? "-" : "") . "cstopb", $out ); - } elseif ($this->_os === "osx") { + } elseif ($this->os === "osx") { $ret = $this->_exec( - "stty -f " . $this->_device . " " . - (($length == 1) ? "-" : "") . "cstopb", + "stty -f " . $this->device . " " . (($length == 1) ? "-" : "") . "cstopb", $out ); } else { $ret = $this->_exec( - "mode " . $this->_winDevice . " STOP=" . $length, + "mode " . $this->winDevice . " STOP=" . $length, $out ); } - if ($ret === 0) { return true; } - trigger_error( "Unable to set stop bit length : " . $out[1], E_USER_WARNING @@ -444,55 +398,54 @@ public function confStopBits($length) /** * Configures the flow control * - * @param string $mode Set the flow control mode. Availible modes : + * @param string $mode Set the flow control mode. Availible modes : * -> "none" : no flow control * -> "rts/cts" : use RTS/CTS handshaking * -> "xon/xoff" : use XON/XOFF protocol + * * @return bool */ public function confFlowControl($mode) { - if ($this->_dState !== SERIAL_DEVICE_SET) { - trigger_error("Unable to set flow control mode : the device is " . - "either not set or opened", E_USER_WARNING); + if ($this->dState !== SERIAL_DEVICE_SET) { + trigger_error( + "Unable to set flow control mode : the device is " . "either not set or opened", + E_USER_WARNING + ); return false; } - - $linuxModes = array( + $linuxModes = [ "none" => "clocal -crtscts -ixon -ixoff", "rts/cts" => "-clocal crtscts -ixon -ixoff", "xon/xoff" => "-clocal -crtscts ixon ixoff" - ); - $windowsModes = array( + ]; + $windowsModes = [ "none" => "xon=off octs=off rts=on", "rts/cts" => "xon=off octs=on rts=hs", "xon/xoff" => "xon=on octs=off rts=on", - ); - + ]; if ($mode !== "none" and $mode !== "rts/cts" and $mode !== "xon/xoff") { trigger_error("Invalid flow control mode specified", E_USER_ERROR); return false; } - - if ($this->_os === "linux") { + if ($this->os === "linux") { $ret = $this->_exec( - "stty -F " . $this->_device . " " . $linuxModes[$mode], + "stty -F " . $this->device . " " . $linuxModes[$mode], $out ); - } elseif ($this->_os === "osx") { + } elseif ($this->os === "osx") { $ret = $this->_exec( - "stty -f " . $this->_device . " " . $linuxModes[$mode], + "stty -f " . $this->device . " " . $linuxModes[$mode], $out ); } else { $ret = $this->_exec( - "mode " . $this->_winDevice . " " . $windowsModes[$mode], + "mode " . $this->winDevice . " " . $windowsModes[$mode], $out ); } - if ($ret === 0) { return true; } else { @@ -508,11 +461,12 @@ public function confFlowControl($mode) /** * Sets a setserial parameter (cf man setserial) * NO MORE USEFUL ! - * -> No longer supported - * -> Only use it if you need it + * -> No longer supported + * -> Only use it if you need it + * + * @param string $param parameter name + * @param string $arg parameter value * - * @param string $param parameter name - * @param string $arg parameter value * @return bool */ public function setSetserialFlag($param, $arg = "") @@ -520,16 +474,14 @@ public function setSetserialFlag($param, $arg = "") if (!$this->_ckOpened()) { return false; } - $return = exec( - "setserial " . $this->_device . " " . $param . " " . $arg . " 2>&1" + "setserial " . $this->device . " " . $param . " " . $arg . " 2>&1" ); - - if ($return{0} === "I") { + if (strpos($return, 'I') === 0) { trigger_error("setserial: Invalid flag", E_USER_WARNING); return false; - } elseif ($return{0} === "/") { + } elseif (strpos($return, '/') === 0) { trigger_error("setserial: Error with device file", E_USER_WARNING); return false; @@ -541,26 +493,22 @@ public function setSetserialFlag($param, $arg = "") // // CONFIGURE SECTION -- {STOP} // - // // I/O SECTION -- {START} // - /** * Sends a string to the device * - * @param string $str string to be sent to the device + * @param string $str string to be sent to the device * @param float $waitForReply time to wait for the reply (in seconds) */ public function sendMessage($str, $waitForReply = 0.1) { - $this->_buffer .= $str; - + $this->buffer .= $str; if ($this->autoFlush === true) { $this->serialflush(); } - - usleep((int) ($waitForReply * 1000000)); + usleep((int)($waitForReply * 1000000)); } /** @@ -568,52 +516,52 @@ public function sendMessage($str, $waitForReply = 0.1) * * @param int $count Number of characters to be read (will stop before * if less characters are in the buffer) + * * @return string */ - public function readPort($count = 0) + public function readPort(int $count = 0): string { - if ($this->_dState !== SERIAL_DEVICE_OPENED) { + if ($this->dState !== SERIAL_DEVICE_OPENED) { trigger_error("Device must be opened to read it", E_USER_WARNING); return false; } - - if ($this->_os === "linux" || $this->_os === "osx") { + if ($this->os === "linux" || $this->os === "osx") { // Behavior in OSX isn't to wait for new data to recover, but just // grabs what's there! // Doesn't always work perfectly for me in OSX - $content = ""; $i = 0; - + $content = ""; + $i = 0; if ($count !== 0) { do { if ($i > $count) { - $content .= fread($this->_dHandle, ($count - $i)); + $content .= fread($this->dHandle, ($count - $i)); } else { - $content .= fread($this->_dHandle, 128); + $content .= fread($this->dHandle, 128); } } while (($i += 128) === strlen($content)); } else { do { - $content .= fread($this->_dHandle, 128); + $content .= fread($this->dHandle, 128); } while (($i += 128) === strlen($content)); } return $content; - } elseif ($this->_os === "windows") { + } elseif ($this->os === "windows") { // Windows port reading procedures still buggy - $content = ""; $i = 0; - + $content = ""; + $i = 0; if ($count !== 0) { do { if ($i > $count) { - $content .= fread($this->_dHandle, ($count - $i)); + $content .= fread($this->dHandle, ($count - $i)); } else { - $content .= fread($this->_dHandle, 128); + $content .= fread($this->dHandle, 128); } } while (($i += 128) === strlen($content)); } else { do { - $content .= fread($this->_dHandle, 128); + $content .= fread($this->dHandle, 128); } while (($i += 128) === strlen($content)); } @@ -634,13 +582,12 @@ public function serialflush() if (!$this->_ckOpened()) { return false; } - - if (fwrite($this->_dHandle, $this->_buffer) !== false) { - $this->_buffer = ""; + if (fwrite($this->dHandle, $this->buffer) !== false) { + $this->buffer = ""; return true; } else { - $this->_buffer = ""; + $this->buffer = ""; trigger_error("Error while sending message", E_USER_WARNING); return false; @@ -650,14 +597,12 @@ public function serialflush() // // I/O SECTION -- {STOP} // - // // INTERNAL TOOLKIT -- {START} // - public function _ckOpened() { - if ($this->_dState !== SERIAL_DEVICE_OPENED) { + if ($this->dState !== SERIAL_DEVICE_OPENED) { trigger_error("Device must be opened", E_USER_WARNING); return false; @@ -668,7 +613,7 @@ public function _ckOpened() public function _ckClosed() { - if ($this->_dState === SERIAL_DEVICE_OPENED) { + if ($this->dState === SERIAL_DEVICE_OPENED) { trigger_error("Device must be closed", E_USER_WARNING); return false; @@ -679,22 +624,20 @@ public function _ckClosed() public function _exec($cmd, &$out = null) { - $desc = array( - 1 => array("pipe", "w"), - 2 => array("pipe", "w") - ); - + $desc = [ + 1 => ["pipe", "w"], + 2 => ["pipe", "w"] + ]; $proc = proc_open($cmd, $desc, $pipes); - - $ret = stream_get_contents($pipes[1]); - $err = stream_get_contents($pipes[2]); - + $ret = stream_get_contents($pipes[1]); + $err = stream_get_contents($pipes[2]); fclose($pipes[1]); fclose($pipes[2]); - $retVal = proc_close($proc); + if (func_num_args() == 2) { + $out = [$ret, $err]; + } - if (func_num_args() == 2) $out = array($ret, $err); return $retVal; } From 4742b8b20873a26835995e9b3e2951434aa708c7 Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 1 Apr 2020 21:38:16 +0300 Subject: [PATCH 2/9] Structural fixes --- examples/VS421CPNTA.php | 102 ------------------------ examples/dummy.php | 17 ++-- src/Interfaces/FlowControlInterface.php | 10 +++ src/Interfaces/ParityInterface.php | 2 +- 4 files changed, 22 insertions(+), 109 deletions(-) delete mode 100644 examples/VS421CPNTA.php create mode 100644 src/Interfaces/FlowControlInterface.php diff --git a/examples/VS421CPNTA.php b/examples/VS421CPNTA.php deleted file mode 100644 index 58d8d6b..0000000 --- a/examples/VS421CPNTA.php +++ /dev/null @@ -1,102 +0,0 @@ -"; - echo "
- - - - - - -
Switch to input? :
-
"; - echo ""; -} else { - -include 'PhpSerial.php'; - -// Let's start the class -$serial = new PhpSerial; - -// First we must specify the device. This works on both linux and windows (if -// your linux serial device is /dev/ttyS0 for COM1, etc) -// $serial->deviceSet("COM1"); -$serial->deviceSet("/dev/cu.usbserial-FTDY7ID6"); - -// We can change the baud rate, parity, length, stop bits, flow control -$serial->confBaudRate(2400); -$serial->confParity("none"); -$serial->confCharacterLength(8); -$serial->confStopBits(1); -$serial->confFlowControl("none"); - -// Then we need to open it -$serial->deviceOpen(); - -// To write into -$serial->sendMessage("I".$the_input); - -// Or to read from -$read = ''; -$theResult = ''; -$start = microtime_float(); - -while ( ($read == '') && (microtime_float() <= $start + 0.5) ) { - $read = $serial->readPort(); - if ($read != '') { - $theResult .= $read; - $read = ''; - } -} - -// If you want to change the configuration, the device must be closed -$serial->deviceClose(); - -// etc... - -echo "Read data: ".$theResult."
"; - -echo "
- - - - - - - -
Switch to input? :
-
"; -} diff --git a/examples/dummy.php b/examples/dummy.php index 486031f..ef1e696 100644 --- a/examples/dummy.php +++ b/examples/dummy.php @@ -1,19 +1,24 @@ deviceSet("COM1"); // We can change the baud rate, parity, length, stop bits, flow control -$serial->confBaudRate(2400); -$serial->confParity("none"); +$serial->confBaudRate(BaudInterface::RATE_2400); +$serial->confParity(ParityInterface::NONE); $serial->confCharacterLength(8); $serial->confStopBits(1); -$serial->confFlowControl("none"); +$serial->confFlowControl(FlowControlInterface::NONE); // Then we need to open it $serial->deviceOpen(); @@ -28,7 +33,7 @@ $serial->deviceClose(); // We can change the baud rate -$serial->confBaudRate(2400); +$serial->confBaudRate(BaudInterface::RATE_2400); // etc... // diff --git a/src/Interfaces/FlowControlInterface.php b/src/Interfaces/FlowControlInterface.php new file mode 100644 index 0000000..f02a272 --- /dev/null +++ b/src/Interfaces/FlowControlInterface.php @@ -0,0 +1,10 @@ + Date: Wed, 1 Apr 2020 21:44:55 +0300 Subject: [PATCH 3/9] Structural fixes --- composer.json | 5 ----- 1 file changed, 5 deletions(-) diff --git a/composer.json b/composer.json index 7431911..e725098 100644 --- a/composer.json +++ b/composer.json @@ -13,10 +13,5 @@ "psr-4": { "PhpSerial\\": "src/" } - }, - "extra": { - "branch-alias": { - "dev-develop": "1.0.x-dev" - } } } From 6d7fdc8c643b6db87501256b396d44b21c995139 Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 1 Apr 2020 21:47:53 +0300 Subject: [PATCH 4/9] Structural fixes --- src/Interfaces/FlowControlInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Interfaces/FlowControlInterface.php b/src/Interfaces/FlowControlInterface.php index f02a272..f5c5f32 100644 --- a/src/Interfaces/FlowControlInterface.php +++ b/src/Interfaces/FlowControlInterface.php @@ -7,4 +7,4 @@ class FlowControlInterface public const NONE = 'none'; public const XON_XOFF = 'xon/xoff'; public const RST_CTS = 'rts/cts'; -} \ No newline at end of file +} From 80c6552e576169e5044249444c7c765da86c3800 Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 1 Apr 2020 21:58:17 +0300 Subject: [PATCH 5/9] Tag fix --- src/PhpSerial.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/PhpSerial.php b/src/PhpSerial.php index 950e0e7..bb16929 100644 --- a/src/PhpSerial.php +++ b/src/PhpSerial.php @@ -645,3 +645,4 @@ public function _exec($cmd, &$out = null) // INTERNAL TOOLKIT -- {STOP} // } + From a9221a86e64339cb773d3f9871d6c97823e7d4c1 Mon Sep 17 00:00:00 2001 From: Artem Date: Sat, 4 Apr 2020 16:07:47 +0300 Subject: [PATCH 6/9] Stty fix --- src/PhpSerial.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpSerial.php b/src/PhpSerial.php index bb16929..e6102fa 100644 --- a/src/PhpSerial.php +++ b/src/PhpSerial.php @@ -43,7 +43,7 @@ public function __construct() $sysName = php_uname(); if (substr($sysName, 0, 5) === "Linux") { $this->os = "linux"; - if ($this->_exec("stty") === 0) { + if ($this->_exec("stty --version") === 0) { register_shutdown_function([$this, "deviceClose"]); } else { trigger_error( From f4081b83cb49642a3a125de16a9ba07b7e20d5e0 Mon Sep 17 00:00:00 2001 From: Artem Date: Sat, 4 Apr 2020 16:11:24 +0300 Subject: [PATCH 7/9] License fix --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index e725098..088be55 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "zzepish/php-serial", "description": "Multi-platform serial port access convenience class", - "license": "GNU GPLv2", + "license": "GPL-3.0-only", "authors": [ { "name": "Rémy Sanchez", From e1994d285cf64d3b67e898b71dbdcf4eafcdd3c4 Mon Sep 17 00:00:00 2001 From: Artem Date: Mon, 6 Apr 2020 17:09:39 +0300 Subject: [PATCH 8/9] Added device close on __destruct --- src/PhpSerial.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/PhpSerial.php b/src/PhpSerial.php index e6102fa..5d67aad 100644 --- a/src/PhpSerial.php +++ b/src/PhpSerial.php @@ -644,5 +644,10 @@ public function _exec($cmd, &$out = null) // // INTERNAL TOOLKIT -- {STOP} // + + public function __destruct() + { + $this->deviceClose(); + } } From 1eeb1bd10669700e195b22723c3787e90cb165d0 Mon Sep 17 00:00:00 2001 From: Artem Date: Mon, 6 Apr 2020 17:15:10 +0300 Subject: [PATCH 9/9] Removed minimum stability --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index 088be55..5af5f45 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,6 @@ "email": "remy.sanchez@hyperthese.net" } ], - "minimum-stability": "stable", "autoload": { "psr-4": { "PhpSerial\\": "src/"