Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on a messaging application
I have a gsm modem and device usb rs232
I want a private message to your phone from a form that I write
below is my code
but it don't work.
My phone does not receive any messages
C#
namespace WindowsFormsApplication1
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void Form5_Load(object sender, EventArgs e)
        {
            _serialPort = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
            //_serialPort.Open();
            _response = string.Empty;
            _serialPort.RtsEnable = true;
            _serialPort.DataReceived += SerialPortDataReceived;

        }
        private static SerialPort _serialPort;
        private string _response;
        void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (e.EventType == SerialData.Chars)
            {
                _response += _serialPort.ReadLine();
            }
        }
        public bool IsReady()
        {
            try
            {
                if (!_serialPort.IsOpen)
                    _serialPort.Open();

                _serialPort.Write("AT\r");
                Thread.Sleep(3000);
            }
            catch
            {
                return false;
            }

            return _response.Contains("OK");
        }
        public static bool SendSms(string phoneNumber, string message)
        {
            try
            {
                if (!_serialPort.IsOpen)
                    _serialPort.Open();

                // Send
                _serialPort.Write("AT+CMGF=1\r");
                _serialPort.Write("AT+CMGS=\"" + phoneNumber + "\"\r\n");
                _serialPort.Write(message + "\x1A");
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SendSms(textBox1 .Text , textBox2 .Text );
        }

    }
}

thanks a lot
Posted

1 solution

So use the debugger, and see exactly what is going on.
Start by checking outside your app: use a terminal program to connect to COM4, and manually send exactly what you are trying to transmit from your app.

Does it work? If not, then you need to look at hardware / the message strings.
When it does, use the debugger to see exactly what your app is sending, and what responses you are getting from the mobile.

We can't do that for you: we aren't able to access your phone from here!
But...that's some poor code: you never look to see if the modem is responding at all, and if it is what it is sending you back.

I'd be tempted to start with displaying the incoming data in a textbox so I could review it and see if there were any errors, at least in the beginning.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900