【问题标题】:SerialPort in mono in linux not responding to DataReceived eventlinux中单声道中的SerialPort不响应DataReceived事件
【发布时间】:2015-09-17 22:50:47
【问题描述】:

我正在编写一个应用程序,它使用单声道中 SerialPort 类公开的串行端口。到目前为止,我所写的内容在 Windows 中完美运行,但是在 linux 中,从未输入过 DataReceived 事件处理程序,因此我无法从我的设备接收任何数据。我已将事件处理程序声明如下:

    comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);

基本上,我正在探索良好的跨平台选项,但这是一个交易破坏者。有关如何解决此问题或发生了什么的任何建议?

编辑- 我还应该指出,我已经用其他应用程序测试了 linux 上的串行端口和设备,一切似乎都在工作。

【问题讨论】:

标签: linux mono serial-port


【解决方案1】:

也许它最后发生了变化,但据我所知,事件当前没有在 Mono 的串行端口中实现。您必须以任何方式创建另一个线程来从串行端口读取数据,这以阻塞方式发生。试试看它是否有效。

【讨论】:

  • 修复 Mono 来触发事件怎么样?它可能会涉及更少的工作,并且会减少 hacky 代码
【解决方案2】:

在 Antanas Veiverys 博客上,您可以找到两种可能的解决方法。

(2012) 通过调整单声道源代码。 http://antanas.veiverys.com/enabling-serialport-datareceived-event-in-mono/

(2013) 通过不接触单声道源而是在派生类中解决问题。 http://antanas.veiverys.com/mono-serialport-datareceived-event-workaround-using-a-derived-class/

(2014 年) 但是,我鼓励您阅读 Ben Voigts 的博客文章,他忽略了使用 DataReceivedEvent,而是使用 BaseStream 异步 BeginRead/EndRead 函数从串行端口读取数据。 http://www.sparxeng.com/blog/software/must-use-net-system-io-ports-serialport#comment-840

实现和使用给定的代码示例适用于 Windows/Unix,因此我进行了测试。而且它比以线程方式使用阻塞 Read() 函数更优雅。

【讨论】:

    【解决方案3】:

    mono 不支持串行端口的事件。

    显示在mono's website

    【讨论】:

    • 包含您描述的页面的链接会很棒。
    【解决方案4】:

    我对 SerialPort.DataReceived 也有同样的问题。康拉德的建议。

    using System.IO.Ports;
    using System.Threading;
    
    namespace Serial2
    {
        class MainClass
        {
            public static void Main(string[] args)
            {
                Thread writeThread = new Thread(new ThreadStart(WriteThread));
                Thread readThread = new Thread(new ThreadStart(ReadThread));
                readThread.Start();
                Thread.Sleep(200);        // TODO: Ugly.
                writeThread.Start();
    
                Console.ReadLine();
            }
    
            private static void WriteThread()
            {    // get port names with dmesg | grep -i tty
                SerialPort sp2 = new SerialPort("/dev/ttyS5", 115200, Parity.None, 8, StopBits.One);
                sp2.Open();
    
                if(sp2.IsOpen)
                    Console.WriteLine("WriteThread(), sp2 is open.");
                else
                    Console.WriteLine("WriteThread(), sp2 is open.");
    
                sp2.Write(" This string has been sent over an serial 0-modem cable.\n");    // \n Needed (buffering?).
                sp2.Close();
            }
            private static void ReadThread()
            {
                SerialPort sp = new SerialPort("/dev/ttyS4", 115200, Parity.None, 8, StopBits.One);
    
                sp.Open();
    
                if(sp.IsOpen)
                    Console.WriteLine("ReadThread(), sp Opened.");
                else
                    Console.WriteLine("ReadThread(), sp is not open.");
    
                while(true)
                {
                    Thread.Sleep(200);
                    if(sp.BytesToRead > 0)
                    {
                        Console.WriteLine(sp.ReadLine());
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      • 1970-01-01
      相关资源
      最近更新 更多