Pages: [1]   Go Down
Author Topic: Doesn't serial.available return buffer size?  (Read 725 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've tried to read  the number of bytes in the serial buffer in order to debug my app, and use that value to blink a LED the same number of times. But all I get is two blinks? I'm rather confident that my PC and Arduino do communicate correctly so it can't simply be that the number of recieved bytes aren't right. The code:

if (Serial.available() > 0)
  {
    Flasher(Serial.available()); // This is the interesting call!!
    byte sizeLSB = Serial.read();
    byte sizeMSB = Serial.read();
    int adr = Serial.read();
    
    int size = sizeMSB*256 + sizeLSB;
    
    adr = 0;
    unitAddress = 0;
    
    if (adr == unitAddress)
    {
      while (Serial.available() > 0)
      {
        int cmd = Serial.read();

        switch (cmd)
        {
        case 0x00:
        // No command
          break;
        case 0x01:
        // Set fixture command;
        break;
        case 0x02:
        // Clear fixture command;
        break;
        case 0x55:
          // Ping command
          PongCmd(unitAddress);
          break;
        default:
        // Return command rejected
        RejectCmd(unitAddress);

        }
      }
    }

  }

void Flasher(int noOfTimes) // I only get two flashes here
{
  for (int i = 0; i <= noOfTimes; i++)
  {
  digitalWrite( 13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
  }
}


I've left out some initializing code and stuff...


Regards,
Tomas Sandkvist
Logged

Sussex UK / CT USA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1028
Forums forever
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just a longshot...

You ARE trying to determine how many bytes are in the buffer, yes? Not the SIZE of the buffer?

I didn't follow all of your code, but the Flash function looks okay...

If you're getting two flashes, I'd guess that there two bytes in the buffer at the time you reach the call of Flash.

It might be worth doing a little debug version that tests Flash in more isolated circumstances.

I note you are doing a third Serial.read after the Flash call, when you don't know that there are more than two bytes TO read. (You don't actually test whether there's ONE to read, but Flash seems to say that there is.

You might want to use the fact that Serial.read returns -1 if there is no data TO read at the time Serial.read is called.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Just a longshot...

You ARE trying to determine how many bytes are in the buffer, yes? Not the SIZE of the buffer?

I didn't follow all of your code, but the Flash function looks okay...

If you're getting two flashes, I'd guess that there two bytes in the buffer at the time you reach the call of Flash.

It might be worth doing a little debug version that tests Flash in more isolated circumstances.

I note you are doing a third Serial.read after the Flash call, when you don't know that there are more than two bytes TO read. (You don't actually test whether there's ONE to read, but Flash seems to say that there is.

You might want to use the fact that Serial.read returns -1 if there is no data TO read at the time Serial.read is called.


I'm sending four bytes from my PC app, and I'm pretty certain that it really does, since the part of the program that I was supposed to debug actually does work as assumed. Otherwise I would not have gotten any response as far as I can understand.

For a moment I almost thought that serial.available() returned the number of int:s in the buffer, but I tried sending a lot more bytes, getting the same response, two blinks.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 183
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe you need a delay after the first serial.available? It could be that the buffer isn't completely filled when you get to the next serial.available.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Maybe you need a delay after the first serial.available? It could be that the buffer isn't completely filled when you get to the next serial.available.

Thanks a lot, that solved the problem. Added a 10 ms delay, now the count is correct!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe it might be a better idea to read the available date before calling a function which delay the processing
1 second? If there is a buffering, waht shells, but when not - it should help.

imho smiley


Logged

Pages: [1]   Go Up
Jump to: