
Originally Posted by
PaulStoffregen
Also, and hopefully this isn't too "obvious", the default Serial1 receive buffer is only 64 bytes. Unless you've used attachRts() and whatever is sending the data stops when RTS signals Teensy's buffer is nearly full, you can pretty easily get a buffer full situation if detecting the SD card takes 1 second and data arrives at any reasonably fast baud rate during that time. Other than RTS/CTS flow control, the other solution is to expand the buffer with addMemoryForRead().
Keep in mind USB "Serial" is completely different from hardware "Serial1" and "Serial2". The USB protocol has flow control built in. It's always active and can't be shut off or ignored like RTS/CTS. If your PC (or Mac) tries to send more data during a time when Teensy is busy and your program isn't calling Serial.read(), the data remains in buffers on your computer until Teensy is able to receive it.
But with non-USB real hardware serial, unless you're using RTS/CTS flow control, if bytes arrive when Teensy's buffer is already full, they have to be discarded. Only proper use of RTS/CTS flow control can make the transmitter wait, as happens automatically with USB serial.
If that doesn't solve your problem, if you're convinced there is a bug in Teensy's Serial1 or Serial2 implementation, then you need to craft a small & simple test program to demonstrate the problem. I'm not necessarily saying Teensy's code is 100% bug free. It might be a bug. But a reasonable small test program is required before any investigation will begin.
I did craft what you asked for - see other post from Saturday - anything else you need?
I'm using Arduino 1.8.19, and Teensyduino your latest (Dec 2021 ish).
I've now further reduced the number of code lines for an example that reproduces the issue using just a Teensy 4, a uSD card, and Monitor. In Monitor, using two hands, type on the PC keyboard just one letter and enter to create a stream of >>1 characters per second. With the (exFAT) SD card in, it responds immediately to any keystroke. Without the SD card in, it blocks reception for one full second. But why? And can it be fixed?
So it's just this code, only 71 lines:
Code:
#include <SdFat.h>
#include <SdFatConfig.h>
#include <sdios.h>
#include <TimeLib.h>
SdFs sd;
FsFile txtFile;
IntervalTimer myTimer_100ms;
volatile int Count_100ms_ticks = 0;
volatile int Serial_chars_received = 0;
volatile char last_char_from_Serial = 0;
void setup()
{
Serial.begin(115200);
while (!Serial)
delay(10);
Serial.println ("Starting 100ms timer");
myTimer_100ms.begin(timerAction_100ms, 100000); // timerAction to run every 0.1 seconds
myTimer_100ms.priority (3*16); // lower number = higher priority
}
void timerAction_100ms ()
{
char st[256] = "";
sprintf (st, "Hello again #100msticks=%d #Serial_chars=%d last_char = %c", Count_100ms_ticks, Serial_chars_received, last_char_from_Serial);
Serial.println(st);
Count_100ms_ticks++;
}
void serialEvent()
{
while (Serial.available())
{
last_char_from_Serial = Serial.read();
Serial_chars_received++;
Serial.write (last_char_from_Serial);
}
}
void loop ()
{
char str[100];
strcpy (str, "test.log");
char log_filename[100];
strcpy (log_filename, "test.log");
if (sd.begin(SdioConfig(FIFO_SDIO)))
{
txtFile = sd.open(log_filename, FILE_WRITE);
if (txtFile)
{
txtFile.seek(EOF);
txtFile.write (str, strlen(str));
}
txtFile.close();
sd.end();
}
delay(100);
}