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

Thread: Serial.print now causes mysterious code malfunctions!

  1. #1
    Member
    Join Date
    Jan 2021
    Location
    London England
    Posts
    23

    Serial.print now causes mysterious code malfunctions!

    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

  2. #2
    Member
    Join Date
    Jan 2021
    Location
    London England
    Posts
    23
    perhaps I should add: - Im using Teensy 4.1 and PlatformIO
    Malcolm

  3. #3
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,429
    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. ...

  4. #4
    Member
    Join Date
    Jan 2021
    Location
    London England
    Posts
    23
    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!

  5. #5
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,429
    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:478528
       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...

  6. #6
    Member
    Join Date
    Jan 2021
    Location
    London England
    Posts
    23
    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 😁👍

Posting Permissions

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