SparkFun Forums 

Where electronics enthusiasts find answers.

All things pertaining to wireless and RF links
By csienke
#42643
Hello!

I'm new to Bluetooth and AT commands. I would like to use a Bluetooth module with RS232 interface, but I can't figure out how to receive and send an AT command.
My question is: every command I send ends with CR, or I must to send the > character too? I mean which one is correct to send:

1.) ATA CR
or
2.) ATA <CR>

The same question is on the receive part. Which one is correct:
1.) CR LF CONNECT 123456789012 CR LF
or
2.) <CR,LF>CONNECT 123456789012<CR,LF>

Can everybody help me please?
By cgili
#42708
You don't actually use a literal string of <CR> or <LF>. Those are non-printable characters, so you need to transmit the actual value equivalent (see http://www.asciitable.com) of a carriage return or line feed. So you can set a char variable to the following hex values, and transmit the char:

<LF> = 0x0A
<CR> = 0x0D

Or, if you are using C or C++, and sending the commands as strings, you need to escape the character. To do this, you can just add \r or \n to the string. So you would send something like "AT\r" and the response should be "\r\nOK\r\n". This would be the string equivalent of "AT<CR>" and "<CR_LF>OK<CR_LF>". If you are using something other than a C derivative, you might have different escape characters.
By csienke
#42714
Thank you for the reply cgili!

I want to write a program in MPLAB C18. So I simply send the hexa values of the characters on the USART?

Example: The command AT<CR> is equivalent with 0x65 0x84 0x0D ?
And the response of this is 0x0D 0x0A 0x79 0x75 0x0D 0x0A ?
By cgili
#42717
Yep, I use a simple function like this:
Code: Select all
void SerialOut(char val)
{
	TXREG = val;
	while(!TRMT) asm("CLRWDT");    // Reset WDT while waiting
				//for the TSR buffer to clear
	//you can drop the asm call from the while loop if watchdog is disabled
}
Then you can use the hex values or the actual character in quotes:
SerialOut('A');
SerialOut('T');
SerialOut(0x0D);

You can also wrap that in a function that will handle full strings:
SerialStringOut("AT\r");
By csienke
#42767
I wrote the SerialStringOut() function. Here is the code:
Code: Select all
void SerialStringOut(const unsigned char *str)
{
	unsigned char c;
	while (c = *str++)
	{
		TXREG = c;
		while (!TRMT);     //wait for the TSR buffer to clear
	}
}
My question is: is the code correct? I mean when I call this function, it will read the '\r' character out instead the '\' and 'r' characters?

SerialStringOut("AT\r");
By cgili
#42783
Not sure about the evaluation of the while loop there. I've never done an assignment for the conditional evaluation, so that might be a trick I'm not following. Could you explain what you're doing there?

Here's mine:
Code: Select all
void SerialOutString(const unsigned char *sstring)
{
	int i = 0;

	while (sstring[i] != '\0')
	{
		TXREG = sstring[i++];
		while(!TRMT) asm("CLRWDT");
	}
}
By wiml
#42796
csienke's loop looks fine to me --- the assignment's return value is the value being assigned, so it's the same as
Code: Select all
for( ; c != 0 ; c = *str++ ) { ... } 
csienke: the C-language syntax for quoted strings uses backslashes to let you include special characters, such as \r (0x0D or carriage return), \n (0x0A, newline (NL) or linefeed (LF)), \t (tab), and so on. You can also use \x for a hexadecimal character. So the string "ab\rcd\x55e" has the characters 'a', 'b', CR, 'c', 'd', 0x55, 'e' (plus a trailing 0x00 character to terminate the string).

If you want an actual backslash you need to double it, so "AT\\r" is a four character string with an A, T, backslash, r.
By cgili
#42806
wiml... okay, that makes sense. Just hadn't seen it done that way before.

As for csienke's output, he was trying to get "AT<CR>" so "AT\r" will work for him.
By csienke
#42812
Sorry cgili, I just read your post.

The end of the string is always '\0', so I wrote this way the code. And the while loop will stop when this character in str is reached. When the c variable will have the value '\0', the loop ends, because the while expression will be false (It will look like this: while(0) ).

I would like to thank you for all your replys cgili. And you too wiml.

In case of other troubles I'll post my question in this forum.