Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 31st, 2007, 05:11 AM
Registered User
 
Join Date: Aug 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Issue Sending SMS using AT commands

Hy to all!

I have a problem sending SMS from C# code. (See the code below*)

When I'm using the same AT command in Windows HyperTerminal , the sms is sent successfully. The diference is that in HyperTerminal a screen appears to enter the SMS content, while in C# this cannot be done.

I think that the problem is related to the appearence of the screen used to write the SMS content.

Is there anyone who had a similar problem or has a solution?


* Full sample code

       // Create Serial port instance
        private SerialPort srpModemPort = new SerialPort();

        // Form load event
        private void ManagementScreen_Load(object sender, EventArgs e)
        {
            srpModemPort = new SerialPort();
            srpModemPort.PortName = "COM3";
            srpModemPort.BaudRate = 115200;
            srpModemPort.Parity = Parity.None;
            srpModemPort.DataBits = 8;
            srpModemPort.StopBits = StopBits.One;
            srpModemPort.Handshake = Handshake.RequestToSend;
            srpModemPort.DtrEnable = true;
            srpModemPort.RtsEnable = true;
            srpModemPort.NewLine = System.Environment.NewLine;

            srpModemPort.DataReceived += new SerialDataReceivedEventHandler(OnCom4Receive);
        }

        // Receive the answers from the modem
        private void OnCom4Receive(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                MessageBox.Show(srpModemPort.ReadExisting());
            }
            catch (Exception exc)
            {
                MessageBox.Show("Error receiving the answer. Reason:" + exc.Message);
            }
        }

        // The method that sends the at command
        private void sendSMS()
        {
            if (!srpModemPort.IsOpen)
                srpModemPort.Open();
            srpModemPort.WriteLine("AT+CMGS=" + txtPhone.Text + "\r" + " This is the message content followed by Ascii code for end-of-file marker or Ctrl+Z." + (char)(26));
        }

        // ********************************
        // Send SMS button clicked
        private void button1_Click(object sender, EventArgs e)
        {
            sendSMS();
        }
 
Old September 2nd, 2007, 08:13 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Hi Tavi,
Hyperterminal and C# both send code to the Serial Port.
I think you error might be in the syntax of the code you are sending.
Try enclosing command in C# as you do in Hyperterminal.
So AT+CMGS="0412321123"
becomes "AT+CMGS=\" + txtPhone.Text + "\"\r"
not "AT+CMGS=" + txtPhone.Text + "\r"
This enlcoses the phone number in quotes when it is sent to the serial port as it is in Hyperterminal.

Here are a few topics I was involved in some time ago on this subject which have some source code that works well.

http://p2p.wrox.com/topic.asp?TOPIC_...rchTerms=modem
http://p2p.wrox.com/topic.asp?TOPIC_...rchTerms=modem


======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================
 
Old September 3rd, 2007, 04:39 AM
Registered User
 
Join Date: Aug 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi rodmcleay,

First of all, thank you for your reply, it's so good to talk to another person about this issue.

Now related to my problem:
Before posting the problem I tryed to enclose the phone number with quotes but with the same results.

And as you suggested I tryed to do as in HyperTerminal.
I. in Hyperterminal
a. I typed the command AT+CMGS="xxxxxxxx" and pressed enter;
b. the modem responded with: ">"
c. I typed the content of the message and pressed CTRL+Z
d. message was sent successfully

II. In C#
a. I sent the command "AT+CMGS=\"xxxxxxxx\"\r" and pressed enter;
b. the modem responded with: "> ERROR"
c. I was still trying to send the content of the message followed by ASCII character corresponding to CTRL+Z
d. message not sent

As you can see an ERROR appears, and I really don't know why.

Note: instead of xxxxxxxx I have used a real phone number :)

I'm thinking to search for an API that allows to send sms from c# code. Also, if you have any suggestions in this direction....

But I would preffer to fix my code :)

I will try to use the code from the second link you sent me, but in the meantime if you have any idea related to my problem, please tell me.

Full sample code attached: (The method waitForResponse() will use some threads, but for tests is working)

    public String sMessageReceived = "";
        // Create Serial port instance
        private SerialPort srpModemPort = new SerialPort();

        // Form load event
        private void ManagementScreen_Load(object sender, EventArgs e)
        {
            srpModemPort = new SerialPort();
            srpModemPort.PortName = "COM2";
            srpModemPort.BaudRate = 115200;
            srpModemPort.Parity = Parity.None;
            srpModemPort.DataBits = 8;
            srpModemPort.StopBits = StopBits.One;
            srpModemPort.Handshake = Handshake.RequestToSend;
            srpModemPort.DtrEnable = true;
            srpModemPort.RtsEnable = true;
            srpModemPort.NewLine = System.Environment.NewLine;

            srpModemPort.DataReceived += new SerialDataReceivedEventHandler(OnCom4Receive);
        }

        // Receive the answers from the modem
        private void OnCom4Receive(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                 sMessageReceived = srpModemPort.ReadExisting();
            }
            catch (Exception exc)
            {
                MessageBox.Show("Error receiving the answer. Reason:" + exc.Message);
            }
        }

        // The method that sends the at command
        private void sendSMS()
        {
            if (!srpModemPort.IsOpen)
            {
                srpModemPort.Close();
                srpModemPort.Open();
            }

            sMessageReceived = null;
            DisplayInfo("Sending command: AT");
            srpModemPort.WriteLine("AT\r");
            waitForResponse();


            DisplayInfo("Sending command: AT+CMGS=\"xxxxxxxx\"");
            sMessageReceived = null;
            srpModemPort.WriteLine("AT+CMGS=\"xxxxxxxx\"\r");
            waitForResponse();

            if (sMessageReceived.Contains(">"))
            {
                DisplayInfo("Sending text body...");
                sMessageReceived = null;
                String sMessageContent = "> This is the message content followed by Ascii code for end-of-file marker or Ctrl+Z." + (char)(26);
                srpModemPort.WriteLine(sMessageContent);
                waitForResponse();
            }
            else
            {
                DisplayInfo("It seems like it is an error. Stop sending the SM.");
            }
        }

        private void waitForResponse()
        {
            int nCount = 0;

            DisplayInfo("Message receiving");
            while (nCount < 1000000000)
            {
                if (sMessageReceived != null)
                {
                    if (sMessageReceived.Length > 3 || sMessageReceived.StartsWith(">"))
                    {
                        DisplayInfo("At iteration: " + nCount + " Message received:" + sMessageReceived + "");
                        break;
                    }
                }
                nCount++;
            }

            if (nCount == 1000000000)
                DisplayInfo("At iteration: " + nCount + " Message still not received");
        }

        private void DisplayInfo(String sMessage)
        {
            txtInfo.Text += sMessage + "\r\n";
        }

        // Send SMS button clicked
        private void button1_Click(object sender, EventArgs e)
        {
            sendSMS();
        }
 
Old September 3rd, 2007, 07:50 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Hi Tavi,
Unfortunately I cant debug your code now, but it does look pretty close to the mark. I assume all your settings are the same for the port in code as they are in Hyperterminal such as Baude rate etc.
I seem to recall using the char value for carriage return instead of the escape sequence.
So try
serialPort.Write("AT+CMGS=\"" + mobileNumber + "\"" + (char)13);
and let me know how that goes.
Does a simple AT command return "OK"?
ie serialPort.Write("AT" + (char)13);



======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================
 
Old September 4th, 2007, 09:09 AM
Registered User
 
Join Date: Aug 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi rodmcleay,

I have used the code from the second link you sent me:
http://p2p.wrox.com/topic.asp?TOPIC_...rchTerms=modem

and I was able to successfully send SMS from C#.

Thank you very much!








Similar Threads
Thread Thread Starter Forum Replies Last Post
sms sending manojbhoyar ASP.NET 1.0 and 1.1 Basics 3 October 31st, 2008 11:58 PM
Binary Messaging ( sending RINGTONE) via SMS Gyanex C# 2005 0 January 12th, 2008 06:08 AM
Sending a string of commands to the command prompt Wire323 Access VBA 4 December 14th, 2006 06:14 AM
sending sms abcd_wxyz56 General .NET 0 November 17th, 2006 01:42 AM
Sending SMS in ASP software_developer_kk Classic ASP Basics 3 March 29th, 2005 11:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.