无法设置COM端口,请检查它是否正确

时间:2017-04-10 14:05:03

标签: php

我正在使用诺基亚USB设备和PHP脚本发送短信但收到错误 堆栈上有很多答案,但没有答案可以解决这个问题

 Fatal error:  Uncaught Exception: Unable to setup COM port, check it is correct in C:\xampp\htdocs\sms-prac\send-sms.php:103
    Stack trace:
    #0 C:\xampp\htdocs\sms-prac\send-sms.php(36): gsm_send_sms->init()
    #1 {main}
      thrown in C:\xampp\htdocs\sms-prac\send-sms.php on line 103 

我的gsm_send_sms类代码

    //Send SMS via serial SMS modem
    class gsm_send_sms {

        public $port = 'COM1';
        public $baud = 115200;

        public $debug = false;

        private $fp;
        private $buffer;

        //Setup COM port
        public function 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}\"");
            }

            $this->debugmsg("Port opened");
            $this->debugmsg("Checking for responce from modem");

            //Check modem connected
            fputs($this->fp, "AT\r");

            //Wait for ok
            $status = $this->wait_reply("OK\r\n", 5);

            if (!$status) {
                throw new Exception('Did not receive responce from modem');
            }

            $this->debugmsg('Modem connected');

            //Set modem to SMS text mode
            $this->debugmsg('Setting text mode');
            fputs($this->fp, "AT+CMGF=1\r");

            $status = $this->wait_reply("OK\r\n", 5);

            if (!$status) {
                throw new Exception('Unable to set text mode');
            }

            $this->debugmsg('Text mode set');

        }

        //Wait for reply from modem
        private function wait_reply($expected_result, $timeout) {

            $this->debugmsg("Waiting {$timeout} seconds for expected result");

            //Clear buffer
            $this->buffer = '';

            //Set timeout
            $timeoutat = time() + $timeout;

            //Loop until timeout reached (or expected result found)
            do {

                $this->debugmsg('Now: ' . time() . ", Timeout at: {$timeoutat}");

                $buffer = fread($this->fp, 1024);
                $this->buffer .= $buffer;

                usleep(200000);//0.2 sec

                $this->debugmsg("Received: {$buffer}");

                //Check if received expected responce
                if (preg_match('/'.preg_quote($expected_result, '/').'$/', $this->buffer)) {
                    $this->debugmsg('Found match');
                    return true;
                    //break;
                } else if (preg_match('/\+CMS ERROR\:\ \d{1,3}\r\n$/', $this->buffer)) {
                    return false;
                }

            } while ($timeoutat > time());

            $this->debugmsg('Timed out');

            return false;

        }

        //Print debug messages
        private function debugmsg($message) {

            if ($this->debug == true) {
                $message = preg_replace("%[^\040-\176\n\t]%", '', $message);
                echo $message . "\n";
            }

        }

        //Close port
        public function close() {

            $this->debugmsg('Closing port');

            fclose($this->fp);

        }

        //Send message
        public function send($tel, $message) {

            //Filter tel
            $tel = preg_replace("%[^0-9\+]%", '', $tel);

            //Filter message text
            $message = preg_replace("%[^\040-\176\r\n\t]%", '', $message);

            $this->debugmsg("Sending message \"{$message}\" to \"{$tel}\"");

            //Start sending of message
            fputs($this->fp, "AT+CMGS=\"{$tel}\"\r");

            //Wait for confirmation
            $status = $this->wait_reply("\r\n> ", 5);

            if (!$status) {
                //throw new Exception('Did not receive confirmation from modem');
                $this->debugmsg('Did not receive confirmation from modem');
                return false;
            }

            //Send message text
            fputs($this->fp, $message);

            //Send message finished indicator
            fputs($this->fp, chr(26));

            //Wait for confirmation
            $status = $this->wait_reply("OK\r\n", 180);

            if (!$status) {
                //throw new Exception('Did not receive confirmation of messgage sent');
                $this->debugmsg('Did not receive confirmation of messgage sent');
                return false;
            }

            $this->debugmsg("Message sent");

            return true;

        }

    }

我用来发送短信的PHP代码

 <?php

    //SMS via GSM Modem - A PHP class to send SMS messages via a GSM modem attached to the computers serial port.

    //Windows only (tested on XP with PHP 5.2.6)
    //Tested with the EZ863 (Telit GE863) GSM modem
    //Requires that PHP has permission to access "COM" system device, and system "mode" command
    ini_set('max_execution_time', 300);
    error_reporting(E_ALL);

    //Example




    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "MembersManagmentSystem";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "SELECT Member.memberId, Member.memberContactMobileNumber, SMSRecipient.smsRecipientReceiverId, SMSRecipient.smsInfoId, SMSInfo.smsInfoText, SMSInfo.`smsInfoSaveDateTime` FROM Member, SMSRecipient,SMSInfo WHERE SMSRecipient.`smsRecipientReceiverId` = Member.`memberId` AND SMSRecipient.`smsInfoId`= SMSInfo.`smsInfoId` AND  SMSRecipient.smsRecipientSentDateTime IS NULL AND Member.`memberContactMobileNumber` IS NOT NULL AND SMSInfo.`smsInfoSaveDateTime` > DATE_SUB(NOW(), INTERVAL 1 DAY)";
    $result = $conn->query($sql);
        echo "<pre>";

    $gsm_send_sms = new gsm_send_sms();
    $gsm_send_sms->debug = false;
    $gsm_send_sms->port = 'COM3';
    $gsm_send_sms->baud = 115200;
    $gsm_send_sms->init();



    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
        print_r($row);

            $status = $gsm_send_sms->send($row['memberContactMobileNumber'], $row['smsInfoText']);


            if ($status) {

                $sql = "UPDATE SMSRecipient SET smsRecipientSentDateTime = now() WHERE smsInfoId = ".$row['smsInfoId']." and smsRecipientReceiverId = ".$row['memberId'];

                if ($conn->query($sql) === TRUE) {
                   echo "Message sent\n";
                }

            } else {
                echo "Message not sent\n";
            }


        }



        //exit();
    } else {
        echo "0 results";
    }







    $gsm_send_sms->close();




    $conn->close();


    ?>

更改$ gsm_send_sms-&gt; debug = false;为了真实,得到

    Array
(
    [0] => 
    [1] => Status for device COM3:
    [2] => -----------------------
    [3] =>     Baud:            115200
    [4] =>     Parity:          None
    [5] =>     Data Bits:       8
    [6] =>     Stop Bits:       1
    [7] =>     Timeout:         OFF
    [8] =>     XON/XOFF:        OFF
    [9] =>     CTS handshaking: OFF
    [10] =>     DSR handshaking: OFF
    [11] =>     DSR sensitivity: OFF
    [12] =>     DTR circuit:     ON
    [13] =>     RTS circuit:     ON
    [14] => 
)

在上面的代码调试之后,我发现当我第一次连接我的设备并运行它时php执行此行就像无限循环一样,在此行之前所有debugmsg和其他代码运行良好请帮助我

$buffer = fread($this->fp, 8192);

0 个答案:

没有答案