Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Giving PHP permission to access COM port

Tags:

php

3g

modem

I'm creating a php script that connects to a 3G modem connected via serial connection on COM5.

I'm getting the following error and I believe it is because php does not have r/w access to COM5:

Warning: fopen(COM5:) [function.fopen]: failed to open stream: No such file or directory in C:\xampp\htdocs\SMStest\test2.php on line 9

// mode com1: BAUD=9600 PARITY=N data=8 stop=1 xon=off
$fp = fopen ("COM5:", "w+");
if (!$fp) {
    echo "Uh-oh. Port not opened.";
} else {
    $e = chr(27);
    $string  = $e . "A" . $e . "H300";
    $string .= $e . "V100" . $e . "XL1SATO";
    $string .= $e . "Q1" . $e . "Z";
    echo $string;
    fputs ($fp, $string );
    fclose ($fp);
}
like image 372
Nebula Avatar asked Nov 13 '22 10:11

Nebula


1 Answers

There are many ways to access COM ports on windows, alternatives to your method are opening it with the following paths:

\Device\00000123 (You can find the correct value in device manager, properties, details, physical device object name)

\\.\com5 (This is how I would open the port as a file if I was writing a program in C or something)

like image 185
Leigh Avatar answered Nov 16 '22 03:11

Leigh