collinATculture
New member
I was experiencing strange behavior in a project and was able to boil it down into this simplified version to demonstrate the issue. I'm running the code below on a teensy3.6, connected over USB on OSX. I'm flashing with platformIO and also using PIO for serial communications in terminal.
If you send one line containing "Hello World" the program below outputs the expected block of text:
------ Printing Your Message --------
Message Content: Hello World
Delayed 100 microseconds
Delayed 250 microseconds
Delayed 500 microseconds
Delayed 1 milliseconds
Delayed 10 milliseconds
Delayed 100 milliseconds
------ End of Message --------
However, if you copy two or three (or more) lines of "Hello World" from your favorite text editor:
Hello World
Hello World
The first (and only the first) text block often aborts midway through, resulting in output like this:
------ Printing Your Message --------
Message Content: Hello World
Delayed 100 microseconds
Delayed 250 microseconds
Delaye------ Printing Your Message --------
Message Content: Hello World
Delayed 100 microseconds
Delayed 250 microseconds
Delayed 500 microseconds
Delayed 1 milliseconds
Delayed 10 milliseconds
Delayed 100 milliseconds
------ End of Message --------
I've been struggling to understand what can be happening here in such a simple example. I've seen the higher level problem that led me to this boiled-down version on multiple teensy3.6's, including raspberry pi's over USB, and also when using a python sketch to more carefully control whether I'm terminating my messages with \r, \n, or both. Any idea what's going on here? Is there some timing issue that I'm not aware of? Should I be adding some delay between lines from the transmit side? Any help is much appreciated, this has been plaguing me for a couple months now and I keep coming back to it without success (finally decided to post now that I have a simple demo of the issue).
The main.ino file:
Using a platformio.ini file here, in case it matters:
If you send one line containing "Hello World" the program below outputs the expected block of text:
------ Printing Your Message --------
Message Content: Hello World
Delayed 100 microseconds
Delayed 250 microseconds
Delayed 500 microseconds
Delayed 1 milliseconds
Delayed 10 milliseconds
Delayed 100 milliseconds
------ End of Message --------
However, if you copy two or three (or more) lines of "Hello World" from your favorite text editor:
Hello World
Hello World
The first (and only the first) text block often aborts midway through, resulting in output like this:
------ Printing Your Message --------
Message Content: Hello World
Delayed 100 microseconds
Delayed 250 microseconds
Delaye------ Printing Your Message --------
Message Content: Hello World
Delayed 100 microseconds
Delayed 250 microseconds
Delayed 500 microseconds
Delayed 1 milliseconds
Delayed 10 milliseconds
Delayed 100 milliseconds
------ End of Message --------
I've been struggling to understand what can be happening here in such a simple example. I've seen the higher level problem that led me to this boiled-down version on multiple teensy3.6's, including raspberry pi's over USB, and also when using a python sketch to more carefully control whether I'm terminating my messages with \r, \n, or both. Any idea what's going on here? Is there some timing issue that I'm not aware of? Should I be adding some delay between lines from the transmit side? Any help is much appreciated, this has been plaguing me for a couple months now and I keep coming back to it without success (finally decided to post now that I have a simple demo of the issue).
The main.ino file:
Code:
void setup() {
Serial.begin(115200);
}
void loop() {
// Check for new messages
handleMessages();
}
void handleMessages() {
while (Serial.available()) {
// Get a newline-terminated line from USB serial
const int maxMessageLength = 200;
char message[maxMessageLength];
// Line terminator can be \n or \r\n
Serial.readBytesUntil('\n', message, maxMessageLength);
Serial.println("------ Printing Your Message --------");
Serial.print("Message Content: ");
Serial.println(message);
delayMicroseconds(100);
Serial.println("Delayed 100 microseconds");
delayMicroseconds(250);
Serial.println("Delayed 250 microseconds");
delayMicroseconds(500);
Serial.println("Delayed 500 microseconds");
delay(1);
Serial.println("Delayed 1 milliseconds");
delay(10);
Serial.println("Delayed 10 milliseconds");
delay(100);
Serial.println("Delayed 100 milliseconds");
Serial.println("------ End of Message --------");
Serial.println();
}
}
Using a platformio.ini file here, in case it matters:
Code:
[env:teensy36]
platform = teensy
framework = arduino
board = teensy36