用VS2005 的SerialPort 做了一个读卡的程序,
通过Write方法向串口发送代码,用serialPort.ReadByte()
方法接收串口返回的代码,
 问题是:发送接收一次后,大约要等15秒后,再发送,才能接收到数据,能不能让这时间间隔短一点??
这个时间是与哪些设置有关?

解决方案 »

  1.   

    应该是代码的问题。查看一下是否串口的工作方式两边不一样。
    另外,使用DataReceived事件来处理读取数据,更好一些。
      

  2.   

     我代码如下: 请帮忙看看有什么问题??发送: spcom.Write(tmpb, 0, tmpb.Length);  
     // spcom 是SerialPort 控件, tmpb 是要发的byte[] 数组
    读取:
     private void spcom_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {  try
                {                 
                    int bytesRead = spcom.BytesToRead;
                    byte[] bytesData = new byte[bytesRead];               
                    for (int i = 0; i < bytesRead; i++)
                {   bytesData[i] = Convert.ToByte(spcom.ReadByte());  }                
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    for (int i = 4; i < bytesRead-1; i++)
                 sb.Append(bytesData[i].ToString("x2")).Append("   ");                           
                    string code = sb.ToString().ToUpper();               
                  MessageBox.Show(code);
                  spcom.DiscardInBuffer();
                  spcom.DiscardOutBuffer();                                 
                }
                catch
                {
                    MessageBox.Show("Error");
                }          
            }