谢谢jedypjd网友,是的,利用断点方式调试读取串口发送过来的数据时,发现有时小于18,但大多数是远远大于18,为此,想解决每次接收的数据以每18个字节为一组的读取出来,利用队列形式。
以下是实现队列方法,但有些代码不知怎么写,请高手支招,谢谢!:
 
        int sindex=0;  //定义队列首指针
        int eindex=0;  //定义队列尾指针
        const int bufSIZE = 500;//数据大小
        byte[] Sbuffer = new byte[bufSIZE];
        //串口接收数据 
        private void COMPort_DataReceived(object sender, SerialDataReceivedEventArgs e)//接收
        {
            int i=0,con;
            int bytes = ComPort.BytesToRead;//获取接收缓冲区中数据的字节数
            byte[] buffer = new byte[bytes];//定义所获取字节数大小的缓冲区
            ComPort.Read(buffer, 0, bytes);//输入缓冲区读取的一些字节并将那些字节写入字节数组中指定的偏移量处
            //将数据放在自定义缓冲区Sbuffer 内
            con=((bufSIZE-eindex)>buffer.Length)?buffer.Length:bufSIZE-eindex;  //比较用于存放的缓冲区与接收的字节缓冲区,若接收的字节数大于定义的缓冲区大小,那么                                                                               // 常量赋值为定义的缓冲区大小;反之为实际接收的缓冲区大小。
            System.Buffer.BlockCopy(buffer, eindex, Sbuffer, 0, con); //将指定数目的字节con从起始于特定偏移量eindex的源数组复制到起始于特定偏移量0的目标数组Sbuffer。
            if(con<buffer.Length)
            {
                if((buffer.Length-con)<sindex)
                {
                    System.Buffer.BlockCopy(buffer, eindex, Sbuffer, 0, buffer.Length-con); 
                    eindex= buffer.Length-con;
                }
                else
                {
                }
            }
            else
            {
                eindex=eindex+con; 
            }
            //取一个完整的包
            while(true)
            {
              while((Sbuffer[sindex]!=0xff)&&(eindex!=sindex))
                {
                  sindex=(++sindex)%bufSIZE;
                }
              if((eindex>sindex)&&((eindex-sindex)>=18))
              {
                {                
                
                }
               sindex=(18+sindex)%bufSIZE;
              }
              else if((eindex<sindex)&&((eindex+bufSIZE-sindex)>18))
              {
                 {}
                 sindex=(18+sindex)%bufSIZE;
               }
              else break;
            }
            if (sindex == 0)
            {
                while ((buffer[i] != 0xff) && (i < buffer.Length)) { i++; }
                if (i < buffer.Length)
                {
                    System.Buffer.BlockCopy(buffer, i, Sbuffer, 0, buffer.Length - i);
                    sindex = buffer.Length - i;
                }
            }
            else
            {
                System.Buffer.BlockCopy(buffer, i, Sbuffer, sindex, buffer.Length );
                sindex = sindex + buffer.Length;
            }
            if (sindex >= 18)
            {
                { }
            }
            //
            Log(LogMsgType.Incoming, ByteArrayToHexString(buffer));
            CheckData(buffer);  //检验接收的数据正确性
        }