Serial port communication with windows with php

I am working with an application that connects to a 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 works great with me. Now the call is every time I connect to a new USB port that COM is changing for my modem. Is there any other way to automatically detect a device using php in windows?

+4
source share
1 answer

You need to determine the COM port number of your USB device through an external command called PHP shell_exec ().

On Windows, you can try this little tool:

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

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

After calling this tool via shell_exec (), you need to analyze its output (RegExp) and find the exact COM port number based on the company / device name.

+2
source

Source: https://habr.com/ru/post/1483823/


All Articles