为什么缓冲区是空的?

时间:2012-06-10 10:42:03

标签: c# serial-communication

我正在尝试检查一些在GSM调制解调器上运行的AT命令的值。 当我试图检查命令的输出是否包含某些值时,我陷入了困境。

使用以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Timers;


namespace SerialTest
{
    public class Program
    {

        public static void Main(string[] args)
        {
            string buff="0";
            string ok = "OK";

            SerialPort p = new SerialPort("COM28");
            p.DataReceived += new SerialDataReceivedEventHandler(p_DataReceived);
            p.Open();
            string line = "1";
            p.Write("AT" + "\r");
            buff = p.ReadExisting();
            Console.WriteLine("buff: \"{0}\"\nok: \"{1}\"", buff, ok);
            p.Write("AT+CMGF=1"+ "\r" );
            buff = p.ReadExisting();
            Console.WriteLine("buff: \"{0}\"\nok: \"{1}\"", buff, ok);
            do
            {
                p.Write("AT+CMGL=\"REC UNREAD\" " + "\r");
                buff = p.ReadExisting(); 
                Console.WriteLine("buff: \"{0}\"\nok: \"{1}\"", buff, ok);
                /*if (buff.Contains(ok))
                    Console.WriteLine("Everything is OK");
                else
                    Console.WriteLine("NOK");*/
                line = Console.ReadLine();
            } while (line != "quit");
            p.Close();
        }

        public static void p_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            Console.WriteLine((sender as SerialPort).ReadExisting());
        }
    }
}

我得到了这个输出:

enter image description here

为什么有时buff是空的并且稍后显示?

2 个答案:

答案 0 :(得分:1)

ReadExisting()的来电是多余的。 您不应该在main方法中读取串口输出,让事件处理程序为您完成工作。 此外,您应该将事件处理程序中读取的内容附加到buff变量,以便在检查缓冲区时可以收到所有数据。

请改为尝试:

public class Program
{
    public static void Main(string[] args)
    {
        ModemReader r = new ModemReader();
        r.StartReading();
    }
    class ModemReader{
        string buff="";
        public void StartReading()
        {
            string ok = "OK";
            SerialPort p = new SerialPort("COM28");
            p.DataReceived += new SerialDataReceivedEventHandler(p_DataReceived);
            p.Open();
            string line = "1";
            p.Write("AT" + "\r");
            Console.WriteLine("buff: \"{0}\"\nok: \"{1}\"", buff, ok);
            p.Write("AT+CMGF=1"+ "\r" );
            Console.WriteLine("buff: \"{0}\"\nok: \"{1}\"", buff, ok);
            do
            {
                // Clear the buffer here
                buff = "";  

                p.Write("AT+CMGL=\"REC UNREAD\" " + "\r");
                Console.WriteLine("buff: \"{0}\"\nok: \"{1}\"", buff, ok);
                if (buff.Contains(ok))
                    Console.WriteLine("Everything is OK");
                else
                    Console.WriteLine("NOK");
                line = Console.ReadLine();
            } while (line != "quit");
            p.Close();
        }
        public void p_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string s = (sender as SerialPort).ReadExisting();
            buff += s;
            Console.WriteLine(s);
        }
    }
} 

答案 1 :(得分:0)

您只能使用一次端口数据。因为您在处理程序中使用它,所以当您尝试将其放入缓冲区时它不再存在。