1

This is my code. When i run this first byte is saved in tempbuf's 0th position. But when i got second byte, the first value is cleared in tempbuf :-(

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    int bytes = serialPort1.BytesToRead;
    byte[] byte_buffer = new byte[200];
    byte[] tempbuf = new byte[200];
    byte[]tempbuf2=new byte[200];
    byte[] ar = new byte[20];
    byte[] ssd = new byte[4];
    byte[] ctrl = new byte[20];

     string pp = "";
     string ll = "";

     serialPort1.Read(byte_buffer, 0, bytes);



     if (i == 0)
     {
        index = byte_buffer[0];
     }
      tempbuf[q++] = byte_buffer[0];
     i++;
     if (i == 200)
     {

      array[index] = TestSerializer.MarshalToStructureArray(tempbuf, tempbuf.Length);
      if (textBox30.InvokeRequired)
      {

     this.Invoke(((MethodInvoker)delegate
      {
       textBox30.Text = index.ToString();
     }));
    }
    i = 0;
    }
   if (q == 200)
    {


   q = 0;
}
3
  • 1
    Learn to format your code please. Commented Aug 29, 2014 at 4:30
  • Where is q defined and what are you using it for? Your variable names are fairly cryptic so it's hard to tell what you're trying to do. Commented Aug 29, 2014 at 5:21
  • I will explain in detail. In This program receiving one byte at a time in byte buffer array. If first byte received is '1',it will be in tempbuf's 'o' th position. And if second byte received is '2',In tempbufs '0'th position must contain '1' & '1'st position contain '2'.'q' is just a variable for Incrimenting the position of tempbuff. Commented Aug 29, 2014 at 5:52

1 Answer 1

1

I solved this problem by declaring tempbuf as static byte array:

static byte[] tempbuf = new byte[200];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.