Skip to content

Serial.print bug ? #6293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Testato opened this issue May 16, 2017 · 4 comments
Closed

Serial.print bug ? #6293

Testato opened this issue May 16, 2017 · 4 comments

Comments

@Testato
Copy link

Testato commented May 16, 2017

this code work only for an half,
it print only 12

void setup() {
Serial.begin(9600);
}
void loop() {
  Serial.print(1234);
  exit(0);    
}
@facchinm
Copy link
Member

The report is missing "some" information... If the bug report refers to a UNO, it's quite normal that not all bytes are being written, since the 16u2 changes its behaviour based on the status of the 328 (and probably changes baud rate, too, I have no time to check).
However, adding a small delay between the print and exit should solve the issue.
Reopen if needed

@bengtmartensson
Copy link

Just two comments:

  1. Normal programming practice says that you cannot rely everything to be output until you flush or close the buffer/stream. It appears that Serial.end should be used to close the stream.

  2. exit is meaningless in the Arduino context.

@matthijskooijman
Copy link
Collaborator

Actually, I think exit() might be calling abort(), which IIRC disables interrupts and then starts looping infinitely. Because interrupts are disabled, serial output stops. If you insist on using exit(), make sure to call Serial.flush() to write all serial output first.

@MauroMombelli
Copy link

MauroMombelli commented May 16, 2017

correct answer is then:

  1. Normal programming practice says that you cannot rely everything to be output until you flush or close the buffer/stream. It appears that Serial.end should be used to close the stream.

  2. exit() [...] disables interrupts and then starts looping infinitely. Because interrupts are disabled, serial output stops.

This happen because the Serial library is asynchronous, in this particular case reeling on the interrupt to put the next byte in the output buffer in the register that will take care of the communication.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants