Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..
I want to receive 200 bytes from serialport.One byte at a atime.And when i receive first byte from the serialport,i want to store it in a byte array's first position.After that when receive second byte,that will store in the second position of the byte array...
Posted
Updated 27-Aug-14 23:38pm
v2
Comments
OriginalGriff 28-Aug-14 5:28am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
I think I understand what you are trying to do, but the way you are trying makes little sense in isolation.
Perhaps if you show us the whole fragment you have already to read the data and store it, and explain what problem that gives (with an example if possible) it would help?
Use the "Improve question" widget to edit your question and provide better information.

That's easy:
C#
private byte[] data = new byte[200];
private int index = 0;
...
   if (index < data.Length)
      {
      data[index++] = recievedByteValue;
      }
 
Share this answer
 
Comments
Member 10994712 29-Aug-14 0:07am    
This is my code.When i run this first byte is saved in tempbuf's 0 th position .But when i got second byte,the first value is cleared:-(

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];
}
// int j;
// byte_buffer.CopyTo(tempbuf,i);
tempbuf[q++] = byte_buffer[0];
// array[index] = TestSerializer.MarshalToStructureArray(tempbuf, tempbuf.Length);
// q++;
i++;
if (i == 200)
{

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

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


q = 0;
}

How can i solve this?
Member 10994712 29-Aug-14 2:42am    
@originalGriff:can u solve this??
I found the solution..I declared byte array as static byte array....
 
Share this answer
 

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