Q about reading multi char string and use of delay

Hi guys.

I am writing a program in Arduino which can read a number(we do not know how many digit it is, e.g. 245) from Serial port. I thought of the following code which does not need any terminating character(255#). The code logic works just fine but if I remove the debugging Serial.println() command the code will not work and I will get the numbers separates(255 would read in a total mess). Another alternative is to add a little delay to the code like what I have in line 7. Do I do something wrong or Arduino serial communication is just like this ? :o I do start the serial.begin on 9600

while ( Serial.available() > 0 ){
  temp *= 10;
  readPwm = Serial.read();
  readPwm = readPwm - 48;
  temp += readPwm * 10;
  //Serial.print("debug info ***** "); //??? doesn't work without this
  delay(10);
  //Serial.println(temp); 
}

You multiply temp by 10 whenever there is another character to read. Then you add the digit read, after multiplying it by 10. You should not be multiplying the digit read by 10.

Serial data transmission is slow. The Serial.print statement takes time, which allows time for more data to arrive.

You have two choices. Add a delay statement to the while loop, and hope all the data arrives in time is the first choice. The longer you waste time waiting for data, the better your chances of it all arriving in time.

The second choice is to add an end-of-packet marker, and keep appending to temp until that end of packet marker arrives. Only use the data in temp when the end of packet marker has arrived.

The delay masks the issue of not being able to read a complete packet in one pass. If the delay is too small, the whole packet will not arrive before the delay(s) expire. If the delay is too large, you are just wasting time.

The better approach is to add an end of packet marker to the packer, wo you can read the packet as quickly as possible..