致命错误:未捕获异常';异常';带有消息';无法设置COM端口,请检查它是否正确';


Fatal error: Uncaught exception 'Exception' with message 'Unable to setup COM port, check it is correct'

致命错误:未捕获异常"exception",在C:''examplep''htdocs''jsms''sms.php中显示消息"无法设置COM端口,请检查它是否正确"。堆栈跟踪:#0 C:''examplep''tdocs''jsms ''sms.php(17):gsm_send_sms->init()#1{main}在第61行的C:''examplep''ltdocs''jsms''sms.php中抛出公共函数init(){

    $this->debugmsg("Setting up port: '"{$this->port} @ '"{$this->baud}'" baud");
    exec("MODE {$this->port}: BAUD={$this->baud} PARITY=N DATA=8 STOP=1", $output, $retval);
    if ($retval != 0) 
    {
        throw new Exception('Unable to setup COM port, check it is correct');
    }
    $this->debugmsg(implode("'n", $output));
    $this->debugmsg("Opening port");
    //Open COM port
    $this->fp = fopen($this->port . ':', 'r+');
    //Check port opened
    if (!$this->fp) {
        throw new Exception("Unable to open port '"{$this->port}'"");
    }

您没有捕获抛出的异常:

if ($retval != 0) 
{
    throw new Exception('Unable to setup COM port, check it is correct');
}

如果

exec("MODE {$this->port}: BAUD={$this->baud} PARITY=N DATA=8 STOP=1", $output, $retval);

返回一个错误int(anything!=0)。

用途:

try {
    exec("MODE {$this->port}: BAUD={$this->baud} PARITY=N DATA=8 STOP=1", $output, $retval);
    if ($retval != 0) 
    {
        throw new Exception('Unable to setup COM port, check it is correct');
    }
} catch (Exception $e) {
    // do whatever you want to handle the error
}