Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c设置要读取的字节数阈值_C#_Serial Port - Fatal编程技术网

C# c设置要读取的字节数阈值

C# c设置要读取的字节数阈值,c#,serial-port,C#,Serial Port,设置serialPort1.ReceivedBytestThreshold=64;仅在触发接收的字节事件时生效。现在我正在使用计时器和serialPort1.Readtemp,0,bytestoread;。,我没有使用接收到的字节事件。设置接收字节阈值会影响此操作 private void timer1_Tick(object sender, EventArgs e) //creates a timer tick event to read everything in the port when

设置serialPort1.ReceivedBytestThreshold=64;仅在触发接收的字节事件时生效。现在我正在使用计时器和serialPort1.Readtemp,0,bytestoread;。,我没有使用接收到的字节事件。设置接收字节阈值会影响此操作

private void timer1_Tick(object sender, EventArgs e) //creates a timer tick event to read everything in the port when the timer ticks
    {

        int bytestoread = 0;

        timer1.Stop();
        try
        {
            bytestoread = serialPort1.BytesToRead;
        }
        catch(InvalidOperationException ex)
        {
            //MessageBox.Show("Serial connection lost. Exception types:" + ex.ToString());
        }
        if (serialPort1.IsOpen)
        {
            if(bytestoread != 0)
            {
                byte[] temp = new byte[bytestoread];
                serialPort1.Read(temp, 0, bytestoread);
                if(firstRead && (temp[0] == 0xC0 && temp[1] == 0x04 && temp[2] == 0xC0))
                {
                    serialPort1.Write(packageToSend, 0, packageToSend.Length);
                }
                else
                {
                    tempBuffer.AddRange(temp);
                    firstRead = false;
                    btnIndicator.BackColor = Color.Green;
                }                    
            }
        }
        timer1.Start();
    }

查看您发布的代码,我看不出SerialPort.ReceivedBytestThreshold属性会如何影响您的代码。是.NET库中的文档。它说它在DataReceived事件发生之前控制内部输入缓冲区中的字节数。默认值为1。由于您没有在发布的代码中处理DataReceived事件,因此更改该事件不会对整体执行产生影响


这种情况的用例是,如果您有一个用于此事件的事件处理程序,并且只希望在有一定数量的字节可用时触发它。

您能更具体一些吗?serialPort1是什么类型的?你能发布一个更广泛的代码示例以便我们知道发生了什么吗?serialPort1是一个用于读取dataRight的串行端口,我假设它是SerialPort类型,但你能发布一个更广泛的上下文代码示例吗?明白了。我添加了我正在使用的代码。因此,基本上每隔10毫秒,串行端口中的所有内容都应该被读取。这似乎就是正在发生的事情,一切都像我预期的那样运行。然而,我不知道我是否应该弄乱默认的读取阈值1,或者这甚至会影响代码中的任何事情。我在另一段代码中打开串行部分,并在其中更改默认的读取阈值。