gpt4 book ai didi

windows - 使用php在windows上通信串口

转载 作者:可可西里 更新时间:2023-11-01 13:26:52 26 4
gpt4 key购买 nike

我正在使用一个应用程序,该应用程序使用 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);
?>

短信.php:

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

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

最佳答案

您需要通过 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 端口号。

关于windows - 使用php在windows上通信串口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16860605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com