範例:
$port = 'com3'; //modem port
//初始化(windows適用)
exec("mode $port baud=5600 data=8 parity=n stop=1 to=off xon=off octs=on odsr=off idsr=off dtr=on rts=hs");
$fd = fopen($port, "r+");
/**
* 執行AT指令
* Modem AT Command
*/
fwrite($fd, "ATZ\r");
/**
* 接收Modem回傳
*/
stream_set_timeout($fd, 10);
stream_set_blocking($fd, 1);
$info = stream_get_meta_data($fd);
//沒有timed out
if (!$info['timed_out']){
while(!feof($fd)){
$response .= stream_get_contents($fd, 1);
//響應訊息包含 OK、ERROR、RING、NO DIALTONE 字樣,表示響應結束,跳出while
if (preg_match("/OK\\r\\n|ERROR\\r\\n|RING\\r\\n|NO DIALTONE\\r\\n/i", $response)) break;
}
print_r(explode("\r\n", $response));
}