Question about an example from the "Using USB Serial Communication" help page

Status
Not open for further replies.

amundsen

Well-known member
Question about an example from the "Using USB Serial Communication" help page

Hello,

I don't understand why there is a sync function included in the code below, from the Using USB Serial Communication page. Is it required for the data reception?
Code:
// This will always work on Teensy, does not depend on buffer size
boolean getNumbersFromSerial() {
  int count=0;
  char buf[11];
  while (count < 11) {
    if (Serial.available()) {  // receive all 11 bytes into "buf"
      buf[count++] = Serial.read();
    }
  }
  if (buf[0] == '@') {     
    time_t pctime = 0;
    for(int i=1; i < 11; i++) {   
      char c = buf[i];    
      if (c >= '0' && c <= '9') {   
        pctime = (10 * pctime) + (c - '0') ; // convert digits to a number    
      }
    }
    pctime += 10;   
    DateTime.sync(pctime);   // Sync clock to the time received
    return true;   // return true if time message received
  }
  return false;  //if no message return false
}
 
The intention of the function is to synchronise the Teensy with the host PC, and it also made a handy example for the USB process, but it is not central to it. If your use case is not trying to recieve a time update then you would want to get ride of that element and parse out the value for whatever you are doing.
 
Status
Not open for further replies.
Back
Top