UART reading performance at high baud rates (FTDI + Windows)

I have an FPGA digitizer connected via FTDI FT4232 USB-UART converter to my Windows 10 computer. The communication speed is 5 Mega Bauds, 8N1. I am trying to read 1048576 bytes of data which is sent as a response to a simple text command. Data is protected by a CRC32 checksum (transferred using another command).

Everything works quite well with RealTerm terminal emulator, and now I need to do the same thing with my C#.NET application. Having read some real experience with the SerialPort class, I currently have the code below. The approach is simple: There is a dedicated reader thread which reads data from the serial port and pushes it to a producer-consumer queue, from which data is read by another thread.

_PicoblazeReadBuffer = new ConcurrentQueue<byte[]>();
_Port = new SerialPort("COM12", 5000000, Parity.None, 8, StopBits.One);
_PicoblazeReaderThreadCanceller = new CancellationTokenSource();
_PicoblazeReaderThread = new Thread(ReaderThread) {
    Priority = ThreadPriority.Highest,
    IsBackground = true,
};
_PicoblazeReaderThread.Start(_PicoblazeReaderThreadCanceller.Token);

...

private void ReaderThread(object cancellationTokenAsObject)
{
    byte[] buffer = new byte[131072];
    int bufferOffset = 0;
    CancellationToken token = (CancellationToken)cancellationTokenAsObject; 

    Stopwatch stopwatch = new Stopwatch();
    while (_Port.IsOpen && !token.IsCancellationRequested) {
        try {
            int readLength = Math.Min(4096, buffer.Length - bufferOffset);
            int actualLength = _Port.BaseStream.Read(buffer, bufferOffset, readLength);
            if (actualLength > 0) {
                bufferOffset += actualLength;
                if (bufferOffset >= 65535 || stopwatch.ElapsedMilliseconds > 100) {
                    byte[] chunk = new byte[bufferOffset];
                    Buffer.BlockCopy(buffer, 0, chunk, 0, bufferOffset);
                    _PicoblazeReadBuffer.Enqueue(chunk);
                    _PicoblazeReadSemaphore.Release();
                    bufferOffset = 0;
                    stopwatch.Restart();
                }
            }
        } catch (Exception ex) {
            ...
        }
    }
}

The problem: Some data get lost during the transfer, and therefore my consumer thread times out. When I tried to save the intermediate result, it seems the data always get trimmed at the end, but the beginning is valid.

  • When I increase the Latency Timer from default 16 ms to at least 60 ms, the reading completes successfully. I cannot understand, why this happens, because if I understand it correctly, the latency timer should affect performance only when I send short data packets.
  • Although I want to read 4096 bytes (default driver buffer size), the function completes earlier and returns only 250 bytes on average. Therefore, I added a simple data packing condition, which reduces the overhead related to the queue locking. This issue might be connected to the event characters, which forces the chip to flush out the buffers, but the problem persists even if I send all-zero pattern from the FPGA.

Do you have any experience with high baud-rate data transfers in C#?

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật