c#: sms appears to have been sent, but stuck in phone outbox

Posted by I__ on Stack Overflow See other posts from Stack Overflow or by I__
Published on 2010-06-16T21:25:01Z Indexed on 2010/06/16 23:02 UTC
Read the original article Hit count: 202

Filed under:

i wrote code to send an SMS using my gsm phone which is attached to the computer through com port. the code is below.

the problem is i do see that it is in the outbox of the phone and it actually appears to have been sent, but when i contact the recipient they say that i have not received the message.

i test the phone, and i create and send a message using only the phone and it works perfectly, however when i do this with my code, it APPEARS to have been sent, and i am getting all the correct AT COMMAND responses from the phone, but the message is actually NOT sent.

here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO.Ports;



namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        SerialPort serialPort1;
        int m_iTxtMsgState = 0;
        const int NUM_MESSAGE_STATES = 4;
        const string RESERVED_COM_1 = "COM1";
        const string RESERVED_COM_4 = "COM4";

        public Form1()
        {
            InitializeComponent();

            this.Closing += new CancelEventHandler(Form1_Closing);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            serialPort1 = new SerialPort(GetUSBComPort());

            if (serialPort1.IsOpen)
            {
                serialPort1.Close();
            }

            serialPort1.Open();

            //ThreadStart myThreadDelegate = new ThreadStart(ReceiveAndOutput);
            //Thread myThread = new Thread(myThreadDelegate);
            //myThread.Start();

            this.serialPort1.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
        }

        private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            serialPort1.Close();
        }

        private void SendLine(string sLine)
        {
            serialPort1.Write(sLine);
            sLine = sLine.Replace("\u001A", "");
            consoleOut.Text += sLine;
        }

        public void DoWork()
        {
            ProcessMessageState();
        }

        public void ProcessMessageState()
        {
            switch (m_iTxtMsgState)
            {
                case 0:
                    m_iTxtMsgState = 1;
                    SendLine("AT\r\n");  //NOTE: SendLine must be the last thing called in all of these!
                break;

                case 1:
                    m_iTxtMsgState = 2;
                    SendLine("AT+CMGF=1\r\n");

                break;

                case 2:
                    m_iTxtMsgState = 3;
                    SendLine("AT+CMGW=" + Convert.ToChar(34) + "+9737387467" + Convert.ToChar(34) + "\r\n");
                break;

                case 3:
                    m_iTxtMsgState = 4;
                    SendLine("A simple demo of SMS text messaging." + Convert.ToChar(26));
                break;

                case 4:
                    m_iTxtMsgState = 5;

                break;

                case 5:
                    m_iTxtMsgState = NUM_MESSAGE_STATES;
                break;
            }
        }

        private string GetStoredSMSID()
        {
            return null;
        }
        /* //i dont think this part does anything
        private void serialPort1_DataReceived_1(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string response = serialPort1.ReadLine();
            this.BeginInvoke(new MethodInvoker(() => textBox1.AppendText(response + "\r\n")));
        }
        */
        void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                Thread.Sleep(500);

                char[] msg;
                msg = new char[613];
                int iNumToRead = serialPort1.BytesToRead;

                serialPort1.Read(msg, 0, iNumToRead);

                string response = new string(msg);

                this.BeginInvoke(new MethodInvoker(() => textBox1.AppendText(response + "\r\n")));
                serialPort1.DiscardInBuffer();

                if (m_iTxtMsgState == 4)
                {
                    int pos_cmgw = response.IndexOf("+CMGW:");
                    string cmgw_num = response.Substring(pos_cmgw + 7, 4);
                    SendLine("AT+CMSS=" + cmgw_num + "\r\n");
                    //stop listening to messages received
                }

                if (m_iTxtMsgState < NUM_MESSAGE_STATES)
                {
                    ProcessMessageState();
                }
            }
            catch
            { }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_iTxtMsgState = 0;
            DoWork();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string[] sPorts = SerialPort.GetPortNames();
            foreach (string port in sPorts)
            {
                consoleOut.Text += port + "\r\n";
            }
        }

        private string GetUSBComPort()
        {
            string[] sPorts = SerialPort.GetPortNames();
            foreach (string port in sPorts)
            {
                if (port != RESERVED_COM_1
                    && port != RESERVED_COM_4)
                {
                    return port; 
                }
            }

            return null; 
        }
    }

© Stack Overflow or respective owner

Related posts about c#