USB Serial communication problem

Status
Not open for further replies.

badiozam

New member
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:
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!
 
Add a delay in loop, even just 1ms. Teensy can transmit much faster than Java on some PCs can update the screen.
 
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
 
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.
 
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.
 
Status
Not open for further replies.
Back
Top