Tentative d'accéder à un port COM pour recevoir des données d'un périphérique externe : class PhPSserial
Bonjour, je cherche à communiquer depuis mon code PHP avec un port COM pour pouvoir recevoir des données d'un lecteur de cartes vitales.
Pour cela, j'ai trouvé sur Internet, la classe PhpSerial censée permettre d'accéder au port, mais je n'arrive pas à obtenir le résultat voulu.
J'utilise plusieurs fonctions de cette classe pour définir mon port et les paramètres de la communication :
Code:
1 2 3 4 5 6 7 8 9 10 11
|
$serial = new PhpSerial();
$serial->deviceSet("COM8");
$serial->confBaudRate(2400);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");
$serial->deviceOpen();
$serial->sendMessage("Hello !"); |
mais à l'exécution, je reçois plusieurs warning :
Warning: Specified serial port is not valid in C:\wamp64\www\node-project\PhpSerial.php on line 156
Warning: Unable to set baud rate: in C:\wamp64\www\node-project\PhpSerial.php on line 302
Warning: Unable to set parity : in C:\wamp64\www\node-project\PhpSerial.php on line 365
Warning: Unable to set character length : in C:\wamp64\www\node-project\PhpSerial.php on line 415
Warning: Unable to set stop bit length : in C:\wamp64\www\node-project\PhpSerial.php on line 476
Fatal error: Unable to set flow control : in C:\wamp64\www\node-project\PhpSerial.php on line 539
qui trouvent apparemment leur origine dans la fonction device_set où le device n'est pas défini correctement.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
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;
}
} elseif ($this->_os === "osx") {
if ($this->_exec("stty -f " . $device) === 0) {
$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 . " baud=9600 parity=n data=8 stop=1 xon=on")) === 0) {
echo "LA CHOSE";
$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);
return false;
}
} |
La fonction ne semble pas pouvoir rentrer dans ce if, à cause de la condition ajoutée après le and
Code:
1 2 3 4 5 6 7 8 9 10 11
|
if (preg_match("@^COM(\\d+):?$@i", $device, $matches) and $this->_exec(exec("mode " . $device . " baud=9600 parity=n data=8 stop=1 xon=on")) === 0) {
$this->_winDevice = "COM" . $matches[1];
$this->_device = "\\.com" . $matches[1];
$this->_dState = SERIAL_DEVICE_SET;
return true;
} |
J'ai aussi essayer d'utiliser la fonction fopen() indépendamment de la classe PhpSerial pour ouvrir une connection sans succès :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
exec('mode com6: baud=9600 data=8 stop=1 parity=n xon=on');
$fp = fopen("COM8", "r");
if (!$fp) {
echo "Uh-oh. Port not opened.";
}
else {
echo fgets($fp);
fclose($fp);
} |
Je reçois en retour un message : Warning: fopen(COM8): failed to open stream: Permission denied in C:\wamp64\www\node-project\patients.php on line 292.
Si quelqu'un a une idée sur ce qui pourrait clocher et que je ne vois pas, je vous en remercie d'avance.