6

I'm working with an application which connects to Huawei 3G Modem using COM ports in php. Here is my code:

<?php
include("sms.php");
$sms = new sms();
$device = "COM11";
exec("mode $device BAUD=9600 PARITY=n DATA=8 STOP=1 xon=off octs=off rts=on");
$comport = fopen($device, "r+b");
if ($comport === false){
    die("Failed opening com port<br/>");
}else{
    echo "Com Port Open<br/>";
}

//Set non-blocking mode for writing
//stream_set_blocking($comport, 0);
$sms->_blocking($comport,0);
$atcmd = "AT\r";
fputs($comport, $atcmd);

sleep(5); // Sleep for response from the modem

 // Set blocking mode for reading
$sms->_blocking($comport,1);
$res = fgets($comport, 4017);
if(trim($res) == "OK"){
    echo "Modem supports AT Commands<br/>";
}else{
    echo "Error response: ".$res;
}

fclose($comport);
?>

sms.php:

<?php
    class sms{
        function _blocking($device,$mode){
            stream_set_blocking($device, $mode);
            return true;
        }
    }
?>

This is working fine with me. Now challenge is every time i connect to the new usb port the COM was changing for my modem. Is there any other way to detect the device automatically with php in windows ?

Euro Micelli
  • 33,285
  • 8
  • 51
  • 70
Radhakrishna Rayidi
  • 510
  • 2
  • 9
  • 24
  • I have attached my usb modem at COM13. After changing that in your code I'm getting. Warning: fopen(COM13) [function.fopen]: failed to open stream: No such file or directory in C:\xampp\htdocs\sms\index.php on line 6 Failed opening com por – Ahmad Shahwaiz Nov 10 '13 at 15:14
  • Check weather the mode is is available in `COM13` – Radhakrishna Rayidi Nov 26 '13 at 08:06
  • The error is somehow gone but the message is not being sent. Exactly what is the command for sending a message to a particular number? Also can we mask the number ? – Ahmad Shahwaiz Nov 26 '13 at 12:18
  • @AhmadShahwaiz you can't mask the number. See this `http://developer.nokia.com/Community/Wiki/Using_AT_commands_to_send_and_read_SMS` you might know how to send the message to particular number. – Radhakrishna Rayidi Nov 29 '13 at 06:12

1 Answers1

3

You need to determinate COM port number of your USB device via some external command called trough PHP's shell_exec().

For Windows you can try this small tool:

http://todbot.com/blog/2012/03/02/listcomports-windows-command-line-tool-for-usb-to-serial/

https://github.com/todbot/usbSearch/

After you call this tool via shell_exec(), you need to parse it's output (RegExp) and look for exact COM port number based on company/device name.

mikikg
  • 1,488
  • 1
  • 11
  • 23