If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Debugging with the serial port, issues..

I put Serial.println("bla bla"); lines all over in my code trying to trace my program flow. I'm seeing them get skipped over. Is this a known issue? If so is there a way to force them to block 'till they output their data?

I'm sorry, I don't have a simple case to show. But I figured I wouldn't be the first to hit this, so I'm asking.

Printing too much too often from Teensy ( not noted which one ? ) can easily overwhelm the USB transfer to PC and it ability to process and display the data.

Add a way to regulate the amount printed - don't print if unchanged, or print a simple single character signifier.

Perhaps make a function() to call with your strings and have that function do all the printing and using doc here : https://www.pjrc.com/teensy/td_serial.html do Serial.send_now() or .flush() in some controlled fashion to pause at times, perhaps a bool to the function() to say when to wait.

Including any sort of example of runnable code that exhibits the issue allows for better understanding and feedback

If you print from both main program and interrupts, or from multiple interrupts running with different priority levels, the Serial.print code is *not* safe to use. Usually it will work anyway, where data loss is usually the worst problem. Lockups or really bad problems are extremely rare. But if the interrupts happen at just the wrong moment while a main program or lower priority interrupt is doing Serial.print, you can get data loss when the interrupt changes variables that were in use by the main program. It's a known issue for that use case.

Many times I've considered trying to make it safe for interrupts and main program. But doing so could impose a pretty substantial interrupt blocking time on all uses. It's also a pretty low priority compared to so many other things...

However, the is safe to use Serial.print within interrupts if all the interrupts using it run at the same priority. It's also safe if the interrupt never occurs while the other uses are in progress.

There may also be issues for causes with large bidirectional data flow, where fast incoming data and high-latency reading by user code starves the rest of the USB stack for buffers.

Of course, something in your code could be causing trouble. A very common issue we see on this forum is a buffer overflow, like writing 1 place past the end of an array (usually due to a "<=" check which should have been "<"). Corrupting the memory causes problems later which appear to be unrelated bugs when other code runs needing that memory.

Could be other stuff too. Nobody can even begin to guess without seeing your code. But here's an informal list covering pretty much all the scenarios we usually see on this forum.

It looked like I was just overrunning the output buffer. I was doing something like a stack crawl by having a serial output in each function both in and out. It would work if I had a better understanding on how to make the serial prints block. It did work after a fashion, and was able to pinpoint where I screwed up my code. But it was iffy. Sometimes I'd only get lie 20% or so. At first this totally freaked me out because I thought I was crashing only about 20% into my code and those places were in tried and true libraries.

As for interrupts, I almost never use them. So that's not really an issue.

Anyway, after I found the last issue and turned off the stack crawl, everything works fine.

I read the stuff on the last link, but I didn't really get a definite answer from it. How to make a print() be blocking or does flush() really work and how?

Thanks agin for replying. It's really good to know there's people out here that can re-point me in the right direction.

Great you made headway. Teensy USB is true 12 Mbit hardware not 'serial' gated by a baud rate through a secondary chip to PC.

That page link to the .flush() and .send_now() should be a way to force a flush or wait at key points. You could drop one of those in loop() perhaps or in at the point loss was showing after over writing.

Without any example code it isn't even clear on which teensy and which Serial or Serial# the output is going so providing example fixes or better ideas is beyond speculation.

All of that can affect program flow and speed and with no idea of the process underway that leaves it up to best guess on your end.

I've posted initial start of debug_t3 lib a few times - it allows recording program flow in some fashion in an internal array that can be exposed as desired to minimize printing until needed or triggered - and it pops up if the processor ever faults.