andydasmith
Active member
Hi, this is my first post, I've been searching all day for an answer to what must be a very simple problem without success so here goes.
I have an open-source arduino project that needs more power so I'm migrating to a Teensy LC.
It involves reading usb serial at 38400 baud from a GPS (five sentences, with around 150 characters in each sentence)
I can do this on the Nano with hardly any overhead so I'm free to read I2C devices and drive a stepper motor at 50 Hz.
When I transfer the code to the Teensy I loose the last two NMEA sentences.
To investigate the problem I threw out all my code and used the code below to run tests:
(its from a well known arduino reference)
const byte numChars = 200;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void setup() {
Serial.begin(38400);
Serial.println("<Arduino is ready>");
}
void loop() {
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
long time1;
void showNewData() {
if (newData == true) {
Serial.println(millis() - time1);
time1 = millis();
Serial.print("This just in ... ");
Serial.println(receivedChars);
newData = false;
}
}
In my application (Cotswold Vario sailplane vertical speed indicator) the Teensy or Nano communicates with an LK8000 open-source Flight navigation system which is able to display the serial output.
When this runs on my LC or 3.1s I see times of around 150 milli-sec to read 150 characters and I loose the last two sentences (same as my code).
When I load the same code on a Nano I see times around 15 milli-sec and I see all five sentences at 1 Hz (GPS outputs 5 sentences each second).
I thought this question must've come up a lot but have searched this forum and the web in vain.
I've picked this up from the PJRC site:
On a Teensy, the entire packet, up to 64 bytes, becomes available all at once. Sketches that do other work while receiving data might depend on slow reception behavior, where successive calls to Serial.available() are very unlikely to return true. On a Teensy receiving large amounts of data, it may be necessary to add a variable to count the number of bytes processed and limit the delay before other important work must be done.
Could this be the problem?
I have an open-source arduino project that needs more power so I'm migrating to a Teensy LC.
It involves reading usb serial at 38400 baud from a GPS (five sentences, with around 150 characters in each sentence)
I can do this on the Nano with hardly any overhead so I'm free to read I2C devices and drive a stepper motor at 50 Hz.
When I transfer the code to the Teensy I loose the last two NMEA sentences.
To investigate the problem I threw out all my code and used the code below to run tests:
(its from a well known arduino reference)
const byte numChars = 200;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void setup() {
Serial.begin(38400);
Serial.println("<Arduino is ready>");
}
void loop() {
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
long time1;
void showNewData() {
if (newData == true) {
Serial.println(millis() - time1);
time1 = millis();
Serial.print("This just in ... ");
Serial.println(receivedChars);
newData = false;
}
}
In my application (Cotswold Vario sailplane vertical speed indicator) the Teensy or Nano communicates with an LK8000 open-source Flight navigation system which is able to display the serial output.
When this runs on my LC or 3.1s I see times of around 150 milli-sec to read 150 characters and I loose the last two sentences (same as my code).
When I load the same code on a Nano I see times around 15 milli-sec and I see all five sentences at 1 Hz (GPS outputs 5 sentences each second).
I thought this question must've come up a lot but have searched this forum and the web in vain.
I've picked this up from the PJRC site:
On a Teensy, the entire packet, up to 64 bytes, becomes available all at once. Sketches that do other work while receiving data might depend on slow reception behavior, where successive calls to Serial.available() are very unlikely to return true. On a Teensy receiving large amounts of data, it may be necessary to add a variable to count the number of bytes processed and limit the delay before other important work must be done.
Could this be the problem?