.net 框架有好的串口类吗?

发布于 2024-07-13 12:48:55 字数 272 浏览 5 评论 0原文

有人知道有一个好的(希望是免费的)类来替代 .net SerialPort 类吗? 这给我带来了问题,我需要一个稍微灵活一点的。

请参阅我关于我的问题的其他线程,但本质上我需要抑制 IOCTL_SERIAL_SET_DTR 和 IOCTL_SERIAL_CLR_DTR 命令当我打开端口时发出,所以我需要比.net框架提供的类更灵活的东西。

Does anybody know of a good (hopefully free) class to replace the .net SerialPort class? It is causing me issues and I need one that is slightly more flexible.

See my other thread about my issues, but essentially I need to suppress the IOCTL_SERIAL_SET_DTR and IOCTL_SERIAL_CLR_DTR commands from being issued when I open the port, so I need something more flexible than the class provided by the .net framework.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赴月观长安 2024-07-20 12:48:55

几年前,我使用 OpenNETCF.IO.Serial在串行支持添加到 .net 之前。 它适用于紧凑型框架,但我将它用于紧凑型设备和常规 Windows 应用程序。 你可以获得源代码,这样你就可以自己修改它,这就是我所做的。

它基本上围绕从 kernel32.dll 导入的串行函数创建 ac# 包装器。

您可能还想看看如何捕获因 USB 电缆被拔出而消失的串行端口

这是代码我以前叫它

     using OpenNETCF.IO.Serial;

     public static Port port;
     private DetailedPortSettings portSettings;
     private Mutex UpdateBusy = new Mutex();

     // create the port
     try
     {
        // create the port settings
        portSettings = new HandshakeNone();
        portSettings.BasicSettings.BaudRate=BaudRates.CBR_9600;

        // create a default port on COM3 with no handshaking
        port = new Port("COM3:", portSettings);

        // define an event handler
        port.DataReceived +=new Port.CommEvent(port_DataReceived);

        port.RThreshold = 1;    
        port.InputLen = 0;      
        port.SThreshold = 1;    
        try
        {
           port.Open();
        }
        catch
        {
           port.Close();
        }
     }
     catch
     {
        port.Close();
     }

     private void port_DataReceived()
     {

        // since RThreshold = 1, we get an event for every character
        byte[] inputData = port.Input;

        // do something with the data
        // note that this is called from a read thread so you should 
        // protect any data pass from here to the main thread using mutex
        // don't forget the use the mutex in the main thread as well
        UpdateBusy.WaitOne();
        // copy data to another data structure
        UpdateBusy.ReleaseMutex();

     }

     private void port_SendBuff()
     {
        byte[] outputData = new byte[esize];
        crc=0xffff;
        j=0;
        outputData[j++]=FS;
        //  .. more code to fill up buff
        outputData[j++]=FS;
        // number of chars sent is determined by size of outputData
        port.Output = outputData;
     }

     // code to close port
     if (port.IsOpen)
     {
        port.Close();
     }
     port.Dispose();

Several years ago I used OpenNETCF.IO.Serial before serial support was added to .net. It is for the compact framework but I used it for both compact devices and regular windows apps. You get the source code so you can modify it yourself which is what I did.

It basically creates a c# wrapper around the serial function imported out of kernel32.dll.

You might also want to have a look at How to capture a serial port that disappears because the usb cable gets unplugged

Here is the code that I used to call it

     using OpenNETCF.IO.Serial;

     public static Port port;
     private DetailedPortSettings portSettings;
     private Mutex UpdateBusy = new Mutex();

     // create the port
     try
     {
        // create the port settings
        portSettings = new HandshakeNone();
        portSettings.BasicSettings.BaudRate=BaudRates.CBR_9600;

        // create a default port on COM3 with no handshaking
        port = new Port("COM3:", portSettings);

        // define an event handler
        port.DataReceived +=new Port.CommEvent(port_DataReceived);

        port.RThreshold = 1;    
        port.InputLen = 0;      
        port.SThreshold = 1;    
        try
        {
           port.Open();
        }
        catch
        {
           port.Close();
        }
     }
     catch
     {
        port.Close();
     }

     private void port_DataReceived()
     {

        // since RThreshold = 1, we get an event for every character
        byte[] inputData = port.Input;

        // do something with the data
        // note that this is called from a read thread so you should 
        // protect any data pass from here to the main thread using mutex
        // don't forget the use the mutex in the main thread as well
        UpdateBusy.WaitOne();
        // copy data to another data structure
        UpdateBusy.ReleaseMutex();

     }

     private void port_SendBuff()
     {
        byte[] outputData = new byte[esize];
        crc=0xffff;
        j=0;
        outputData[j++]=FS;
        //  .. more code to fill up buff
        outputData[j++]=FS;
        // number of chars sent is determined by size of outputData
        port.Output = outputData;
     }

     // code to close port
     if (port.IsOpen)
     {
        port.Close();
     }
     port.Dispose();
梦与时光遇 2024-07-20 12:48:55

我还没有尝试过这个,但也许你可以:

  • 使用 Reflector 或类似内容
  • 根据需要更改源代码
  • 在不同的命名空间中重新构建修改后的副本,并使用它

I haven't tried tried this but, perhaps you can:

  • Get the source code from System.IO.Ports.SerialPort by using Reflector or similar
  • Change that source code as you like
  • Rebuilt the modified copy in a different namespace, and use it
安静被遗忘 2024-07-20 12:48:55

标准 .NET SerialPort 不是密封类 - 您是否有机会从子类获得所需的行为?

The standard .NET SerialPort isn't a sealed class - any chance you can get the behaviour you need from a subclass?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文