简体   繁体   English

如何检测 SerialPort class C# 中的串行位字节错误

[英]How to detect a serial bit byte error in SerialPort class C#

I am using System.IO.Ports.SerialPort to read data from serial communication.我正在使用 System.IO.Ports.SerialPort 从串行通信中读取数据。 The problem is that I want to determine which byte is bad when I read the byte array buffer and write a file.问题是我想在读取字节数组缓冲区并写入文件时确定哪个字节是坏的。 If I know which byte is bad, then I can recreate the correct file since I know the hash of the file.如果我知道哪个字节是坏的,那么我可以重新创建正确的文件,因为我知道文件的 hash。 But It looks like System.IO.Ports.SerialPort only gives a way to "overwrite" an erroneous byte with the SerialPort.ParityReplace property.但它看起来像 System.IO.Ports.SerialPort 只提供了一种使用 SerialPort.ParityReplace 属性“覆盖”错误字节的方法。 If I'm reading a million byte array, then I don't want to set a bit pattern as a replacement value and then search for this bit pattern in the massive array since I may have lots of matches.如果我正在读取一百万字节的数组,那么我不想设置一个位模式作为替换值,然后在海量数组中搜索这个位模式,因为我可能有很多匹配项。 Is there a way for me to determine which byte failed a parity check when I read the byte buffer?有没有办法让我在读取字节缓冲区时确定哪个字节未通过奇偶校验检查? If not, what would be a better way for me to get parity style error detection while sending a file over serial?如果不是,那么在通过串行发送文件时对我来说获得奇偶校验样式错误检测的更好方法是什么?

The code below is the way I'm currently looking at the serial data, but I'm open to other methods if its faster or gives more reliability.下面的代码是我目前查看串行数据的方式,但如果它更快或提供更高的可靠性,我对其他方法持开放态度。

//... earlier code:
_serialPort.ReadBufferSize = 100000000;
//... more irrelevant code

Thread.Sleep(150000); // wait for 150 seconds for the data to come in.
byte[] HundredKBBuffer = new byte[_serialPort.ReadBufferSize]; // the byte array I'll read from
//read data then discard buffer to get new data from the transmitting machine
_serialPort.Read(HundredKBBuffer, 0, HundredKBBuffer.Length);
_serialPort.DiscardInBuffer();
Console.WriteLine("data received");
//code that reads the byte array, looks for header and trailer and writes file
findHeadAndWriteDataToFile(HundredKBBuffer);

Have you tried reading the data in asynchronously as a stream instead of waiting to get the whole block at once?您是否尝试过以 stream 的形式异步读取数据,而不是一次等待获取整个块? This sounds like it would allow you more opportunities to error check.这听起来会让您有更多机会进行错误检查。

What is the correct way to read a serial port using .NET framework? 使用 .NET 框架读取串口的正确方法是什么?

First idea is to you parity check after each byte, but you could loose speed communication (1 byte of data, 1 byte of parity check).第一个想法是在每个字节之后进行奇偶校验,但您可能会降低通信速度(1 个字节的数据,1 个字节的奇偶校验)。

You can laso use CRC Code which is like an extension of the parity check.您也可以使用 CRC 代码,它类似于奇偶校验的扩展。 For example, you send 8 bytes and the ninth byte is the CRC.例如,您发送 8 个字节,第 9 个字节是 CRC。 This allow you to control datas for a size-specified packet.这允许您控制指定大小的数据包的数据。 The CRC function looks like this (it is the CRC-8 function, but you can use an other one): CRC function 看起来像这样(它是 CRC-8 function,但您可以使用另一个):

private byte CRC8(byte[] Array, int length)
    {
        byte CRC = 0x00;
        int length_buffer = length;
        int length_refreshed = length;
        CRC = Array[0];
        length_refreshed--; ;
        for (; length_refreshed > 0; length_refreshed--)
        {
            CRC = (byte)(((int)CRC) ^ (int)Array[length_buffer - length_refreshed]);
        }

        return CRC;
    }

EDIT Check here for CRC: https://en.wikipedia.org/wiki/Cyclic_redundancy_check编辑在这里检查CRC: https://en.wikipedia.org/wiki/Cyclic_redundancy_check

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM