Serial.print now causes mysterious code malfunctions!

Status
Not open for further replies.

MMESSITER

Active member
Hello

While continuing to develop my lock-down radio control project (which is now quite large) I've noticed that using Serial.print or Serial.println now causes mysterious malfunctioning of my code.
This of course makes debugging harder!
How can I fix this ? I hope someone knows!!
Thanks in advance !

Malcolm Messiter
 
Really hard to say without additional information. And often times the answer is: It depends....
And saying it malfunctions does not give any clues. I am assuming when you say Serial.print that is literal so USB not Hardware Serail

But most of the time I have had issues, it is due to changing the timing of the code.

Things like: you have some timing loop going on and your printing during some of the time works, but not others... It may depend on if the code can write your complete output into buffers or not or if the code doing the print has to wait for room in the buffer to output before returning... You can avoid this sometimes by calling the Serial.availableForWrite() method and not output if not enough room.

Sometimes it is an issue that you are trying to do a print from an Interrupt. Like IntervalTimer ... or some other and if for example your interrupt is at a higher priority than the one servicing the USB can be issues.

Sometimes it can be again USB full and you are doing a print with interrupts disabled. ...
 
Thanks! I’m not serial printing from an interrupt handler - it is indeed the usb serial device - the Teensy is also doing other serial outputs to a Nextion screen (on serial3). The misbehaving looks to me like a memory overrun because some text data acquires random characters. I’m not using the “String” functions anywhere as I know they’re not reliable. The debug data sent to the usb serial is tiny so I think it’s unlikely to be a full buffer. I could send the source but it is about 5000 lines now!
 
Maybe you are overwriting memory... Sometimes when you have stuff that for example writes outside of array boundaries, may seem to work OK and then you add code which realigns stuff in memory and causes issues.

Other times adding the additional code, causes the code and/or data to get bigger, such that for example maybe the stack collides with the data areas...

What does the build tell you about how much space is used and/or free.

Note: I don't use platformIO so don't know if they are outputting yet the same output we do in Arduino builds...

That is with last build I did:
Code:
Memory Usage on Teensy 4.0:
  FLASH: code:27204, data:5896, headers:8880   free for files:1989636
   RAM1: variables:12992, code:23896, padding:8872   free for local variables:[COLOR="#FF0000"]478528[/COLOR]
   RAM2: variables:5216  free for malloc/new:519072
You can see I have a lot of space free for the Stack (local variable).

So I would look at things like that. Also look if you have large things that are local on stack...
 
Thank you! I’m deeply ashamed! I have just found and fixed the error. It was mine. While setting up the transmitter, but not yet connected to a receiver, my code was trying and retrying to send data and generally getting its knickers in a twist. I just added a few lines to skip that bit during setup. Now it’s working fine in all respects ! Even Serial.print works well 😁👍
 
Status
Not open for further replies.
Back
Top