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
The same question is on the receive part. Which one is correct:
You don’t actually use a literal string of or . 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:
= 0x0A
= 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” and “<CR_LF>OK<CR_LF>”. If you are using something other than a C derivative, you might have different escape characters.
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:
I wrote the SerialStringOut() function. Here is the code:
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?
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:
void SerialOutString(const unsigned char *sstring)
{
int i = 0;
while (sstring[i] != '\0')
{
TXREG = sstring[i++];
while(!TRMT) asm("CLRWDT");
}
}
csienke’s loop looks fine to me — the assignment’s return value is the value being assigned, so it’s the same as
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.
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.