10 years, 3 months ago.

Serial output CRLF

When I capture serial output from mbed board with GNU screen, I always get newline only without return: line 1 line 2 line 3 I couldn't figure out how to solve it in screen. But if I change common/retarget.cpp to send \r\n when encountering \n, the output is what I expected.

So the question is: should this be fixed in sender side (retarget), or receiver side (serial capture program)

Thanks, Joey

3 Answers

10 years, 3 months ago.

Most serial programs on your PC you can set if they want only a newline, or if they also want a return to actually return (CR, CR+LF, LF are the options for Teraterm for example).

What I generally do, since by default Teraterm wants \r\n, is give him that. Simply end your printf statements with \r\n and it sends it correctly..

Joey Ye
poster
10 years, 3 months ago.

While printf \r\n does work, it is not the solution I like. \r\n is not a common C programing practise. All C/C++ source code I've written expect \n only.

That is also fine, but you describe exactly why it shouldn't send anything besides \n if you only type \n, most other devices only expect \n, they don't expect \r and \n, so it shouldn't send both unless specifically told to do so.

Then your solution is simply telling your serial client on your PC to expect only \n.

posted by Erik - 05 Dec 2013

Hello Joey Ye,

what solution do you propose? To handle this at the sender side? I like that, if there's like "line discipline", to select what should go to the receiver side. By default, only LF (\n) is supplied, otherwise to each LF add CR (\r\n). What do you say?

Regards,
0xc0170

posted by Martin Kojtal 05 Dec 2013

Hello, Martin,

What's I'm suggesting is 1. printf sends \n 2. whe _write function of retarget sees \n, call serial_putc twice to send \r and \n 3. no change serial sender 4. receiver get \r\n

By doing this, other devices still receive what ever they got before. The only place got impacted is printf like function, which we know that receiver is no other device, but a serial terminal who usually expect \r\n.

Thanks, Joey

posted by Joey Ye 06 Dec 2013
10 years, 3 months ago.

Hello,

I agree, to change the default behavior of _write to add \r. Something I tried to explain in my post above, I failed :-) Your explanation is clear.

Just a question to think about. What if a user want to change it to "as is" (to be only \n)? The only option will be to change the implementation of retarget? Are you going to implement it?

Side note: this was supposed to be a comment, but obviously something went wrong! I even opened a different browser, was not able to send a comment.

Regards,
0xc0170

I'm going to implement it, which is just 2 lines of change.

For those who want only \n, though I doubt anyone would need it, no good solution. For IAR and GCC, the mode parameter of _write isn't available, which could have been helpful to solve it.

Thanks, Joey

posted by Joey Ye 06 Dec 2013

My vote is to leave the code as is. If you use a terminal program like screen that wants /r/n then use it in your printf() statements. If you have a more flexible terminal program (most of the other terminal programs) which can just accept /n then use it instead. I think an embedded framework should be light weight and having such a conversion down in _write() is overkill. I only use \n in my printf() statements since I switched away from screen on my second day of using the mbed.

posted by Adam Green 07 Dec 2013