Skip to content

Commit

Permalink
Add 4800, parity, and stop bit settings
Browse files Browse the repository at this point in the history
  - Add 4800 baud to the list of rates seached by findBaud().
  - Extend the setBaud() method to set parity and stop bits.
  - Add an example sketch to test findBaud() and setBaud().
  • Loading branch information
Jerry Dunmire committed Dec 7, 2014
1 parent 3b2940a commit 7fd579e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
40 changes: 31 additions & 9 deletions HC05.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ HC05::HC05(int cmdPin, int statePin)
_bufsize = sizeof(_buffer)/sizeof(char);
}

static const unsigned long rates[] = {9600,19200,57600,115200,38400};
static const unsigned long rates[] =
{4800,9600,19200,38400,57600,115200};

unsigned long HC05::findBaud()
{
Expand Down Expand Up @@ -95,7 +96,7 @@ int HC05::cmd(const char* cmd, unsigned long timeout)
}
else
{
DEBUG_PRINTLN("timeout");
DEBUG_PRINTLN("timeout 1");
}
}
while ((recvd > 0) && (_buffer[0] != 'O' || _buffer[1] != 'K'));
Expand All @@ -112,12 +113,12 @@ int HC05::cmd(const char* cmd, unsigned long timeout)

/*
* If setBaud() is called while the HC-05 is connected, then
* it will be disconnected when the AT+RESET command is issued, and
* it will be disconnected when AT+RESET command is issued, and
* it may take 2 (or more?) connection attempts to reconnect. The extra
* connect attempts may be a host side issue and not specific to the
* HC-05 module.
*/
void HC05::setBaud(unsigned long baud)
void HC05::setBaud(unsigned long baud, unsigned long stopbits, unsigned long parity)
{
int recvd = 0;
setCmdPin(HIGH);
Expand All @@ -126,24 +127,45 @@ void HC05::setBaud(unsigned long baud)
_btSerial.write("AT+UART=");
DEBUG_PRINT(baud);
_btSerial.print(baud);
DEBUG_WRITE(",0,0\r\n");
_btSerial.write(",0,0\r\n");

DEBUG_PRINT(",");
_btSerial.print(",");

DEBUG_PRINT(stopbits);
_btSerial.print(stopbits);

DEBUG_PRINT(",");
_btSerial.print(",");

DEBUG_PRINT(parity);
_btSerial.print(parity);

DEBUG_WRITE("\r\n");
_btSerial.write("\r\n");

recvd = _btSerial.readBytes(_buffer,_bufsize);
if (recvd > 0)
{
DEBUG_WRITE((uint8_t *)_buffer,recvd);
}
else
{
DEBUG_PRINTLN("timeout");
DEBUG_PRINTLN("timeout 2");
}
setCmdPin(LOW);
delay(100);
cmd("AT+RESET");
setCmdPin(LOW);
_btSerial.begin(baud);
delay(1000);
}

// Usually parity is none, and there is only one stop bit, so this
// simpler call will do the job.
void HC05::setBaud(unsigned long baud)
{
setBaud(baud, 0, 0);
}


int HC05::available()
{
_btSerial.available();
Expand Down
3 changes: 2 additions & 1 deletion HC05.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ class HC05 : public Stream
HC05(int cmdPin, int statePin);
HC05(int cmdPin, int statePin, uint8_t rx, uint8_t tx);
unsigned long findBaud();
void setBaud(unsigned long baud);
void setBaud(unsigned long baud); // always no parity, one stop bit
void setBaud(unsigned long baud, unsigned long stopbits, unsigned long parity);

// cmd(): 100ms default timeout covers simple commands, but commands
// that manage the connection are likely to take much longer.
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ The Stream class is extended with the following methods.
call this only when the rate returned by findBaud() is not the one
you require.

`setBaud(unsigned long rate, unsigned long parity, unsigned long stopbits)`
Use this method when you need something besides the default no
parity, one stop bit settings that are the default.
__CAUTION!__ The HC-05 supports many serial configurations that are not
compatible with an Arduino. For example, the Arduino software serial
port port supports only no parity, one stop bit settings.

`cmd()`
Send a command to the module. The 'key' (cmdPin) pin is activated to
put the module in command mode where 'AT' commands are recognized.
Expand Down Expand Up @@ -88,6 +95,11 @@ See the `SoftwareSerial.fzz` file for the proper default connections.
ports. Power to HC-05 must be controlled by an Arduino pin.
See the `Recovery.fzz` diagram for suitable connections.

`findBaudTest`
Tests both setBaud() and findBaud() by trying every combination of
supported rates. The output from this example looks best if
DEBUG_HC05 is not defined. (Simply comment out that line in HC05.h).


Installation
------------
Expand Down

0 comments on commit 7fd579e

Please sign in to comment.