简体   繁体   中英

Read bytes with 921600 baud C#

I have to receive and process arrays of bytes with a bitrate of 921600 in real time via RS-422. I process about 3500 bytes at a time and my problem is that I lose about 2800 bytes later.

I use: client.OnReceiving += new EventHandler<DataStreamEventArgs>(receiveHandler);

void receiveHandler(object sender, DataStreamEventArgs e)
{
  ReadData(e.Response);
}

void ReadData (byte[] byteBuffer)
{
  if (byteBuffer != null)
  {
    for (int i = 0; i < byteBuffer.Length; i++)
    {
      if (i < byteBuffer.Length-1 && byteBuffer[i] == 0xAA && byteBuffer[++i] == 0xBA)
      {
        results = new byte[138];
        Buffer.BlockCopy(byteBuffer, i + 1, results, 0, results.Length);
        byte[] h = { results[results.Length - 2], results[results.Length - 1] };
        ControlSum = (ushort)BitConverter.ToInt16(h, 0);
        results = results.Take(results.Count() - 2).ToArray();
        ushort u = checkSum.CRC_Calc(results);
        PackCounterForAll++;
        if (ControlSum == u)
        {
          int offset = 0;
          @float = results.Skip(8)
            .ToArray()
            .GroupBy(x => offset++ / 4)
            .Select(x => BitConverter.ToSingle(x.ToArray(), 0))
            .ToArray();
          PackCounter++;
        }
      }
    }
  }
}

First, I have to find the two start bytes, write a 138-byte packet, and calculate its checksum. As far as I understand, I process the first bytes that come to me through:

    public int Receive(byte[] bytes, int offset, int count)
    {
        int readBytes = 0;

        if (count > 0)
        {
            readBytes = _serialPort.Read(bytes, offset, count);
        }

        return readBytes;
    }

I process them and after that I start to lose them. How can I fix this?

It depends on your situation and wish. The easiest would be to use some flow control signals such as RTS / CTS to signal your sending endpoint that you cannot process the data for a brief moment. Depending on your connection you can do that using hardware:

Hardware flow control uses RS-232's RTS and CTS signals to indicate when data transmission should be paused or re-started. For example, as indicated in the figure, when PC1 is ready to receive, it raises the RTS signal to request data from PC2

or using software (if you have no hardware wires connected or no control over the hardware port):

Software flow control works by sending an XON/XOFF signal through the data channels. For example, as indicated in the following figure, PC2 sends an XON pattern when it is ready to receive, and then when its Rx buffer is almost full, it sends an XOFF pattern to request that PC1 stop transmitting

A quick search revealed this PDF for you explaining the topic but the inte.net is full of it (just google flow control RS-244):

https://www.moxa.si/Title_Pages/Basics_of_RS232-422-485.pdf

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