这几天正在写一个短信程序.
测试代码如下:            try
            {
                int i = 0;
                this.serialPort1.PortName = this.cboPort.Text;
                this.serialPort1.Open();
                this.serialPort1.WriteLine("at+CGMM");  //获取设备支持的频率                Thread.Sleep(500);
                i = this.serialPort1.BytesToRead;                this.serialPort1.WriteLine("at+cgmi"); //获取短信设备制造商信息
                // byte[] buffer = new byte[1];                //int i = this.serialPort1.BytesToWrite;
                Thread.Sleep(500);
                i = this.serialPort1.BytesToRead;                if (i > 0)
                    this.textBox1.Text = this.serialPort1.ReadLine();                MessageBox.Show("OK");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                this.serialPort1.Close();
            }程序执行后,向串口写数据没有问题,可是读取AT指令的返回数据却出现问题.
数据读取缓冲区一直是空的,好像短信设备根本没有返回数据.请各位帮忙想想.

解决方案 »

  1.   

    有可能真的没有返回,你可以使用串口精灵监视看看另外, i = this.serialPort1.BytesToRead; 是否会有问题,去掉试试看,因为如果有数据返回的话,就要查看BytesToRead是否会清除数据缓存。
      

  2.   

    干吗不把读信息写在Data_Arrived事件里啊?这样就算这次读到了,也不能保证每次都能读对啊.
      

  3.   

    public System.IO.Ports.SerialPort m_CommPort =new System.IO.Ports.SerialPort();
    m_CommPort.DataReceived += new SerialDataReceivedEventHandler(m_CommPort_DataReceived);private void m_CommPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
    try {
    byte[] buffer; // To obtain the number of bytes waiting in the port's buffer
    Int32 iByteCount;
    buffer = new byte[iByteCount];
    m_CommPort.Read(buffer, 0, buffer.Length);
    ..... WaitResetEvent.Set(); }
    catch (Exception ex)
    { }
    }