日期:2014-05-17  浏览次数:21102 次

C# Serialport串口通信,接收数据丢失
用DataReceived方法接收数据时有时候会丢失,尤其是在debug的时候,有时候甚至多定义几个变量,数据接收也会出现丢失,有什么办法可以解决吗?

------解决方案--------------------
引用:
Quote: 引用:


请找缓冲区。。。之前有说过的
求链接

http://bbs.csdn.net/topics/390316188
------解决方案--------------------
如果是二进制,最好用流或buffer来操作。

 while (i > 0)
                    {
                        int readBufCount = SerialPortClient.BytesToRead;
                        if (readBufCount > 8)
                        {
                            m_autoPortEvent.WaitOne(40, false);
                            readBufCount = SerialPortClient.BytesToRead;
                            recvData = new byte[readBufCount];
                            SerialPortClient.Read(recvData, 0, recvData.Length);
                            return recvData;
                        }
                        i--;
                    }
                    if (recvData == null)
                    {
                        throw new Exception("time out for getData");
                    }