Serial port communication in System.IO.Ports.SerialPort

P

Pubs

Hi,

I am using the serial and I have a problem with the timeout. I am
defining my array to fixed length and call the read function. I
assumed that the function will not return data until it reads the
number of bytes I requested. My timeout is very large so it will not
timeout. However I noticed that it does return values even before it
gets all the data I requested. I have included some code lines below.

ReadBuffer = new byte[200];
if (ReadPort(ref ReadBuffer))
{
PORT_RESPONS.LastReceivedByte =
ReadBuffer[ReadBuffer.Length-1];
if (ReadBuffer[ReadBuffer.Length - 1] ==
PORT_COMMANDS.CMD_DONE)

In this code I want to read the buffer once it gets 200 data points.
but this code returns the values as soon as the first byte receives
and thus reast of the data in the array shows zero. But If I debug
line by line it shows everything properly.

My external device sends data at 115.2 kbps.

can some one help me in this regard.
 
P

Pubs

Hi,

I am using the serial and I have a problem with the timeout.  I am
defining my array to fixed length and call the read function. I
assumed that the function will not return data until it reads the
number of bytes I requested. My timeout is very large so it will not
timeout. However I noticed that it does return values even before it
gets all the data I requested. I have included some code lines below.

ReadBuffer = new byte[200];
       if (ReadPort(ref ReadBuffer))
               {
                   PORT_RESPONS.LastReceivedByte =
ReadBuffer[ReadBuffer.Length-1];
                  if (ReadBuffer[ReadBuffer.Length - 1] ==
PORT_COMMANDS.CMD_DONE)

In this code I want to read the buffer once it gets 200 data points.
but this code returns the values as soon as the first byte receives
and thus reast of the data in the array shows zero. But If I debug
line by line it shows everything properly.

My external device sends data at 115.2 kbps.

can some one help me in this regard.

I forgot to include the readPort function.
I have folling line inside the ReadPort function.

SerialPort.Read(DataBuf, 0, DataBuf.Length);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top