开发者

Serial Port reading causes application freezing for a while - Do I need threading?

开发者 https://www.devze.com 2023-04-09 23:42 出处:网络
I got some code in c# for reading results of AT command compatible modem. In case I start COM port reading whole apps freezes until data received. I suppose I should use threading but I\'ve no idea h

I got some code in c# for reading results of AT command compatible modem. In case I start COM port reading whole apps freezes until data received. I suppose I should use threading but I've no idea how ?

        if (!serialP开发者_运维技巧ort1.IsOpen)
            serialPort1.Open();
        serialPort1.WriteTimeout = 5000;
        serialPort1.NewLine = "\r\n";
        serialPort1.WriteLine("AT#MON");
        serialPort1.NewLine = "\r\n";
        while (true) // Loop indefinitely
        {

            serialPort1.NewLine = "\r\n"; // Prompt
            string linee = serialPort1.ReadLine(); // Get string from port

            if (System.Text.RegularExpressions.Regex.IsMatch(linee, ("^xcqa:")))
            {
                textBox1.AppendText(System.Environment.NewLine);
            }
            if (System.Text.RegularExpressions.Regex.IsMatch(linee, ("^fkss:")))
            {
                textBox1.AppendText(System.Environment.NewLine);
            }
            if (System.Text.RegularExpressions.Regex.IsMatch(linee, ("ended"))) // Check string
            {
                break;
            }
            textBox1.AppendText(linee);
        }


Yes. You need to use multithreading. Spawning a new thread will allow other code in your application to carry on simultaneously. For this, your serial port read code would need its own thread. Read up on multithreading doc for C#.

  • Threading Tutorial
  • Threading in C#
  • Introduction to Multithreading in C#
0

精彩评论

暂无评论...
验证码 换一张
取 消