Linux 中单声道的串行端口不响应 DataReceived 事件

发布于 2024-12-04 05:54:42 字数 380 浏览 1 评论 0原文

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

    comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);

基本上我正在探索良好的跨平台选项,这是一个破坏性的事情。关于如何解决这个问题或发生了什么事有什么建议吗?

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

I am writing an app that uses the serial port exposed by the SerialPort class in mono. What I have written so far works perfect in windows, however in linux the DataReceived event handler is never entered, so I cannot receive any data form my device. I have declared the event handler as follows:

    comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);

Basically I am exploring good cross-platform options and this is a deal-breaker. Any advise on how to fix this or what is going on?

Edit-
I should also point out that I have tested the serial port and device on linux with other applications and all appears to be working.

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

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

发布评论

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

评论(4

江湖正好 2024-12-11 05:54:43

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

Maybe it has changed lastly, but as far as I know, events are not currently implemented in Mono's serial port. You have to make another thread in any flavour to read data from serial port, which happens in blocking manner. Try it and tell if it worked.

醉南桥 2024-12-11 05:54:43

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

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

(2013) 通过不触及单声道源代码,而是在派生类中解决问题。
http://antanas.veiverys.com/单串行端口数据接收事件解决方法使用派生类/

(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() 函数更优雅。

On Antanas Veiverys blog you can find two possible ways to solve it.

(2012) By adjusting the mono source code.
http://antanas.veiverys.com/enabling-serialport-datareceived-event-in-mono/

(2013) By not touching the mono source but solving the issue in a derived class.
http://antanas.veiverys.com/mono-serialport-datareceived-event-workaround-using-a-derived-class/

(2014)
However, I encourage you to read Ben Voigts blog post where he ignores using the DataReceivedEvent and instead used the BaseStream async BeginRead/EndRead functions to read data from the serial port.
http://www.sparxeng.com/blog/software/must-use-net-system-io-ports-serialport#comment-840

Implementing and using the given code sample works on both Windows/Unix, so I have tested. And it is more elegant than using the blocking Read() function in a threaded fashion.

谁的新欢旧爱 2024-12-11 05:54:43

mono 不支持串行端口事件。

它显示在 mono 网站

mono does not support Event for serialport.

It is shown on mono's website

征﹌骨岁月お 2024-12-11 05:54:43

我对 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());
                }
            }
        }
    }
}

I have the same problem with SerialPort.DataReceived. Konrad's advice.

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