使用php在Windows上通信串行端口 [英] Communicating serial port on windows with php

查看:144
本文介绍了使用php在Windows上通信串行端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用通过php中的COM端口连接到华为3G调制解调器的应用程序.这是我的代码:

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;
        }
    }
?>

这对我来说很好.现在的挑战是,每次我连接到新的USB端口时,COM都会为我的调制解调器进行更改.还有其他方法可以在Windows中使用php自动检测设备吗?

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 ?

推荐答案

您需要通过称为低谷PHP的shell_exec()的外部命令来确定USB设备的COM端口号.

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

对于Windows,您可以尝试使用此小工具:

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/

通过shell_exec()调用此工具后,需要分析其输出(RegExp),然后根据公司/设备名称查找确切的COM端口号.

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.

这篇关于使用php在Windows上通信串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆