I need to read bytes from Serial, store them in an array and then print them when there's no incoming
bytes.
char inData[20]; // Allocate some space for the string
int inChar; // Where to store the character read
byte charIndex = 0; // Index into array; where to store the character
boolean okPrint;
void setup()
{
Serial.begin(115200);
}
void loop()
{
while(Serial.available() > 0) // Don't read unless
// there you know there is data
{
if(charIndex < 19) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[charIndex] = char(inChar); // Store it
charIndex++; // Increment where to write next
inData[charIndex] = '\0'; // Null terminate the string
}
okPrint == true;
}
// Now do something with the string (but not using ==)
//Serial.println(String(inData)); // Print the string but only once
}
Yes it's possible, but more complicated. Why do you not want to use a character as an end-marker?
if you really don't want to use an end-marker then you need to have a variable that gets updated with the value of millis() as each character is received. If no characters come the value won't change and eventually a test such as
if (millis() - lastCharacterMillis > waitMIllis)
will be true and you can assume you have the whole message.
You will need to choose a value for waitMillis that makes sense for your application.
Robin2:
I am not surprised if there are errors. Perhaps you would be kind enough to point them out for me.
...R
Seriously?
unsigned long waitForCharMillis = 1000; // allow a 1 second gap before giving up
A 4 byte long for a value of 1000 ?
A second of time is ambiguous anyway, one could send hundreds of data sets in that time. The point is to get things done accurately in the shortest possible time, in fact my example of 20 milliseconds is probably way too long as well
char endMarker = '\n';
Well?
while (Serial.available() > 0 && newData == false) {
Most definitely. When I get things wrong I like to correct them.
unsigned long waitForCharMillis = 1000; // allow a 1 second gap before giving up
A 4 byte long for a value of 1000 ?
It avoids confusion and silly and obscure errors if all values associated with millis() are unsigned long
A second of time is ambiguous anyway, one could send hundreds of data sets in that time. The point is to get things done accurately in the shortest possible time, in fact my example of 20 milliseconds is probably way too long as well
I agree that 1 second is a long time. I put that in to provoke a response from the OP who had not explained in sufficient detail what he wanted to happen. And if the person doing the typing was hesitant, you might need 1 second, or even longer.
char endMarker = '\n';
Well?
I agree, that line could have been deleted - but it won't do any harm. It is a hangover from the version recvWithEndMarker().
while (Serial.available() > 0 && newData == false) {
Assumes, bool 'newData' is declared elsewhere?
It is. Just look at my complete example.
Thank you for your interest. I am pleased to see that my code was not "full of errors"
If you are wondering why I structured the examples the way I did, rather than put everything in loop() as you did in Reply #12 then have a look at Planning and Implementing a Program. Even for a small program it is a good idea to use functions. Small programs often expand into bigger programs. And when you have a function that has been tested and is know to work you can just copy and paste it into another program.