I want to read the messages from GSM modem in a c# application. I have written the following code and used background worker for implementing Thread.sleep() on a separate thread. But the point at which i use port.ReadExisting(), nothing is being read from the port. Am i using a wrong way of handling the background worker?
private void btn_Read_Click(object sender, EventArgs e)
{
lvwMessages.Items.Clear();
status_other.Visible = true;
status_other.Text = "Loading messages...";
if (read_all.Checked)
{
port.WriteLine("AT+CMGL=\"ALL\"");
}
else if (read_unread.Checked)
{
port.WriteLine("AT+CMGL=\"REC UNREAD\"");
}
port.DiscardOutBuffer();
port.DiscardInBuffer();
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Thread.Sleep(5000);
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
string res = port.ReadExisting();// here no data is fetched into res
//rest of the code