Click here to Skip to main content
15,354,639 members
Please Sign up or sign in to vote.
2.14/5 (3 votes)
See more:
I am developing in C# appliction to read data from gsm modem using AT command.

I am trying to read data from Port using port.DataReceived event and check the return value but it does not read any value
I used break points to see what my string has read and to my surprise it was reading correctly . But when i directly run my application, it does not read anything?
DataReceived Code Is:
C#
public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
  {
      try
      {
          SerialPort sp = (SerialPort)sender;
          s = sp.ReadExisting();
          if (s != "")
          {
              reciveValue +=s;
              FunSaveValue(reciveValue);
          }
          if (e.EventType == SerialData.Chars)
          {
              receiveNow.Set();
          }
      }
      catch (Exception ex)
      {
          throw ex;
      }
  }


In FunSaveValue(reciveValue) function check read Value

public void FunSaveValue(string reciveValue)
{
//System.Threading.Thread.Sleep(1000);
if (reciveValue.Contains("\r\nRING\r\n"))
{

ShortMessageCollection messages = new ShortMessageCollection();
Regex r = new Regex(@"\+CLIP:""(.+)"",(\d+),""(.*)"",(.*),""(.*)"",(\d+)\r\n");


Match m = r.Match(reciveValue);
while (m.Success)
{
ShortMessage msg = new ShortMessage();
//msg.Index = int.Parse(m.Groups[1].Value);
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;
// MessageBox.Show("Message Deliverd To:" + msg.Sender.ToString());
messages.Add(msg);
m = m.NextMatch();
string recievedData = ExecCommand(port, "AT+CHLD=0", 300, "No phone connected");
}

//MessageBox.Show("Incoming Call....");
// incall_status.Visible = true;
}
else if (reciveValue.Contains("\r\nNO CARRIER\r\n"))
{
//MessageBox.Show("Call Disconnected");
//backgroundWorker1.RunWorkerAsync();
}
if (reciveValue.EndsWith("\r\nOK\r\n") || reciveValue.EndsWith("\r\n> ") || reciveValue.EndsWith("\r\nERROR\r\n"))
{
//MessageBox.Show("Hi");
}

if (reciveValue.Contains("\r\n+CMT:"))
{
ShortMessageCollection messages = new ShortMessageCollection();
messages = ParseReadMsg(reciveValue);
foreach (ShortMessage msg in messages)
{
// ListViewItem item = new ListViewItem(new string[] { msg.Index, msg.Sent, msg.Sender, msg.Message });
string[] words = msg.Message.Split(' ');
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);
if (t > 0)
{
// MessageBox.Show("Record Saved Successfully");
}
}
}
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 (t > 0)
{
// MessageBox.Show("Record Saved Successfully");
}
}
}
}
}
//MessageBox.Show(s.ToString());
}
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 = int.Parse(m.Groups[1].Value);
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;
//MessageBox.Show("Message Deliverd To:" + msg.Sender.ToString());
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,MsgStatus)values('" + msg.Sender.ToString() + "','" + msg.Status.ToString() + "','Delivered')";
int t = dc.insertupdate(str);
}
messages.Add(msg);
m = m.NextMatch();
}
}
else
{

}
}
Posted
Updated 27-Nov-13 22:13pm
v3
Comments
   
Not enough information. We have pretty poor access to your hard drive these days. Read about race condition and think in this direction.
—SA
OriginalGriff 28-Nov-13 4:05am
   
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
So show us the event handler code!
Use the "Improve question" widget to edit your question and provide better information.
agent_kruger 28-Nov-13 7:06am
   
is your main problem that when you use break point you get correct value but if you run directly it does not get the needed value?
NABIN SEN 28-Nov-13 7:10am
   
yes

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