简体   繁体   中英

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. 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. But It looks like System.IO.Ports.SerialPort only gives a way to "overwrite" an erroneous byte with the SerialPort.ParityReplace property. 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? 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?

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).

You can laso use CRC Code which is like an extension of the parity check. For example, you send 8 bytes and the ninth byte is the 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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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