Skip to content

Fixed linux DeviceSetup that does not handle any other than ttySXX and made changes how you define port names instead of addresses in DeviceSetup for all platforms. #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 22 additions & 15 deletions src/PhpSerial.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand Down