我想在 ac# 应用程序中读取来自 GSM 调制解调器的消息。我编写了以下代码并使用后台工作人员在单独的线程上实现 Thread.sleep() 。但是在我使用 port.ReadExisting() 时,没有从端口读取任何内容。我是否使用了错误的方式来处理后台工作人员?
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