From d45fb08e5a12bc71dd825b0868fbe0a7f88b1b26 Mon Sep 17 00:00:00 2001 From: Jeroi Date: Sun, 6 Apr 2014 13:31:40 +0300 Subject: [PATCH 1/6] Fixed linux deviceSet when using some other names thant ttyS ie ttyACM or others. Now it checks weather the device exists in linux and then sets device. --- src/PhpSerial.php | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/PhpSerial.php b/src/PhpSerial.php index 1d253d5..2a6e436 100644 --- a/src/PhpSerial.php +++ b/src/PhpSerial.php @@ -86,16 +86,11 @@ public function deviceSet($device) { 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; - - return true; - } + if ($this->_exec("stty -F /dev/" . $device) === 0) { + $this->_device = "/dev/".$device; + $this->_dState = SERIAL_DEVICE_SET; + return true; + } } elseif ($this->_os === "osx") { if ($this->_exec("stty -f " . $device) === 0) { $this->_device = $device; From b3cbbd8d0175537fae6837770c7106d52d4b7087 Mon Sep 17 00:00:00 2001 From: Jeroi Date: Sun, 6 Apr 2014 13:48:03 +0300 Subject: [PATCH 2/6] Edited Readme.md to cover code changes in deviceSet --- Readme.md | 4 ++-- src/PhpSerial.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Readme.md b/Readme.md index 82fbf93..e580ddc 100644 --- a/Readme.md +++ b/Readme.md @@ -21,8 +21,8 @@ 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) +// First we must specify the device. This works on both linux and windows +// In windows you have to spesify COM name, in linux it's ttyS0 or ttyACM0 similar name port name and in Mac it's tty.serial or similar serial name) $serial->deviceSet("COM1"); // We can change the baud rate, parity, length, stop bits, flow control diff --git a/src/PhpSerial.php b/src/PhpSerial.php index 2a6e436..fa4d1e4 100644 --- a/src/PhpSerial.php +++ b/src/PhpSerial.php @@ -74,7 +74,7 @@ public function PhpSerial() /** * Device set function : used to set the device name/address. - * -> linux : use the device address, like /dev/ttyS0 + * -> linux : use the device port name, like ttyS0 or ttyACM0 * -> osx : use the device address, like /dev/tty.serial * -> windows : use the COMxx device name, like COM1 (can also be used * with linux) @@ -92,7 +92,7 @@ public function deviceSet($device) return true; } } elseif ($this->_os === "osx") { - if ($this->_exec("stty -f " . $device) === 0) { + if ($this->_exec("stty -f /dev/" . $device) === 0) { $this->_device = $device; $this->_dState = SERIAL_DEVICE_SET; From 520659c5b3a33b7e09137bb14a2c5f5c32e24f5d Mon Sep 17 00:00:00 2001 From: Jeroi Date: Sun, 6 Apr 2014 13:55:27 +0300 Subject: [PATCH 3/6] Rewrite some instruction helpers at deviceSet function --- src/PhpSerial.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/PhpSerial.php b/src/PhpSerial.php index fa4d1e4..580af38 100644 --- a/src/PhpSerial.php +++ b/src/PhpSerial.php @@ -74,10 +74,9 @@ public function PhpSerial() /** * Device set function : used to set the device name/address. - * -> linux : use the device port name, like ttyS0 or ttyACM0 - * -> osx : use the device address, like /dev/tty.serial - * -> windows : use the COMxx device name, like COM1 (can also be used - * with linux) + * -> linux : use the device port name, like ttyS0 or ttyACM0 and simlars + * -> osx : use the device address, like tty.serial and similars + * -> windows : use the COMxx device name, like COM1 * * @param string $device the name of the device to be used * @return bool From a75fed12b9d1ea1a39cdfc53867a9585680a048c Mon Sep 17 00:00:00 2001 From: Jeroi Date: Sun, 6 Apr 2014 14:42:45 +0300 Subject: [PATCH 4/6] Added now COMXX support for linux also. --- Readme.md | 5 +++-- src/PhpSerial.php | 20 ++++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Readme.md b/Readme.md index e580ddc..e2b85c8 100644 --- a/Readme.md +++ b/Readme.md @@ -21,8 +21,9 @@ include 'PhpSerial.php'; // Let's start the class $serial = new PhpSerial; -// First we must specify the device. This works on both linux and windows -// In windows you have to spesify COM name, in linux it's ttyS0 or ttyACM0 similar name port name and in Mac it's tty.serial or similar serial name) +// First we must specify the device. COMXX works on both linux and windows +// because it's referred in linux as ttySXX or specify other Linux port name like ttyACM0. +// With mac you need to specify port name like tty.serial or similar. $serial->deviceSet("COM1"); // We can change the baud rate, parity, length, stop bits, flow control diff --git a/src/PhpSerial.php b/src/PhpSerial.php index 580af38..b3443fe 100644 --- a/src/PhpSerial.php +++ b/src/PhpSerial.php @@ -85,10 +85,22 @@ public function deviceSet($device) { if ($this->_dState !== SERIAL_DEVICE_OPENED) { if ($this->_os === "linux") { - if ($this->_exec("stty -F /dev/" . $device) === 0) { - $this->_device = "/dev/".$device; - $this->_dState = SERIAL_DEVICE_SET; - return true; + + if (substr($device, 0, 3) == "COM") { + preg_match("@^COM(\\d+):?$@i", $device, $matches) + $devName = "ttyS" . ($matches[1] - 1) === 0); + + if ($this->_exec("stty -F /dev/" . $devName ) { + $this->_device = "/dev/".$devName; + $this->_dState = SERIAL_DEVICE_SET; + return true; + } + else { + if ($this->_exec("stty -F /dev/" . $device) === 0) { + $this->_device = "/dev/".$device; + $this->_dState = SERIAL_DEVICE_SET; + return true; + } } } elseif ($this->_os === "osx") { if ($this->_exec("stty -f /dev/" . $device) === 0) { From 5fcf1e453107e8b3650f099c42c5f99abc2e18a9 Mon Sep 17 00:00:00 2001 From: Jeroi Date: Sun, 6 Apr 2014 14:52:02 +0300 Subject: [PATCH 5/6] Oops had few copy paste bugs. Resolved them. --- src/PhpSerial.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/PhpSerial.php b/src/PhpSerial.php index b3443fe..df30d03 100644 --- a/src/PhpSerial.php +++ b/src/PhpSerial.php @@ -88,13 +88,14 @@ public function deviceSet($device) if (substr($device, 0, 3) == "COM") { preg_match("@^COM(\\d+):?$@i", $device, $matches) - $devName = "ttyS" . ($matches[1] - 1) === 0); + $devName = "ttyS" . ($matches[1] - 1); - if ($this->_exec("stty -F /dev/" . $devName ) { + if ($this->_exec("stty -F /dev/" . $devName === 0) { $this->_device = "/dev/".$devName; $this->_dState = SERIAL_DEVICE_SET; return true; } + } else { if ($this->_exec("stty -F /dev/" . $device) === 0) { $this->_device = "/dev/".$device; From 27caf8578c7c17dbdbe632ce57c025efde284f9f Mon Sep 17 00:00:00 2001 From: Jeroi Date: Mon, 7 Apr 2014 07:21:07 +0300 Subject: [PATCH 6/6] Small typo fix. --- src/PhpSerial.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpSerial.php b/src/PhpSerial.php index df30d03..9cd5ec1 100644 --- a/src/PhpSerial.php +++ b/src/PhpSerial.php @@ -90,7 +90,7 @@ public function deviceSet($device) preg_match("@^COM(\\d+):?$@i", $device, $matches) $devName = "ttyS" . ($matches[1] - 1); - if ($this->_exec("stty -F /dev/" . $devName === 0) { + if ($this->_exec("stty -F /dev/" . $devName) === 0) { $this->_device = "/dev/".$devName; $this->_dState = SERIAL_DEVICE_SET; return true;