Giter VIP home page Giter VIP logo

Comments (4)

mike-matera avatar mike-matera commented on September 28, 2024

What board are you using?

I was not able to reproduce the problem on an Uno. I suspect that you may have an Arduino with an integrated USB controller like an ATMega32u4 that's on the nano and many others. The UARTS on my ATMega328P just blast out the bits whether or not anyone is listening and I don't see any change in cycle time. I measured with millis() as well as looked.

However, if the problem also exists (to a different degree) when using Serial.print() there's probably nothing I can do about it. The printf() function is implemented using Serial.write(). Here's the code that printf uses from ArduinoSTL.cpp

static int arduino_putchar(char c, FILE* f) {
  Stream *uart = __stdio_wrapper.getUart();
  if (c == '\n') uart->write('\r');
  return uart->write(c) == 1? 0 : 1;
}

As you can see I call write for every character --because that's what printf needs-- which isn't particularly efficient. That might explain why printf() has a greater delay.

from arduinostl.

iFreilicht avatar iFreilicht commented on September 28, 2024

Yes that's right, I'm using an Arduino Micro.

The printf() function is implemented using Serial.write().

This explains why it seemed to completely lock the whole device up in my production code (where much more text is being printed).

What seems to work is to count how long a printf takes and if it takes too long restart the program like so:

int tic = millis();
printf("running... ");
if (millis() - tic > 500){
  asm volatile("jmp 0");
}

Thanks for the quick answer, I think you can close this issue as unfixable.

from arduinostl.

mike-matera avatar mike-matera commented on September 28, 2024

This might work too:

https://www.arduino.cc/en/Serial/IfSerial

It looks like you can determine if the serial port is attached. Try this instead:

if (Serial) {
  printf("running...");
}

The documentation doesn't make it clear if this should only be used to time initialization or to more generally detect the presence of a serial monitor.

from arduinostl.

iFreilicht avatar iFreilicht commented on September 28, 2024

Ok I tried this, but it doesn't seem to be working. if(Serial) is still true after the serial Monitor disconnected. Not to worry, thanks for your help!

from arduinostl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.