0

我正在尝试检查一些在 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());
        }
    }
}

我得到这个输出:

在此处输入图像描述

为什么有时buff为空,稍后显示?

4

2 回答 2

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);
        }
    }
} 
于 2012-06-10T10:56:33.847 回答
0

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

于 2012-06-10T10:48:29.093 回答