diff --git a/Readme.md b/Readme.md index 82fbf93..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 (if -// your linux serial device is /dev/ttyS0 for COM1, etc) +// 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 1d253d5..9cd5ec1 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 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) + * -> 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 @@ -86,18 +85,26 @@ 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 (substr($device, 0, 3) == "COM") { + preg_match("@^COM(\\d+):?$@i", $device, $matches) + $devName = "ttyS" . ($matches[1] - 1); + + 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; + $this->_dState = SERIAL_DEVICE_SET; + 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;