【问题标题】:Communicating serial port on windows with php用php在windows上通信串口
【发布时间】:2013-05-31 15:17:04
【问题描述】:

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

<?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自动检测设备?

【问题讨论】:

  • 我已将我的 USB 调制解调器连接到 COM13。在你的代码中改变它之后,我得到了。警告:fopen(COM13) [function.fopen]: 无法打开流:第 6 行的 C:\xampp\htdocs\sms\index.php 中没有这样的文件或目录打开 com por
  • COM13查看天气模式是否可用
  • 错误以某种方式消失了,但没有发送消息。将消息发送到特定号码的确切命令是什么?我们也可以屏蔽这个数字吗?
  • @AhmadShahwaiz 你不能屏蔽这个数字。看到这个http://developer.nokia.com/Community/Wiki/Using_AT_commands_to_send_and_read_SMS你可能知道如何将消息发送到特定号码。

标签: windows serial-port gsm modem


【解决方案1】:

您需要通过一些称为 trough PHP 的 shell_exec() 的外部命令来确定 USB 设备的 COM 端口号。

对于 Windows,你可以试试这个小工具:

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 端口号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多