使用 C# 从串行端口接收消息的线程无法工作

发布于 2024-08-27 22:29:59 字数 1759 浏览 5 评论 0原文

我正在使用串行端口接收消息。下面的函数在线程中运行。当我调试时,我发现线程运行正常。但“if (sp.IsOpen)”始终为 false,因此代码根本不在 IF 条件内执行。据说港口已关闭。

我的系统中将有多个串行端口,但我不知道哪个端口将接收消息。所以我需要监听一个线程中的所有端口。

我该如何解决这里的问题?

 private void ListenerPorts()
    {

        log.Info("Listening Thread Started");

        while (true)
        {
            //foreach (SerialPort sp in storeport)
            foreach (SerialPort sp in comPortsList)
            {

                if (sp.IsOpen)
                {
                    sp.ReadTimeout = readTimeoutInMs;
                    sp.WriteTimeout = writeTimeoutInMs;

                    try
                    {
                        string msg = sp.ReadLine();
                        this.GetMessageRichTextBox("Message : " + msg + "\n");
                        sp.WriteLine(sp.PortName);

                        if (msg.Contains("COM"))
                        {
                            // is AutoScan
                            receiverPortName = sp.ReadLine();
                            this.updateLblStatusRichTextBox(sp.PortName + " is connected to " + msg + "\n");
                        }
                        else
                        {
                            //standalone is uppercase
                            ReceiverPortName = sp.ReadLine();
                            this.updateLblStatusRichTextBox(sp.PortName + " is connected to " + ReceiverPortName + "\n");

                        }
                    }

                    catch (Exception ex)
                    {
                        // no data
                        System.Diagnostics.Debug.WriteLine(sp.PortName + " : " + ex.Message);

                    }
                }           
            }
        }
    }

I am using Serial port to receive the messages. The below function is running in a thread. When i debug i find that the thread is running properly. But "if (sp.IsOpen)" is always false, due to which the code is not executing inside the IF condition at all. It says the Port is closed.

I will be having multiple serial ports in my system and i will not know, which port will receive the message. So i need to listen to all the ports in one Thread.

How can i solve my problem here ?

 private void ListenerPorts()
    {

        log.Info("Listening Thread Started");

        while (true)
        {
            //foreach (SerialPort sp in storeport)
            foreach (SerialPort sp in comPortsList)
            {

                if (sp.IsOpen)
                {
                    sp.ReadTimeout = readTimeoutInMs;
                    sp.WriteTimeout = writeTimeoutInMs;

                    try
                    {
                        string msg = sp.ReadLine();
                        this.GetMessageRichTextBox("Message : " + msg + "\n");
                        sp.WriteLine(sp.PortName);

                        if (msg.Contains("COM"))
                        {
                            // is AutoScan
                            receiverPortName = sp.ReadLine();
                            this.updateLblStatusRichTextBox(sp.PortName + " is connected to " + msg + "\n");
                        }
                        else
                        {
                            //standalone is uppercase
                            ReceiverPortName = sp.ReadLine();
                            this.updateLblStatusRichTextBox(sp.PortName + " is connected to " + ReceiverPortName + "\n");

                        }
                    }

                    catch (Exception ex)
                    {
                        // no data
                        System.Diagnostics.Debug.WriteLine(sp.PortName + " : " + ex.Message);

                    }
                }           
            }
        }
    }

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

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

发布评论

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

评论(1

水染的天色ゝ 2024-09-03 22:29:59

你的串口初始化代码在哪里?特别是 SerialPort.Open(); 行?

看看使用

SerialPort.DataReceived += 
  new SerialDataReceivedEventHandler(SerialDataReceivedEventHandler);

从他们那里接收数据。

Where's your initialization code for the serial ports? And in particular the line SerialPort.Open();?

Take a look at using

SerialPort.DataReceived += 
  new SerialDataReceivedEventHandler(SerialDataReceivedEventHandler);

to receive the data from them instead.

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