Go Down

Topic: Error in Serial.available() (Read 1 time) previous topic - next topic

liudr

As what AWOL said, a delay is a heuristic solution and is prone to overflow  ;). I would suggest the OP use end-of-message character, such as '\n' to indicate the end of one line of message so arduino will keep reading the serial port (when there is data) and buffer the data in memory until it receives this end-of-message character and then takes the whole string and processes it.

sdwood68

OK, so my fundamental mistake was thinking the serial buffer updated after a carriage return. I think I like the option of looking for a carriage return then just delaying. Someone might be slow at typing. But I should have a timeout so it does not just sit waiting indefinitely.
Code: [Select]

  while (Serial.peek |= /n && Some Timeout Test) {
    newString += Serial.read();
  }


Thanks

sdwood68

I've tried my solution and I'm back to thinking that Serial.available() is not working right.
1) It does appear that it waits for the carriage return before reporting the available charters. i.e. type something into the serial monitor and don't press the carriage return and it will never report any characters available.
2) When polling Serial.available() it always returns 1 character and the the rest
3) Serial.read() never returns a carriage return or line feed. It seems to be stripping them out.

I will look into the the Serial object next to try and confirm these observations.

Any Thought?

AWOL

#8
Jul 17, 2011, 10:12 pm Last Edit: Jul 17, 2011, 10:14 pm by AWOL Reason: 1
Serial monitor doesn't send anything at all until you hit the "send" key.
I don't think the problem is with Serial.available
Remember that serial communications are really, really slow.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Quote
I've tried my solution and I'm back to thinking that Serial.available() is not working right.

Yes, it is.

Quote
1) It does appear that it waits for the carriage return before reporting the available charters. i.e. type something into the serial monitor and don't press the carriage return and it will never report any characters available.

Because the serial monitor doesn't send anything until you hit the send button.

Quote
2) When polling Serial.available() it always returns 1 character and the the rest

Serial.available() returns the number of bytes in the serial buffer. Your code is looping, checking on how many characters are available. As soon as one is, it begins reading them.

Quote
3) Serial.read() never returns a carriage return or line feed. It seems to be stripping them out.

No. The serial monitor doesn't send them, unless you have chosen that option (only available in 0022 and later).

Go Up