5

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);
}
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
Nebula
  • 679
  • 2
  • 17
  • 37
  • Yes both with/without the colon no difference, same error. – Nebula Feb 10 '12 at 11:47
  • I'm clueless, but maybe this can help? http://www.phpclasses.org/package/3679-PHP-Communicate-with-a-serial-port.html – Svish Feb 10 '12 at 11:55
  • COM5 is a virtual rather than a physical port: does something like dio_open('COM5:', O_RDWR | O_NOCTTY | O_NONBLOCK); work instead of fopen? – Mark Baker Feb 10 '12 at 11:56
  • 1
    Possible duplicate: http://stackoverflow.com/questions/5920600/open-com-port-in-php – Leigh Feb 10 '12 at 11:56
  • Also, it says right there in the error message what's wrong. You're trying to open a file called "COM5:", and there is no such file in the directory your script is running from. – Svish Feb 10 '12 at 11:56
  • I tried dio_open but i get Fatal error: "Call to undefined function dio_open()" which is strange as it should be part of the standard library...@Svish COM5: is the virtual serial port that the 3g modem is connected to. It is getting picked up in device manager. – Nebula Feb 10 '12 at 12:03
  • Also, have you tried accessing the physical device object? I.e. \Device\00000123 (You can find it in device manager, properties, details, physical device object name). Not sure if this is even possible. – Leigh Feb 10 '12 at 12:03
  • Another thing: this code above is directly from php.net http://php.net/manual/en/function.fopen.php – Nebula Feb 10 '12 at 12:04
  • Last thing I can think of, did you try accessing it with "\\.\com5" ? – Leigh Feb 10 '12 at 12:12
  • Leigh thanks a million, that worked! You can post it as an answer and I'll accept it. – Nebula Feb 13 '12 at 06:57

1 Answers1

2

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)

Leigh
  • 12,859
  • 3
  • 39
  • 60