Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: USB Serial on Teensy 4.0 Buffer size limitation

  1. #1

    USB Serial on Teensy 4.0 Buffer size limitation

    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/ardui...al-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.

  2. #2
    Senior Member
    Join Date
    May 2015
    Location
    USA
    Posts
    1,057
    You should post your code. You can read > 150 bytes into a temporary string and pass this to deserializeJson().

  3. #3
    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);
    }

  4. #4
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    14,871
    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.

  5. #5
    Senior Member
    Join Date
    May 2015
    Location
    USA
    Posts
    1,057
    You could print out the String just after you read it in (before parsing). Copy the output into https://jsonlint.com/ to check it. Also note that the library does have a nesting limit.

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,784
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •