Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
The program below compiles and executes without any error and also shows all the data returned by serial port.

But i know that SerialDataReceivedEventHandler event get called multiple times and each time it gets called it brings chunk of data.

I just want to store that chunk of data for each SerialDataReceivedEvent separately,so to process it,parse it,decode it.

can anyone just go through the code and tell me what should i do?

What I have tried:

public class DataReceiveHandle
  {
      public static int MAX_PACKET_LENGTH = ChannelDataCount.count;

      public bool newData = false;

      public int rxOffset = 0;

      public byte[] rxBuffer = new byte[MAX_PACKET_LENGTH];

      public byte[] rxPackage = new byte[MAX_PACKET_LENGTH];

      public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
      {

         // Console.WriteLine("In Data receive ");
          if (e.EventType != SerialData.Chars) return;

          SerialPort port = (SerialPort)sender;


          int bytesCount = port.BytesToRead;
          port.Read(rxBuffer, rxOffset, bytesCount);

          rxOffset = rxOffset + bytesCount;

          if (HasCompletePackage())
          {
              Console.WriteLine("Full packet has been received");
              rxBuffer.CopyTo(rxPackage, 0);
              rxOffset = 0;

              newData = true;
          }

          if (newData == true)
          {
              if (rxPackage[0] == 224
                  && rxPackage[1] == 242
                  && rxPackage[2] == 1
                  && rxPackage[3] == 0
                  && rxPackage[4] == 60
                  && rxPackage[5] == 37
                  && rxPackage[6] == 132
                  && rxPackage[7] == 0
                  && rxPackage[8] == 0)
              {
                  Console.WriteLine("Header Content is:");
                  for (int i = 0; i <= 8; i++)
                  {
                      Console.WriteLine(rxPackage[i]);
                  }
              }

              Console.WriteLine("Message Content is: ");

              for (int i = 9; i < MAX_PACKET_LENGTH; i++)
              {
                  Console.WriteLine(rxPackage[i]);
              }

          }


      }
      private bool HasCompletePackage()
      {
          if (rxOffset == 60)
              return true;
          else
              return false;
      }
  }
Posted
Comments
Jochen Arndt 4-Jun-18 8:11am    
The intention of my answer to your previous question was that the handling of complete packages is done by the main thread. That must be informed when a new package is available and can then get a copy of the data which includes clearing newData afterwards.

You can even omit the usage of the newData variable when using an appropriate signaling method or message method that includes the data. Read about Invoke / Delegate.
Nishikant Tayade 5-Jun-18 0:01am    
@JochenArndt Thanks,I got it.Is there any way of using circular buffer or dynamic array to store the the data that is sent by serial port at a time and then parse what is available in the buffer and then again put data into it and so on.
Nishikant Tayade 4-Jun-18 8:39am    
@JochenArndt I understood what you said,but though I can get whole data and then do processing on it.What i want to do is get that chunk of data and process it.
Jochen Arndt 5-Jun-18 4:43am    
Detect and process complete packages. Why do you want to process partial data when you have to do the final processing after the package is complete anyway?
Nishikant Tayade 5-Jun-18 4:55am    
As the data receive event get fired multiple times it brings that chunk of data,so instead of waiting till full data is received(which may be time costly)
I want to check what is the data send by serial port and start working on only that limited data.
That's why i am asking about circular buffer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900