<bdo id='bvuvV'></bdo><ul id='bvuvV'></ul>

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

      <tfoot id='bvuvV'></tfoot>
    1. <legend id='bvuvV'><style id='bvuvV'><dir id='bvuvV'><q id='bvuvV'></q></dir></style></legend>

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

    3. 从 C# 中的串行端口读取

      时间:2023-11-10
    4. <small id='HuruO'></small><noframes id='HuruO'>

      <legend id='HuruO'><style id='HuruO'><dir id='HuruO'><q id='HuruO'></q></dir></style></legend>

        • <bdo id='HuruO'></bdo><ul id='HuruO'></ul>

                <tbody id='HuruO'></tbody>
              <tfoot id='HuruO'></tfoot>

                <i id='HuruO'><tr id='HuruO'><dt id='HuruO'><q id='HuruO'><span id='HuruO'><b id='HuruO'><form id='HuruO'><ins id='HuruO'></ins><ul id='HuruO'></ul><sub id='HuruO'></sub></form><legend id='HuruO'></legend><bdo id='HuruO'><pre id='HuruO'><center id='HuruO'></center></pre></bdo></b><th id='HuruO'></th></span></q></dt></tr></i><div id='HuruO'><tfoot id='HuruO'></tfoot><dl id='HuruO'><fieldset id='HuruO'></fieldset></dl></div>
                本文介绍了从 C# 中的串行端口读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我尝试使用 Readline() 并且数据被丢弃,我尝试使用 Read() 但我不知道如何有一个错误证明方法来做到这一点,因为我可能会一个接一个地收到几个数据包而我没有知道会有另一个数据包进来的方法.在数据包之间BytesToRead是0,所以我不能使用它.当向缓冲区读取数据时,您有一个计时器或让线程休眠以允许所有数据包到达?

                I have tried using Readline() and data gets dropped, I tried using Read() but I am not sure how to have an error proof method of doing it, since I may get several packets one after another and I have no way of knowing that there is going to be another packet coming in. In between packets BytesToRead is 0, so I can't use it. When reading data to the buffer to you have a timer or put the thread to sleep to allow for all the packets to arrive?

                我迷路了.不知道下一步该尝试什么.

                I am lost. Don't know what to try next.

                我应该提一下,我不能保证从串行端口出来的字符串将以 或 或 结尾.我只需要一种万无一失的方法来读取用户按下 PRINT 时来自秤的所有数据包.

                I should mention that I get no guarantee that the string coming off the serial port will be ended with or or . I simply need a fool proof way to read ALL the packets that will come from the scale when the user presses PRINT on it.

                有人在这里回答了我喜欢的想法 - 为所有数据包等待一定的时间,但他们删除了他们的答案.你有机会重新发布吗?

                Someone answered here with the idea I liked - waiting for a certain amount of time for all the packets, but they erased their answer. ANy chance you could re-post it?

                推荐答案

                你试过听DataRecieved 事件.io.ports.serialport.aspx" rel="noreferrer">SerialPort 类?

                Have you tried listening to the DataRecieved event of the SerialPort class?

                public class MySerialReader : IDisposable
                {
                    private SerialPort serialPort;
                    private Queue<byte> recievedData = new Queue<byte>();
                
                    public MySerialReader()
                    {
                        serialPort = new SerialPort();
                        serialPort.Open();
                
                        serialPort.DataReceived += serialPort_DataReceived;
                    }
                
                    void serialPort_DataReceived(object s, SerialDataReceivedEventArgs e)
                    {
                        byte[] data = new byte[serialPort.BytesToRead];
                        serialPort.Read(data, 0, data.Length);
                
                        data.ToList().ForEach(b => recievedData.Enqueue(b));
                
                        processData();
                    }
                
                    void processData()
                    {
                        // Determine if we have a "packet" in the queue
                        if (recievedData.Count > 50)
                        {
                            var packet = Enumerable.Range(0, 50).Select(i => recievedData.Dequeue());
                        }
                    }
                
                    public void Dispose()
                    {
                        if (serialPort != null)
                            serialPort.Dispose();
                    }
                

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

                上一篇:为什么访问com端口被拒绝? 下一篇:Serial Port ReadLine vs ReadExisting 或如何正确地从串口读取数据

                相关文章

                <tfoot id='jq7UH'></tfoot>

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

                    <legend id='jq7UH'><style id='jq7UH'><dir id='jq7UH'><q id='jq7UH'></q></dir></style></legend>