我设置了值为272
可是有时接收的长度为16
这是怎么回事,有没有人遇到这种问题
谢谢回复

解决方案 »

  1.   

    我是这样读取的
    SerialPort p = sender as SerialPort;
                    if (!p.IsOpen)
                    {
                        return;
                    }
                    int bytes2 = serialPort.BytesToRead;
                    
                    byte[] buffer2 = new byte[bytes2];
                    if (p.BytesToRead > 0)
                    {
                        p.Read(buffer2, 0, bytes2);
                    }
    请问怎么继续读取
      

  2.   

    p.Read(buffer2, 0, bytes2);读取没有问题.
    你再把问题描述详细一点.是有时读到16位,那么再读到到16字节的前一次读取是多少呢?
    还是一直都只读到16位.
      

  3.   

    代码很多,不好贴
    刚开始读取是272位,过了几十秒,就读到一次不是272位的数据
    以后读取到的还是272位,这样数据就错位了,不能解析
    表达能力不好,不知道你看明白了吗
    我的QQ 402514969 可否加QQ详谈,谢谢
      

  4.   

    ReceivedBytesThreshold只是触发DataReceived事件的一个阀值,由于串口通讯的实时性,很难说你每次读取的数据长度就是阀值的长度。请参考下面的方法。技术要点: 
    (1).首先,SerialPort的ReceivedBytesThreshold先设置成1,表示只要有1个字符送达端口时便触发DataReceived事件 
    (2).当DataReceived触发时,先把ReceivedBytesThreshold设置成一个比较大的值,达到读取本次端口数据时,不再触发DataReceived. 
    (3).循环读取端口中的数据,直至读完。 
    (4).移除读取数据中的非法字符。 
    (5).触发一个后台线程处理收到的数据。 
    (6).在finally中把ReceivedBytesThreshold重置回1         private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) 
            { 
                if (sender.GetType() != typeof(System.IO.Ports.SerialPort)) 
                { 
                    return; 
                } 
                string strReceive = string.Empty; 
                string strCollect = string.Empty; 
                System.IO.Ports.SerialPort comPort = (System.IO.Ports.SerialPort)sender;             try 
                { 
                    comPort.ReceivedBytesThreshold = comPort.ReadBufferSize; 
                    while (true) 
                    { 
                        strReceive = comPort.ReadExisting(); 
                        if (string.Equals(strReceive, string.Empty)) 
                        { 
                            break; 
                        } 
                        else 
                        { 
                            strCollect += strReceive; 
                            Application.DoEvents(); 
                            Thread.Sleep(100); 
                        } 
                    } 
                    strCollect = strCollect.Replace("\0", string.Empty); 
                    strCollect = strCollect.Replace("\r\n", string.Empty); 
                    strCollect = strCollect.Replace("\r", string.Empty); 
                    strCollect = strCollect.Replace("\n", string.Empty);                 if (!this.bIsHandleCom) 
                    { 
                        this.bIsHandleCom = true; 
                        mReceiveData = strCollect; 
                        if (ReceiveDataParserEvent != null) 
                            ReceiveDataParserEvent(mReceiveData); 
                        if (ThreadReceiveParser != null && !ThreadReceiveParser.IsAlive) 
                        { 
                            ThreadReceiveParser.Start(); 
                        } 
                    }             } 
                catch (Exception ex) 
                { 
                    MessageBox.Show(this, ex.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); 
                } 
                finally 
                { 
                    comPort.ReceivedBytesThreshold = 1; 
                } 
            }