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:
public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
if (e.EventType == SerialData.Chars)
{
SerialPort sp = (SerialPort)sender;
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:
public void FunSaveValue(string reciveValue)
{
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+)");
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>