1. <tfoot id='DlIvb'></tfoot>

    <small id='DlIvb'></small><noframes id='DlIvb'>

    <legend id='DlIvb'><style id='DlIvb'><dir id='DlIvb'><q id='DlIvb'></q></dir></style></legend>
      • <bdo id='DlIvb'></bdo><ul id='DlIvb'></ul>

      <i id='DlIvb'><tr id='DlIvb'><dt id='DlIvb'><q id='DlIvb'><span id='DlIvb'><b id='DlIvb'><form id='DlIvb'><ins id='DlIvb'></ins><ul id='DlIvb'></ul><sub id='DlIvb'></sub></form><legend id='DlIvb'></legend><bdo id='DlIvb'><pre id='DlIvb'><center id='DlIvb'></center></pre></bdo></b><th id='DlIvb'></th></span></q></dt></tr></i><div id='DlIvb'><tfoot id='DlIvb'></tfoot><dl id='DlIvb'><fieldset id='DlIvb'></fieldset></dl></div>

    1. 数据到来时C#只读串口

      C# read only Serial port when data comes(数据到来时C#只读串口)

          <small id='QxbP6'></small><noframes id='QxbP6'>

            <tbody id='QxbP6'></tbody>

          1. <legend id='QxbP6'><style id='QxbP6'><dir id='QxbP6'><q id='QxbP6'></q></dir></style></legend>
            <tfoot id='QxbP6'></tfoot>
                <bdo id='QxbP6'></bdo><ul id='QxbP6'></ul>

                <i id='QxbP6'><tr id='QxbP6'><dt id='QxbP6'><q id='QxbP6'><span id='QxbP6'><b id='QxbP6'><form id='QxbP6'><ins id='QxbP6'></ins><ul id='QxbP6'></ul><sub id='QxbP6'></sub></form><legend id='QxbP6'></legend><bdo id='QxbP6'><pre id='QxbP6'><center id='QxbP6'></center></pre></bdo></b><th id='QxbP6'></th></span></q></dt></tr></i><div id='QxbP6'><tfoot id='QxbP6'></tfoot><dl id='QxbP6'><fieldset id='QxbP6'></fieldset></dl></div>

                本文介绍了数据到来时C#只读串口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想读取我的串口,但只有在数据到来时(我不想轮询).

                I want read my serial port but only when data comes(I want not polling).

                我就是这样做的.

                                Schnittstelle = new SerialPort("COM3");
                                Schnittstelle.BaudRate = 115200;
                                Schnittstelle.DataBits = 8;
                                Schnittstelle.StopBits = StopBits.Two;
                             ....
                

                然后我开始一个线程

                             beginn = new Thread(readCom);
                             beginn.Start();
                

                在我的 readCom 中,我正在连续阅读(轮询:()

                and in my readCom I'm reading continuous (polling :( )

                private void readCom()
                    {
                
                        try
                        {
                            while (Schnittstelle.IsOpen)
                            {
                
                                Dispatcher.BeginInvoke(new Action(() =>
                                {
                
                                    ComWindow.txtbCom.Text = ComWindow.txtbCom.Text + Environment.NewLine + Schnittstelle.ReadExisting();
                                    ComWindow.txtbCom.ScrollToEnd();
                                }));
                
                                beginn.Join(10);
                
                            }
                        }
                        catch (ThreadAbortException) 
                        {
                
                        }
                
                        catch (Exception ex)
                        {
                            System.Windows.Forms.MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                

                我希望你在中断到来时阅读.但是我该怎么做呢?

                I want yout read when a Interrupt is coming. But how can I do that ?

                推荐答案

                您必须向 DataReceived 事件添加一个 eventHandler.

                You will have to add an eventHandler to the DataReceived event.

                以下是来自 msdn.microsoft.com 的示例,并进行了一些修改:查看评论!:

                Below is an example from msdn.microsoft.com, with some edits: see comments!:

                public static void Main()
                {
                    SerialPort mySerialPort = new SerialPort("COM1");
                
                    mySerialPort.BaudRate = 9600;
                    mySerialPort.Parity = Parity.None;
                    mySerialPort.StopBits = StopBits.One;
                    mySerialPort.DataBits = 8;
                    mySerialPort.Handshake = Handshake.None;
                
                    mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                
                    mySerialPort.Open();
                
                    Console.WriteLine("Press any key to continue...");
                    Console.WriteLine();
                    Console.ReadKey();
                    mySerialPort.Close();
                }
                
                private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
                {
                    SerialPort sp = (SerialPort)sender;
                    string indata = sp.ReadExisting();
                    Debug.Print("Data Received:");
                    Debug.Print(indata);
                }
                

                每次数据进入时,DataReceivedHandler 都会触发并将您的数据打印到控制台.我认为你应该能够在你的代码中做到这一点.

                Everytime data comes in, the DataReceivedHandler will trigger and prints your data to the console. I think you should be able to do this in your code.

                这篇关于数据到来时C#只读串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                How to store delegates in a List(如何将代表存储在列表中)
                How delegates work (in the background)?(代表如何工作(在后台)?)
                C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                  <tbody id='3o6Sf'></tbody>

              • <small id='3o6Sf'></small><noframes id='3o6Sf'>

                        <tfoot id='3o6Sf'></tfoot>
                          <bdo id='3o6Sf'></bdo><ul id='3o6Sf'></ul>

                          <legend id='3o6Sf'><style id='3o6Sf'><dir id='3o6Sf'><q id='3o6Sf'></q></dir></style></legend>
                        • <i id='3o6Sf'><tr id='3o6Sf'><dt id='3o6Sf'><q id='3o6Sf'><span id='3o6Sf'><b id='3o6Sf'><form id='3o6Sf'><ins id='3o6Sf'></ins><ul id='3o6Sf'></ul><sub id='3o6Sf'></sub></form><legend id='3o6Sf'></legend><bdo id='3o6Sf'><pre id='3o6Sf'><center id='3o6Sf'></center></pre></bdo></b><th id='3o6Sf'></th></span></q></dt></tr></i><div id='3o6Sf'><tfoot id='3o6Sf'></tfoot><dl id='3o6Sf'><fieldset id='3o6Sf'></fieldset></dl></div>