Exemple #1
0
        private void button2_Click(object sender, EventArgs e)
        {
            //SerialPort port = new SerialPort();
            //port = OpenPort(strPort);
            //Thread.Sleep(300);
            SerialPort port = connectPort.OpenPort(
                           "COM7",
                           Convert.ToInt32("115200"),
                           Convert.ToInt32("8"),
                           Convert.ToInt32("300"),
                           Convert.ToInt32("300")
                       );
            string p_strCommand = "AT+CMGL=\"ALL\"";
            string receive = "";

            // Set up the phone and read the messages
            ShortMessageCollection messages = null;
            //try
            //{
                #region Execute Command
                // Check connection
                receive += connectPort.ExecCommand(port, "AT", 300, "No phone connected");
                // Use message format "Text mode"
                receive += connectPort.ExecCommand(port, "AT+CMGF=1", 300, "Failed to set message format.");
                // Use character set "PCCP437"
                //receive += connectPort.ExecCommand(port, "AT+CSCS=\"PCCP437\"", 300, "Failed to set character set.");
                // Select SIM storage
                //receive += ExecCommand(port, "AT+CPMS=\"SM\"", 300, "Failed to select message storage.");
                //receive += connectPort.ExecCommand(port, "AT+CMGR=1", 300, "Failed to select message storage.");
                //Debug.WriteLine(receive);
                // Read the messages
                string input = connectPort.ExecCommand(port, p_strCommand, 1000, "Failed to read the messages.");
                #endregion

                #region Parse messages
                messages = connectPort.ParseMessages(input);
                Debug.WriteLine(messages.Count);
                Debug.WriteLine(input);USSD ussd = new USSD();
                foreach(ShortMessage msg in messages){
                    string m = msg.Message.IndexOf(" ") >= 0 ? msg.Message : ussd.decodeResponseUSSDToText(msg.Message);
                    Debug.WriteLine(m);
                }
                #endregion

            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}
            connectPort.ClosePort(port);
            //ClosePort(port);
            //if (messages != null)
            //    return messages;
            //else
            //    return null;
        }
Exemple #2
0
        private void button1_Click_1(object sender, EventArgs e)
        {
            SerialPort sPort = connectPort.OpenPort(
                           "COM3",
                           Convert.ToInt32("115200"),
                           Convert.ToInt32("8"),
                           Convert.ToInt32("300"),
                           Convert.ToInt32("300")
                       );
            if (sPort == null)
            {
                Debug.WriteLine("port_is_bussy");
            }
            else
            {
                string recievedData = connectPort.ExecCommand(sPort, "AT", 300, "No phone connected");

                if (recievedData.EndsWith("\r\nOK\r\n"))
                {
                    recievedData = connectPort.ExecCommand(sPort, "AT+CMGF=1", 300, "Failed to set message format.");
                    if (recievedData.EndsWith("\r\nOK\r\n"))
                    {
                        string strCommand = "AT+CUSD=1,\"*121#\",15";
                        connectPort.responseUSSD = "";
                        string result = connectPort.ExecCommand(sPort, strCommand, 300, "error");
                        //try
                        //{
                            if (result.EndsWith("\r\nOK\r\n"))
                            {
                                result = connectPort.ExecCommand(sPort, "", 5000, "Failed to read the messages.");
                                //result = result.Replace("OK", "").Replace(strCommand, "").Trim();
                                //response = ussd.cutStringIMEI(result);
                                //Debug.WriteLine(result);
                                string response = connectPort.responseUSSD;
                                response = response.Replace("OK", "").Replace(strCommand, "").Trim();
                                response = response.Substring(response.IndexOf("\"") + 1);
                                response = response.Substring(0, response.IndexOf("\""));
                                USSD ussd = new USSD();
                                response = ussd.decodeResponseUSSDToText(response);
                                MessageBox.Show(response);
                            }
                        //}
                        //catch
                        //{
                        //    MessageBox.Show(result);
                        //}
                    }
                    else
                    {
                        //response = "message format error";
                    }
                }
                connectPort.ClosePort(sPort);
            }
        }