C#通过SerialPort读取字节变慢

时间:2014-11-05 13:31:07

标签: c# serial-port

我使用标准的.Net Serialport clas来读取字节(没有事件)。

我的代码如下所示:

              receivedDataList = new List<byte>();

              _serialPort.ReadTimeout = timeout;

              // First byte has length.
              int bytesExpected = _serialPort.ReadByte();
              receivedDataList.Add((byte)bytesExpected);
              // initialize buffer with expected length.
              byte[] buffer = new byte[bytesExpected];
              int offset = 0;
              int bytesReaded;
              // Read as long as the expected bytes are not reached.
              while (bytesExpected > 0 && (bytesReaded = _serialPort.Read(buffer, offset, bytesExpected)) > 0)
              {
                 offset += bytesReaded;
                 bytesExpected -= bytesReaded;
              }
              receivedDataList.AddRange(buffer);

我正在阅读9600 8n1。 10字节的读取过程需要34ms。如果我在linux中读取,则读取过程最多需要。为20ms。

有没有办法快速阅读,我不想阅读事件......

0 个答案:

没有答案