Can I look at last byte in serial buffer?

Status
Not open for further replies.

TeensyWolf

Well-known member
T4.1 project here.

I'm expecting a serial packet, packet length can vary, and can be > 100 bytes (will have to adjust buffer size).

I'm polling, so I don't want to process the packet until I receive all the bytes. The 2 last bytes are 0x0D 0x0A.

Can I query the buffer to look at the last byte or two?

Or should I be loading the incoming bytes into a separate array, then I can index those bytes?

Thanks.
 
Serial.peek() ??

Sorry that's the first, you probably have to read them as they come in to a buffer and process the when them end markers arrive
 
use Circular_Buffer to queue serial bytes, you can easily peek() into the queue at any position:

buffer.peek(7) // checks the 8th byte in the queue
 
Your can use a simple buffer as in this post (#29)
https://forum.pjrc.com/threads/66086-Read-txt-file-sent-from-PC?p=268825&viewfull=1#post268825
And when you have received 0x0a (LF or '\n')
You just check the last byte like 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) ) {    
    if (buf[idx-3] == charToLookFor) {
  
    }
    idx = 0;
  }
 }
}

void loop() {
  readAndPrintLine();
}
 
Status
Not open for further replies.
Back
Top