这几天正在写一个短信程序.
测试代码如下: 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指令的返回数据却出现问题.
数据读取缓冲区一直是空的,好像短信设备根本没有返回数据.请各位帮忙想想.
测试代码如下: 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指令的返回数据却出现问题.
数据读取缓冲区一直是空的,好像短信设备根本没有返回数据.请各位帮忙想想.
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)
{ }
}