USB Serial on Teensy 4.0 Buffer size limitation

Status
Not open for further replies.

ElkinDiaz

Member
Dear PJRC community,

I am working on a project that embeds multiple methods, most of them are configurable. This processing methods are used in conjunction of acquired sensor measured data, in order to "adjust" this methods, I would need to transfer the required configuration using the USB port and a PC that is connected to the teensy, the same port used to modify the firmware with the Serial interface (not the Hardware Serial ports)

The first attempt worked properly, I send a .JSON string trough the USB, and using ArduinoJSON library I am able to receive and to parse it properly. My problem comes when this .JSON string gets longer than 150bytes, in such case, the .JSON input its truncated, then deserializeJson function fails, returning the IncompleteInput method. ArduinoJSON support webpage recommended to increase the timeout or increase the buffer size. (Increasing timeouts have not worked)

I have found that there are some methods in Teensy 4 to add buffer memory, but those apply only for the hardware serial ports, not the port I need (USB Serial).

I have also found another web where they modify the Arduino core to increase the buffer size:
https://www.hobbytronics.co.uk/arduino-serial-buffer-size

Could you please suggest a solution, so I can increase the buffer size in the USB Serial port of the Teensy 4.0?
Do you know any alternative lighter to send multiple variables through the USB without usage of JSON format?

Thanks in advance for your collaboration.
 
You should post your code. You can read > 150 bytes into a temporary string and pass this to deserializeJson().
 
Hi jonr,

I am sending the method that fails, as I have previously mentioned I am using the ArduinoJSON library, then the Error I get its managed by their library, and it returns an error of the type
IncompleteInput, and when I print the received String its truncated, it happens when I make the JSON larger than 150 characters.

Code:
void JsonConfiguration()
{
  while ( !Serial.available()  ) {}
  if ( Serial.available() )  payload = Serial.readStringUntil( '\n' );  
  DeserializationError   error = deserializeJson(doc, payload);
  if (error) {
    Serial.println(error.c_str());
    return;
  }
  if (doc["Key"] == "Received") {
    Serial.println("{\"Success\":\"True\"}");
    
  }

  else {
    Serial.println("{\"Success\":\"False\"}");
  }

  delay(20);
}
 
USB Serial buffer size on T_4.0 is not likely the issue. It has some (8?) KB available that is sufficient to handle multiple 512B messages.
 
If you really want to dive into the USB code, look for these lines in {Arduino}/hardware/teensy/avr/cores/teensy4/usb_serial.c

Code:
#define TX_NUM   4
#define TX_SIZE  2048 /* should be a multiple of CDC_TX_SIZE */

These give a default of 8K buffer. But the buffer is in 4 parts, so if the USB code has just recently committed some of the buffer's 2K sections to be accessed by USB, then the amount of buffer memory remaining accessible for Serial.print() may be 6K, 4K, 2K or even zero if the USB host hasn't yet allowed the data to transfer.
 
Status
Not open for further replies.
Back
Top