【问题标题】:how to acces SerialPort.Flush() and SerialPort.Finalize() functions如何访问 Serial Port.Flush() 和 Serial Port.Finalize() 函数
【发布时间】:2012-11-27 16:20:49
【问题描述】:

我只是在浏览 msdn 时发现了这个 page。我以前从未见过函数SerialPort.Flush()SerialPort.Finalize()。所以我尝试使用这些功能,但出现错误。

我添加了System.IO.Ports 命名空间,但在Finalize() 函数上出现以下错误:

Cannot access protected member 'object.~Object()' via a qualifier of type 'System.IO.Ports.SerialPort'; the qualifier must be of type 'STP_Design.SerialCom' (or derived from it)

我在Flush() 函数上收到以下错误:

'System.IO.Ports.SerialPort' does not contain a definition for 'Flush' and no extension method 'Flush' accepting a first argument of type 'System.IO.Ports.SerialPort' could be found (are you missing a using directive or an assembly reference?)

我想我正在使用错误的方法访问 finalizing 函数(我可能根本不能访问它)但我真的想知道 Flush() 函数怎么样。

我用过这样的东西:

    private void test()
    {
        SerialPort s1 = new SerialPort();
        s1.PortName = "COM1";
        s1.Open();
        thread.Sleep(200);
        s1.WriteLine("test");
        s1.Flush();
        s1.Close();
        thread.Sleep(200);
        s1.Finalize();
    }

这里有什么见解吗?

编辑:使用 SerialPort.Dispose(boolean) 函数遇到同样的问题可选的布尔值也无法访问...

【问题讨论】:

  • 你永远不能直接调用终结器...
  • 也没有Flush 方法。也许你可以试试SerialPort.BaseStream.Flush
  • 您正在查看 .NET Microframework 的文档。

标签: c# .net winforms io


【解决方案1】:

http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx:没有Flush。您正在查看 .Net Micro Framework,它显然有一个 Flush

GC 在垃圾回收结束时调用终结器。用户代码不能从外部访问它们,也不应该显式调用它们。

【讨论】:

  • 这似乎很公平......我认为 Finalize 根本不应该从代码中调用,并且我认为 dispose 函数中的 bool 是受保护的。感谢您的回答!
  • MySerial.DiscardInBuffer()
【解决方案2】:

您可以使用 DiscardInBuffer() 和 DiscardOutBuffer()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-03
    • 2018-04-21
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 2022-08-12
    • 1970-01-01
    相关资源
    最近更新 更多