0

我创建了一个应用程序来使用 at 命令发送短信,但我遇到了问题。我在 COM7 和 COM8 上连接了 3 个 gsm 调制解调器。

我的代码工作正常,但我希望如果短信发送处于“错误”状态,则将当前端口(例如:COM7)软更改为 COM8 ...

我的代码:

public clsWorker(SMSapplication app, SerialPort serialPort, SerialPort serialPort2, String urlChamp)
        {
            // Récupération du port principal
            this.port = serialPort;

            // Récupération du nom des ports
            this.port_name1 = serialPort.PortName;

            if (serialPort2.PortName == "" || serialPort2.PortName == null)
                this.port_name2 = serialPort.PortName;
            else
                this.port_name2 = serialPort2.PortName;

            // Récupération de l'url de check
            this.url = urlChamp;

            // Récupération de l'instance de la fenetre
            this.clsobjAPP = app;

            // On initialise le contenu du label avec le port actuel
            clsobjAPP.updateCurrentPort(this.port.PortName);

            //MessageBox.Show("Changement de port: " + this.port_name1 + " et " + this.port_name2);
        }


        public void StartThread()
        {
            t = new Thread(new ThreadStart(ThreadLoop));
            //clsobjSMS.setPinCode(this.port, "1234");
            t.Start();
        }


        // I want to change the port name is they is an error
        public void ChangePort()
        {

            if (this.port.PortName == this.port_name1)
            {
                // This don't work... Do you know why ??
                this.port.PortName = this.port_name2;
                MessageBox.Show("Change the current port to: " + this.port.PortName);
                clsobjAPP.updateCurrentPort("coucou1");
            }
            else if (this.port.PortName == this.port_name2)
            {
                // This don't work... Do you know why ??
                this.port.PortName = this.port_name1;
                MessageBox.Show("Change the current port to: " + this.port.PortName);
                clsobjAPP.updateCurrentPort("coucou2");
            }
            else
            {
                clsobjAPP.updateCurrentPort("erreur");
                MessageBox.Show("Erreur !");
            }

        }


        public void ThreadTimer()
        {

            t = new Thread(new ThreadStart(ThreadTimer));
            compareDate = DateTime.Now.AddMinutes(2);
            systemDate = DateTime.Now;

            while(true)
            {


                if (this.compareDate < DateTime.Now)
                {
                    this.StopThread();
                    this.systemDate = DateTime.Now;
                    this.compareDate = DateTime.Now.AddMinutes(2);
                    //MessageBox.Show("Launche every 2 minutes");

                }
                else
                {
                    if (!t.IsAlive)
                    {
                        this.StartThread();

                    }

                }
            }


        }



        public void ThreadLoop()
        {

            while (Thread.CurrentThread.IsAlive)
            {

                // Initialisation des variables:
                string destinataire_mobile = "";
                string destinataire_message = "";
                string id = "";
                string sms_envoye_le = "";
                string hash_envoi = "";
                string expediteur_ip = "";
                string lcHtml = "";


                Thread.Sleep(10000);


                try
                {


                    string lcUrl = this.url;
                   // "http://texto-sms-gratuit.com/android/etat_sms.php?etat=nok";
                    // *** Establish the request
                    HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(lcUrl);

                    // *** Set properties
                    loHttp.Timeout = 6000;     // 10 secs
                    loHttp.UserAgent = "Serveur Texto-SMS-Gratuit.Com";

                    // *** Retrieve request info headers
                    HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();

                    Encoding enc = Encoding.GetEncoding(1252);  // Windows default Code Page

                    StreamReader loResponseStream = new StreamReader(loWebResponse.GetResponseStream(), enc);


                    lcHtml = loResponseStream.ReadToEnd();


                    if (lcHtml == "null" || lcHtml.Length < 10 || lcHtml == "")
                        continue;

                    if (lcHtml == "<meta http-equiv=\"Refresh\" content=\"0\">")
                        continue;

                    JArray ja = JArray.Parse(lcHtml);


                    // Data extract of Json
                    id = ja[0]["id"].ToString();

                    // Mise en attente du msg
                    WebClient myWebClient = new WebClient();
                    myWebClient.DownloadString("http://site.com");

                    destinataire_mobile = ja[0]["destinataire_mobile"].ToString();
                    destinataire_message = ja[0]["destinataire_message"].ToString();
                    sms_envoye_le = ja[0]["sms_envoye_le"].ToString();
                    hash_envoi = ja[0]["hash_envoi"].ToString();
                    expediteur_ip = ja[0]["expediteur_ip"].ToString();


                    loWebResponse.Close();
                    loResponseStream.Close();

                    if (destinataire_mobile != "" && destinataire_message != "" && id != "")
                    {
                        if (destinataire_message.Length >= 159)
                            destinataire_message = destinataire_message.Substring(0, 159);

                        if (destinataire_mobile.Length >= 159)
                            destinataire_mobile = destinataire_mobile.Substring(0, 12);


                        if (destinataire_mobile[3].ToString() == "6" || destinataire_mobile[3].ToString() == "7")
                        {
                            if (clsobjSMS.sendMsg(this.port, destinataire_mobile, destinataire_message))
                            {
                                // Message sent
                                myWebClient.DownloadString("http://site.com/");
                                clsobjAPP.updateCountSMS("countSMSok");
                            }
                            else
                            {
                               // Message sent ERROR !!
                                myWebClient.DownloadString("http://site.com");
                                clsobjAPP.updateCountSMS("countSMSnok");


                                // This method don't work, and I don't know why ...?
                                this.ChangePort();



                            }
                        }
                    }

                }
                catch (Exception ex)
                {
                    //(ex.Message);

                }


            }
        }
    }

非常非常非常感谢您的帮助!此致,

4

0 回答 0