Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developed a application in C# using Gsm Modem,i read data from comport using Port DataReceived Event and check what data received in port,according to that data i saved that data in database.When i used break point data is saved in database but without break point data is not saved.
code For DataReceived:

C#
public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {        
        try
        {            
            if (e.EventType == SerialData.Chars)
            {
            //cmdValue = "";
            SerialPort sp = (SerialPort)sender;
            //s = sp.ReadExisting();            
            int nBytesToRead = sp.BytesToRead;
            char[] receivedData = new char[nBytesToRead];
            sp.Read(receivedData, 0, nBytesToRead);
            for (int i = 0;i<nbytestoread mode="hold" />            {
                s += receivedData[i];
                cmdValue += receivedData[i];
            }
            if (s != "")
            {
                reciveValue=s;
                FunSaveValue(reciveValue);
                if (s != "\r" && s!="\r\n+C")
                {
                    s = "";
                }
            }
                receiveNow.Set();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }


Check Received Data:
C#
public void FunSaveValue(string reciveValue)
    {
        //System.Threading.Thread.Sleep(1000);         
        if (reciveValue.Contains("\r\nRING\r\n"))
        {           
          ShortMessageCollection messages = new ShortMessageCollection();                      
          var m = Regex.Match(reciveValue, "RING[^\\+]*\\+CLIP:\\s*\"(?<phone>[^\"]*)\",(\\d+),\"([^\"]*)\",(\\w*),\"(\\w*)\",(\\w*)");          
            while (m.Success)
            {
                ShortMessage msg = new ShortMessage();               
                msg.Index = m.Groups[1].Value;
                msg.Status = m.Groups[2].Value;
                msg.Sender = m.Groups[3].Value;
                msg.Alphabet = m.Groups[4].Value;
                msg.Sent = m.Groups[5].Value;
                msg.Message = m.Groups[6].Value;              
                messages.Add(msg);
                m = m.NextMatch();
                string recievedData = ExecCommand(port, "AT+CHLD=0", 300, "No phone connected");
            }         
        }
        else if (reciveValue.Contains("\r\nNO CARRIER\r\n"))
        {
        }
        if (reciveValue.EndsWith("\r\nOK\r\n") || reciveValue.EndsWith("\r\n> ") || reciveValue.EndsWith("\r\nERROR\r\n"))
        {            
        }

        if (reciveValue.Contains("\r\n+CMT:"))
        {
            ShortMessageCollection messages = new ShortMessageCollection();
            messages = ParseReadMsg(reciveValue);
            foreach (ShortMessage msg in messages)
            {             
                string[] words = msg.Message.Split(' ');
                if (words[0].ToString() == "REL")
                {
                    string[] msgSent = msg.Sent.Split('+');
                    int chkSender = 0;
                    int msgIndex = Convert.ToInt32(msg.Index);
                    chkSender = FunChkSender(msg.Sender);
                    if (chkSender == 1)
                    {
                        if (words[2].ToString() == "SYSTEM")
                        {
                            string msgBdy = "";
                            string[] pillarId = { };
                            for (int i = 2; words[i] != null; i++)
                            {
                                if (words[i].ToString() != "PILLAR")
                                    msgBdy = string.Concat(msgBdy, words[i].ToString(), " ");
                                else
                                {
                                    pillarId = words[i + 1].Split(':');
                                    break;
                                }
                            }
                            str = "select MessageBody from SmsTransaction where SmsDatetIme='" + msgSent[0].ToString() + "' AND Pillerid='" + pillarId[1].ToString() + "'";
                            int recExist = dc.Record_Exist_Confirm(str);
                            if (recExist == 0)
                            {
                                str = "insert into SmsTransaction(SmsDatetIme,Pillerid,Status,MessageBody)values('" + msgSent[0].ToString() + "','" + pillarId[1].ToString() + "','" + string.Concat(words[4].ToString(), " ", words[5].ToString()) + "','" + msgBdy.ToString() + "')";
                                int t = dc.insertupdate(str);                               
                            }
                        }
                        else
                        {
                            string[] pillerId = words[9].Split(':');
                            string msgBody = string.Concat(words[2].ToString(), " ", words[3].ToString(), " ", words[4].ToString(), " ", words[5].ToString());
                            str = "select MessageBody from SmsTransaction where SmsDatetIme='" + msgSent[0].ToString() + "' AND Pillerid='" + pillerId[1].ToString() + "'";
                            int recExist1 = dc.Record_Exist_Confirm(str);
                            if (recExist1 == 0)
                            {
                                str = "insert into SmsTransaction(SmsDatetIme,OperatorId,Pillerid,Status,MessageBody)values('" + msgSent[0].ToString() + "','" + words[7].ToString() + "','" + pillerId[1].ToString() + "','" + words[3].ToString() + "','" + msgBody.ToString() + "')";
                                int t = dc.insertupdate(str);                                
                            }
                        }
                    }
                }
            }           
        }
         if (reciveValue.ToString() != "")
        {
            ShortMessageCollection messages = new ShortMessageCollection();           
            Regex r = new Regex(@"\r\n\+CDS: (\d+),(\d+),""(.+)"",(\d+),""(.+)"",""(.+)"",(\d+)");
            // string[] words = s.Split(',');
            Match m = r.Match(reciveValue);
            while (m.Success)
            {
                ShortMessage msg = new ShortMessage();                
                msg.Index = m.Groups[1].Value;
                msg.Status = m.Groups[2].Value;
                msg.Sender = m.Groups[3].Value;
                msg.Alphabet = m.Groups[4].Value;
                msg.Sent = m.Groups[5].Value;
                msg.Message = m.Groups[6].Value;
                string[] delDate = msg.Message.Split('+');               
                str = "select MsgNo from MsgSentReport where MobileNo='" + msg.Sender.ToString() + "' AND MsgNo='" + msg.Status.ToString() + "' AND MsgStatus='Delivered'";
                int recExist = dc.Record_Exist_Confirm(str);
                if (recExist == 0)
                {
                    str = "insert into MsgSentReport(MobileNo,MsgNo,MsgDateTime,MsgStatus)values('" + msg.Sender.ToString() + "','" + msg.Status.ToString() + "','"+(delDate[0].ToString())+"','Delivered')";
                    int t = dc.insertupdate(str);
                }
                messages.Add(msg);
                m = m.NextMatch();
            }
        }       
    }</phone>
Posted
Updated 3-Dec-13 17:46pm
v4
Comments
Aravindba 3-Dec-13 23:55pm    
it show any error ? can u use try catch in FunSaveValue and check ?
i think data not receive fastly,so can u use sleep for some secs or mins after receive data and then try to insert.
can u use sleep(3000 or 5000) mode before if (s != "") condition in port_DataReceived.
NABIN SEN 4-Dec-13 1:44am    
Thank .It Solved My Problem.
Aravindba 4-Dec-13 2:28am    
can u reply what u did ?
thatraja 4-Dec-13 5:29am    
Share the solution which could help others in future
Sadique KT 9-Dec-13 6:50am    
Is your problem is solved by giving Thread.Sleep(delay);
Any how write your solution and mark it as solved...

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