Read .txt file sent from PC

Status
Not open for further replies.
Good morning!

Sorry I didn't answer this weekend....

So I tried your example and it keeps only printing the last line.... I didn't gave any thoughts to it this weekend, but today I'm going to try again. If you guys have any other idea, I apreciate your help. If I find a resolution I'll share it too.

This is what I get:

send: 101242
aaaaaaaaaaaaaaab

send: 101244
aaaaaaaaaaaaaaab

send: 101246
aaaaaaaaaaaaaaab

send: 101248
aaaaaaaaaaaaaaab



Thanks,
Jonathan
 
use this:
Code:
void setup() {}

void readAndPrintLine() {
 static char buf[80];
 static unsigned idx = 0;

 while (Serial.available())
 {  
  char c = Serial.read();
  buf[idx] = c;
  idx++;
  buf[idx] = '\0'; 
  if ( (c == '\n') || (idx >= sizeof(buf) -1) ) {    
    Serial.print( buf );
    idx = 0;
  }
 }
}

void loop() {
  readAndPrintLine();
}
 
Thanks! Tomorrow I will try it, cause right now I'm already in bed :)

But will it actually work, if I have already the entire file sent to the teensy?
 
In this part you are not checking for buffer overflow
Which can corrupt any data that is alligned after the buf
Code:
buf[idx] = c;
  idx++;
  buf[idx] = '\0'; // here is no check if the buff gets overflowing

And by the way the only thing that is needed is to increase the buffer size from 18 to for example 40
 
In this part you are not checking for buffer overflow
Which can corrupt any data that is alligned after the buf
Code:
buf[idx] = c;
  idx++;
  buf[idx] = '\0'; // here is no check if the buff gets overflowing

And by the way the only thing that is needed is to increase the buffer size from 18 to for example 40

That happens in the next line.. idx can't reach a value that would cause an overflow.
 
That happens in the next line.. idx can't reach a value that would cause an overflow.
Yes you are right, I was thinking wrong.

And actually it's smart to "append" the null char after every receive.

I don't know if there is any use in this case?
The only thing I can think of is when having some
receive timeout functionality.


And I don't think the > sign is needed
as the receive is very controlled
and the idx is only incremented once in the loop

it would have been sufficient to only use ==
Code:
(idx == sizeof(buf) -1) )

Nope, it would still crash if a line is longer, by accident.
That's not a good coding style.
That is not true as the original code was taking that into account(unless MAX_INPUT is bigger than 1000)
"crash" is a strong word in this case.


I had a long description here to describe all the problems
but then it hit me.

the post contains no notice about
MAX_INPUT
and the code contains no trace of it.

so I can not post that until we know what value that is set to


the following is a little overkill for this application (specially at this state)
char buffer[1000]; //buffer for serial data receive from PC


simple and last
"Forum Rule: Always post complete source code & details to reproduce any issue!"
 
Last edited:
Yes, "==" would be enough, but for the resulting assembler code it makes absolutely no difference or takes longer time. It is just an other opcode.
And its usual to use >= for such cases.
 
Status
Not open for further replies.
Back
Top