1
votes

I am working on bulk sms application where I need to connect to the GSM modem to send and receive sms. As a part of the script I am using dio_open function to connect to COM port to send AT commands. But dio_open function throws warning and it fails to open com port.

Warning: dio_open() [<a href='function.dio-open'>function.dio-open</a>]: cannot open file COM24: with flags 0 and permissions 0: Invalid argument in C:\wamp\www\serial_port\exec.php on line 8

I am using PHP 5.3 with wamp installed on windows system. Below I mentioned PHP code I am using.

<?php
$output = array();
exec('mode COM24: baud=230400 data=8 stop=1 parity=n',$output);
// execute 'help mode' in command line of Windows for help

print_r($output);

$fd = dio_open('COM24:', O_RDONLY);
if(!$fd)
{
echo "Connection not working";
}
else
{
echo "Connection working";
}

?>

I think exec commands working fine as its returning output in array.

Array ( [0] => [1] => Status for device COM24: [2] => ------------------------ [3] => Baud: 230400 [4] => Parity: None [5] => Data Bits: 8 [6] => Stop Bits: 1 [7] => Timeout: OFF [8] => XON/XOFF: OFF [9] => CTS handshaking: OFF [10] => DSR handshaking: OFF [11] => DSR sensitivity: OFF [12] => DTR circuit: ON [13] => RTS circuit: ON [14] => )

But not sure why dio_open is giving warning and why it fails to open com port. I googled for this issue but didn't find anything helpful.please advise if I am missing anything.

1

1 Answers

0
votes

Very late answer, but I've been googling this for hours and would like to help any future adventurers.

dio_open will not work with relative paths in Windows. Take this snippet for instance.

// Open file for write (create if doesn't exist)
if (!$fp = dio_open($path, O_WRONLY | O_CREAT, 0644)) {
    error('Unable to open file for writing: ' . $path);
}

In UNIX, this should work fine. In Windows, it will complain that you can't create the file, even though I've specified O_CREAT.

You need to specify the full path. Here's how I did it in my example.

realpath(dirname($path))."/".basename($path)

This will transform path\file.txt to C:\path\file.txt and then dio_open will work as expected.

I know this isn't directly related to OP since he's asking about COM ports. If you're looking for that, see this post:

"PHP can not read from serial ports under Windows. This is not the case on a Linux system, on which there is no problem to reading from COM ports with the common PHP filesystem functions."

PHP COM port connection with DIO