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:
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
{
}
}