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

Thread: USB Serial communication problem

  1. #1
    Junior Member
    Join Date
    Jan 2018
    Posts
    4

    USB Serial communication problem

    Hello, I apologize if this has been covered elsewhere but I'm haven't found anything that can shed light on a particular issue I have. I have looked at the https://www.pjrc.com/teensy/td_serial.html page for details but I'm not sure it applies.

    I'm trying to run the following code:

    HTML Code:
    void setup()
    {
            Serial.begin(115200);  // USB serial
    }
    
    unsigned int counter = 0;
    void loop()
    {
            Serial.println(counter++);
    }
    If I upload and run this code, then monitor the serial port I can see the counter will initially increment to around 400, then jumps to 4357, and then suddenly the output hangs. Would appreciate any guidance. I'm using a Teensy 3.2.

    Thanks!

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    18,371
    Add a delay in loop, even just 1ms. Teensy can transmit much faster than Java on some PCs can update the screen.

  3. #3
    Junior Member
    Join Date
    Jan 2018
    Posts
    4
    First, thanks for the fast reply!

    I'm actually not getting anything coming back any more. I tried it with and without the delay and there's simply no output coming from the terminal.

    Some more info that is perhaps useful, I modified the script down to the bare bones and took out the counter and put in a 1 second delay:
    Code:
    void setup()
    {
            Serial.begin(115200);  // USB serial
    }
    
    void loop()
    {
            Serial.println("Testing output");
            delay(1000);
    }
    I'm on Linux, doing screen /dev/ttyACM0 115200 Heres what I'm getting before it hangs:
    Code:
    Testing output
    Testing output
    Testing output
    Testing output
    Testing output
    I'm on
    Linux Natasha 4.13.0-16-generic #19-Ubuntu SMP Wed Oct 11 18:35:14 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

    And here's what I'm getting between uploads in dmesg:
    Code:
    [943840.326662] cdc_acm 2-1.7:1.0: ttyACM0: USB ACM device
    [943842.041759] usb 2-1.7: USB disconnect, device number 30
    [943842.265411] usb 2-1.7: new full-speed USB device number 31 using ehci-pci
    [943842.374632] usb 2-1.7: New USB device found, idVendor=16c0, idProduct=0483
    [943842.374633] usb 2-1.7: New USB device strings: Mfr=1, Product=2, SerialNumber=3
    [943842.374634] usb 2-1.7: Product: USB Serial
    [943842.374635] usb 2-1.7: Manufacturer: Teensyduino
    [943842.374636] usb 2-1.7: SerialNumber: 3764100
    [943842.374922] cdc_acm 2-1.7:1.0: ttyACM0: USB ACM device

  4. #4
    Junior Member
    Join Date
    Jan 2018
    Posts
    4
    Ok, I had a bit more success waiting for to have something connect to the Serial line instead of writing to it right away.

    Code:
    void setup()
    {
            Serial.begin(115200);  // USB serial
            while ( !Serial ); // Can't start I/O without testing this
    }
    
    unsigned int counter = 0;
    void loop()
    {
            Serial.println(counter++);
            delay(5);
    }
    This allows me to get farther with the code (it can count between 38 to about 200 sometimes) before the connection is abruptly terminated. Reconnecting yields a shorter sequence (counts up to 8) before the connection hangs and disconnect/reconnect does nothing. Upon reboot the same behavior can be observed. Changing the baud rate to 9600 allowed me to get up to 1200, it appears to be related to timing. I'll continue tinkering.

  5. #5
    Junior Member
    Join Date
    Jan 2018
    Posts
    4
    After some more tinkering I believe I've found the issue. It seems there needs to be more data written in order for it work without freezing (due to perhaps timing issue?)

    This code seems to work indefinitely:
    Code:
    void setup()
    {
            Serial.begin(115200);  // USB serial
            while ( !Serial );
    }
    
    unsigned int counter = 0;
    void loop()
    {
            Serial.print("This is some long string that gets sent in order to see if it affects timing enough to make a difference ");
            Serial.println(counter++);
            delay(10);
    }
    Also, if I downgrade to 9600, I can bring the delay down to 2ms and it also appears to work indefinitely. But anything slower than 10ms at 115200 baud rate and it will simply hang after a few dozen iterations.

    What could be the reason behind this? Is there a more efficient way to communicate with the board? I intend on having it hooked up to a sensor and process/feed the sensor data to a small linux distro like a Raspberry Pi. Any suggestions are welcome. Thanks.

Posting Permissions

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