C#中最快的串口通信? [英] the fastest serial port communication possible in C#?

查看:374
本文介绍了C#中最快的串口通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这个论坛的新手,我在 C# 编程方面没有很多经验.我用 C# 构建了简单的 Windows 应用程序,以与我朋友给我的一些电子板进行通信.他在微控制器中定义了串口波特率为38400.我想通过串口尽可能快地发送和接收字节.使用 C# 中现有的串行端口工具,我只能读取字节,但即使在其自己的线程中使用 eventhandler 仍然不够快.我的缓冲区 [4096] 在几秒钟内就满了.

I am new at this forum and I do not have many experiences with programming in C#. I have build simple Windows application in C# to communicate with some electronic board that my friend gave it to me. He has defined serial port baud rate in microcontroller at 38400. I want to send and receive bytes via serial port as fast as possible. With existing serialport tool in C# I was able only to read bytes but still not fast enough even with eventhandler in its own thread. My buffer[4096] was full in few seconds.

我的问题是是否存在其他一些不占用太多处理器时间的串行端口通信功能或工具.我需要在不到 200 us 的时间内读取一个字节.我在嵌入式系统方面有更多经验,这不是问题.

My question is if there exist some other functions or tools for serial port communication that does not use so much processor time. I need to read one byte in less than 200 us. I have more experiences with embedded systems where this was not a problem.

感谢大家的建议或想法.

Thanks everyone for some advice or idea.

托马兹

推荐答案

请看:如果您必须使用 .NET System.IO.Ports.SerialPort

Please take a look at : If you must use .NET System.IO.Ports.SerialPort

Ben Voigt(作者)给出了一个很好的例子,说明如何使用 SerialPort 的固有 BaseStream 成员,它可以更有效地使用底层 Win32 API.

Ben Voigt (the author) gives an excellent example on how to use the inherent BaseStream member of SerialPort which is more efficient for using the underlying Win32 API.

Port = new SerialPort("COM" + PORT_NUMBER.ToString()); 
...
private void ReadEvent()  
{      
    byte[] buffer = new byte[2000];  
    Action kickoffRead = null;  
    kickoffRead = (Action)(() => Port.BaseStream.BeginRead(buffer, 0, buffer.Length, delegate(IAsyncResult ar)  
    {  
        try  
        {  
            int count = Port.BaseStream.EndRead(ar);  
            byte[] dst = new byte[count];  
            Buffer.BlockCopy(buffer, 0, dst, 0, count);  
            RaiseAppSerialDataEvent(dst);  
        }  
        catch (Exception exception)  
        {  
            MessageBox.Show(ERROR == > " + exception.ToString());    
        }  
        kickoffRead();  
    }, null)); kickoffRead();  
}

问候.

这篇关于C#中最快的串口通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆