Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is code for sending msgs, but i want to recieve or read msgs from sim card. Sending msgs code is given below

C#
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.IO.Ports;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            loadPorts();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void loadPorts()
        {
            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                cboPorts.Items.Add(port);
            }
        }

    private void cboPorts_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        SmsClass sm = new SmsClass(cboPorts.Text);
        sm.Opens();
        sm.sendSms(txtPhone.Text, txtMessage.Text);
        sm.Closes();
        MessageBox.Show("Message Sent!");
    }

    private void AddMobNo_Click(object sender, EventArgs e)
    {
        ListBoxNumbers.Items.Add(tbGMobNo.Text);
        tbGMobNo.Text = "";
    }

    private void btnSendGrouped_Click(object sender, EventArgs e)
    {
        SmsClass sm = new SmsClass(cboPorts.Text);
        for (int a = 0; a <= ListBoxNumbers.Items.Count - 1; a++)
        {
            //SmsClass sm = new SmsClass(cboPorts.Text);
            //sm.Opens();
            //sm.sendSms(ListBoxNumbers.Items[a].ToString(), GtxtMessage.Text);
            //sm.Closes();
            //MessageBox.Show("Message Sent Successfully");

            sm.Opens();
            sm.sendSms(ListBoxNumbers.Items[a].ToString(), GtxtMessage.Text);
            sm.Closes();
            Thread.Sleep(10000);
            MessageBox.Show("Message Sent Successfully");

        }
        sm = null;
    }
  }

}



here is the SmsClass







C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO.Ports;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class SmsClass
    {
        SerialPort serialPort;
        public SmsClass(string comPort)
        {
            this.serialPort = new SerialPort();
            this.serialPort.PortName = comPort;
            this.serialPort.BaudRate = 9600;
            this.serialPort.Parity = Parity.None;
            this.serialPort.DataBits = 8;// Databits are the number of bits in the stream which can be 8 for binary transfer or 7 for text transfer
            this.serialPort.StopBits = StopBits.One;
            this.serialPort.Handshake = Handshake.RequestToSend;// Handshake is a protocol between the sender and receiver for determining the data transfer correctness
            this.serialPort.DtrEnable = true;//Gets or sets a value that enables the Data Terminal Ready
            this.serialPort.RtsEnable = true;//Gets or sets a value indicating whether the Request to Send
            this.serialPort.NewLine = System.Environment.NewLine;
        }
        public bool sendSms(string cellNo, string sms)
        {
            string messages = null;
            messages = sms;
            if (this.serialPort.IsOpen == true)
            {
                try
                {
                    this.serialPort.WriteLine("AT" + (char)(13));
                    Thread.Sleep(4);
                    this.serialPort.WriteLine("AT+CMGF=1" + (char)(13));
                    Thread.Sleep(5);
                    this.serialPort.WriteLine("AT+CMGS=\"" + cellNo + "\"");
                    Thread.Sleep(10);
                    this.serialPort.WriteLine(">" + messages + (char)(26));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Source);
                }
                return true;
            }
            else
                return false;
        }

        public void Opens()
        {
            if (this.serialPort.IsOpen == false)
            {
                this.serialPort.Open();
            }
        }
        public void Closes()
        {
            if (this.serialPort.IsOpen == true)
            {
                this.serialPort.Close();
            }
        }
    }

}

[Edit]Code block added[/Edit]
Posted
Updated 13-Jan-13 5:35am
v2
Comments
Member 9411249 13-Jan-13 13:53pm    
?
Sandeep Mewara 14-Jan-13 2:56am    
Any effort for 'receiving message'? Where did you got stuck?
Member 9411249 14-Jan-13 3:01am    
yes i will try many codes but no one use full. i also stuck on send multi msgs but its works for me with some problems...
Member 9411249 14-Jan-13 3:02am    
i also don't understand 1st step to recieve msgs...

1 solution

Point to be noted that, you can send message via any phone that supports AT command. You can send it through a phone which is connected with PC via bluetooth, cable, anything.

Receiving is not supported by most of the phones. You need a GSM modem ( I repeat a GSM Modem device) for doing this.

If you want to do it with phones, the only cable that i successfully tested receiving is CA-42 cable and few models of Nokia.

I have done some extensive works with GSM modem including, integrating it with CRM solutions. so you can take my words on this.
 
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