Page 1 of 1

SDRead function

Posted: Fri Jan 21, 2011 10:36 am
by Saran_yim
I'm using STM8 with SD function to send command to another MCU (also STM8) and I wait for response

So I made change on Serial buffer to 64 and I have my arrry buffer (msg_in[80])

my code is like this

Code: Select all

clearMSG_in();
      sdWrite(&SD2,"2A0\r",4);
      
      sdReadTimeout(&SD2,msg_in,64,500);
      if(!strcmp(msg_in,"2A\r")){
         return DONESUCCESS;
      }
      else{
         chThdSleepMilliseconds(50);
      }


The sending is working perfect but in reading I got all the previous data left in sd buffer and I have problem to check the input.
So there are any solutions to solve this?

Thank you and Regards,

Saran

Re: SDRead function

Posted: Fri Jan 21, 2011 12:11 pm
by Giovanni
Hi Saran,

It is possible that the sdReadTimeout() goes in timeout before the data has been read, if this happens it does not overwrite the buffer content.

You should check the return value of sdReadTimeout() in order to understand how many bytes it really read, the effective amount can be less of the amount you requested if a timeout happened.

If you want to make sure that sdReadTimeout() always read the requested amount then you should specify TIME_INFINITE as timeout or use sdRead().

Giovanni

Re: SDRead function

Posted: Sat Jan 22, 2011 11:16 am
by Saran_yim
Hi Giovanni

I try to use SDRead instead of SDReadTimedOut but the result is the same.

The point is if everything working perfect I will get the correct answer since the first time but in some case it look like there are something left in sd's buffer

I expected to get 3 bytes response and I see the correct response on PC (monitoring the communication) but response from SDRead is not the one that it should be.

for example I send command 2A0\r and expected to have 2A\r (3 bytes) response but in some case I get the first time XXX\r (4 bytes of anything) that from some communication before sending this command then
SDRead (&SD2,msg_in,3) will get xxx
then second time I get \r2A (\r from the previous command) instead of 2A\r and continue happen like this.

So is there anyway to check how many data in buffer or clear SD's buffer

Thank you

Saran

Re: SDRead function

Posted: Sat Jan 22, 2011 11:44 am
by Giovanni
Hi Saran,

You can read how many bytes there are in the input queue by using the macro chQSpaceI().

Anyway, I think there is a better approach, you are using the charachter "\r" as delimiter, it would be better to read by lines rather that by number of bytes.

You may create a function like this one:

Code: Select all

size_t sdGetLine(SerialDriver *sdp, uint_t *buf) {
  size_t n;
  uint8_t c;

  n = 0;
  do {
    c = sdGet(sdp);
    *buf++ = c;
    n++;
  } while (c != '\r');
  *buf = 0;
  return n;
}


That reads the data using sdGet() until it reach the "\r" and then returns. By reading until the delimiter you would never lose synchronization with the input data. The function could be improved by adding a "max" parameter with the maximum number of bytes to read in order to prevent possible buffer overflows.

Giovanni