15

I am trying desperately to get a Bluetooth dongle working with my Arduino but I can't send it a command that it needs. I can use it when I plug it into my computer via a USB to UART chip and send the command (C) from PuTTY and then press Enter.

The Bluetooth dongle's command sheet says that the command I am trying to send it C<cr> but I can't figure out how to send the proper carriage return character from the Arduino code. I have tried using the Serial.println() function as well as adding the \r character to my current Serial.write("C\r") but neither of those are working.

How can I achieve this?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Sponge Bob
  • 1,551
  • 5
  • 17
  • 23
  • 1
    Check your Putty terminal settings to see what it's set up to send; you can configure it to send whatever line endings you want. – Wooble Aug 17 '12 at 00:04
  • 1
    @Wooble Where is the "PuTTY Reconfiguration" would I be able to find that? – Sponge Bob Aug 17 '12 at 00:07

9 Answers9

13

Interestingly, I can report the opposite on Win 7: PuTTY for me and my embedded project is sending ONLY \r over the COM port. Curious, read: frustratingly unexplainable, but I simply look for either character on the other end of the serial connection.

Then, if you enable "Implicit LF in every CR" under Terminal options it will send both \r\n. Default behaviour seems to be akin to a Commodore machine :D (http://en.wikipedia.org/wiki/Newline). who knew...

Mohammad Kholghi
  • 533
  • 2
  • 7
  • 21
Sowka
  • 195
  • 1
  • 3
  • Just ran into this, thanks for mentioning the implicit LF setting. – Daniel Buckmaster Oct 11 '13 at 22:09
  • 1
    I just ran into this too. I'm really glad I checked first before hooking it to my PIC; as everyone else's post shows, this is quite unexpected. My scope clearly shows that Putty's default is to send \r only. This is not documented. – carveone Jul 26 '14 at 13:08
  • @carveone, I found the docs [here](http://the.earth.li/~sgtatham/putty/0.63/htmldoc/Chapter4.html#config-telnet), see sec. 4.6.14:
    By default, PuTTY sends the Telnet New Line code when you press Return, instead of **sending Control-M as it does in most other protocols.**
    – tsul Nov 18 '15 at 15:35
  • But that should just apply to telnet connections rather than serial ones surely. But this whole thing with \r does explain why I've seen protocols that end in \r rather than \n. It does show that one must be very careful not to assume anything about anything :-) The esp8266 dongle wants both cr-lf and you have to use enter and also ctrl-J. If you google that, you'll see the number of people who've been caught out by that. Enter is CR, not LF... – carveone Nov 18 '15 at 19:45
  • 9
    No, 'Implicit LF in every CR' doesn't do that. There is no option in original PuTTY to send LF when Enter is pressed. – grzegorz Sep 05 '18 at 20:55
10

Sending CR+LF is possible in modified PuTTY. Source code is available at https://github.com/gniemirowski/putty-crlf and Windows binary at https://www.grzegorz.net/pliki/putty-crlf.zip When you run this version just go to Terminal -> Keyboard and select "CR LF" for "The Enter key" option.

enter image description here

grzegorz
  • 331
  • 3
  • 16
8

PuTTY emulates xterm which emulates vt100. To have putty send CR/LF when pressing enter, type ESC[20h in putty after connecting to the serial device. This sets VT100 LNM true.

http://vt100.net/docs/vt100-ug/chapter3.html

Line feed/new line New line ESC [20h Line feed ESC [20l

bubbasnmp
  • 81
  • 1
  • 1
2

If you watch the ascii table or similar reference you might find interesting: \r ou \x0D

For better understanding, see : http://www.grok2.com/sedfaq6.html

2

The modified PuTTY is the easiest solution. If you want to stick with the standard PuTTY, there's some other options... You can send a newline using ctrl+j before pressing enter, but that's a faff. To automate it, you can use AutoHotKey to change your {ENTER} to ^J{ENTER} when you've got a PuTTY window active:

#if WinActive("ahk_exe putty.exe")
    Enter::
        SendInput ^J{Enter}
    Return
#if

To do this for just one PuTTY window, you can give AHK the name of the window:

#if WinActive("COM8 - PuTTY")
    Enter::
        SendInput ^J{Enter}
    Return
#if
vmTim
  • 59
  • 2
1

In standard configuration (on Windows and Linux) if you type "help" and then press enter, the following chain of bytes will appear on the serial port (checked with external connected terminal via RS232, and logic analyzer):

0x68(h) 0x65(e) 0x6c(l) 0x70(p) 0x0d(CR: Carriage Return U+000D)

So it seems like PUTTY puts CR on ENTER (no matter if you are on Linux or Windows).

wovano
  • 4,543
  • 5
  • 22
  • 49
Mazeryt
  • 855
  • 1
  • 18
  • 37
1

On arduino program, just use Serial.write and both characters codes:

Serial.write(13);    // CR
Serial.write(10);    // LF

And Avoid Serial.print as it is intended as human readable, so formatted.

references: write print

Saic Siquot
  • 6,513
  • 5
  • 34
  • 56
-1

I'm almost sure that you are looking for the \n new line character.

Alex
  • 9,313
  • 1
  • 39
  • 44
uDalillu
  • 955
  • 8
  • 11
-1

I tried this very simple code (cr = carriage return)

Serial.write(13);

And because the next "printed" caracters will feed the residual text, it's ok.

yannick85
  • 1
  • 1