2

Thanks for taking a look at my question.

I have this problem with my RN42 Bluetooth Module in where if i am only sending a small amount of data such as printing "Hello World" every second. If i use this code:

void setup()
{

    //Open serial communications and wait for port to open:
    //Serial.begin(115200);
    Serial1.begin(9600);


}

void loop()
{ 
  Serial1.print("Hello World");
  //delay(1000);
}

This is all my code if i use this code with my RN42 when i connect to the module it will stay connected for about 3 seconds and then just reboot. I have tried both 9600 and 115200 and they both close the connection after a few seconds.

This is part of a large project and has been running fine but under some circumstances it was crashing and i have managed to point it to this.

I have the RX and TX connected directly to the Arduino and have the CTS and RTS connected to each other.

Has anyone had any similar issues?

Regards

Jamie

I Managed to figure out what the problem was, and wanted to share. When the Module is in Command Mode and allows you to enter '$$$', The Bluetooth module will reset if you try to send it a huge amount of data. It is in a tiny note on the booklet:

Note: The module supports a fast data mode. In this mode, the module does not go into command mode even if it receives $$$. If you do not enter command mode within the configuration window (60 seconds), the module enters fast data mode.

As i was using the command mode any time it would never enter Fast Data Mode. I have now set the Command Timeout to 5 seconds so if i need to use it i just reset the module.

4

1 回答 1

0

I use the following home built library to talk to an RN42 on Serial3. Notice I have a delay of 50ms to allow it to stabalize during the ::begin(). and after this, I simply use Serial3.Print's or Read's. Change the Serial# and Pins to match your needs.

#define DEFAULT_FLUSH_TIMEOUT 50 // ms
#define BT_RX                 14 // PJ1 Output, USART3_TX
#define BT_TX                 15 // PJ0 Input,  USART3_RX
#define BT_CTS                27 // PA5 Output, Active Low,  Enable Device Transmission
#define BT_RTS                26 // PA4 Input , Active Low,  Requesting Data
#define BT_RST                23 // PA1 Output, Active Low,  Resets BlueTooth Transciever

void RN42::begin() {
  digitalWrite(BT_RST, BT_RST_Enabled);   //Take Radio out of Reset
  digitalWrite(BT_CTS, BT_CTS_Enabled);   //Enable Transmitter

  delay(750); // need to wait for the radio to stablize.

  Serial.print("$$$ = ");
  Serial.print (command("$$$", '\n', DEFAULT_FLUSH_TIMEOUT));
  Serial.println();
  delay(100); // delay as it avoids problems, with flush.

  read_version = command("v\n", '\n', DEFAULT_FLUSH_TIMEOUT);
  read_serial = command("GB\n", '\n', DEFAULT_FLUSH_TIMEOUT);
  read_connection = command("GK\n", '\n', DEFAULT_FLUSH_TIMEOUT);

  Serial.print("Online = ");
  Serial.print(command("---\n", '\n', DEFAULT_FLUSH_TIMEOUT));
  Serial.println();

  Serial.print("version1 = ");
  Serial.println(read_version);

  Serial.print("Serial = ");
  Serial.print(read_serial);
  Serial.println();

  Serial.print("Connection = ");
  Serial.print(read_connection);
  Serial.println();
}

void RN42::end() {
  //turn radio of and put into reset.
  digitalWrite(BT_RST, BT_RST_Disabled); 
  digitalWrite(BT_CTS, BT_CTS_Disabled);
}   

void RN42::TxOff() {
  // turn Transmitter off to save power
  digitalWrite(BT_CTS, BT_CTS_Disabled);
}   

void RN42::TxOn() {
  // turn Transmitter on, more power
  digitalWrite(BT_CTS, BT_CTS_Enabled);
}

void RN42::flush(long timeout) {
    long start_time = millis();
    int state = 1;
    char c;
    while (state)
    {
      int i = Serial3.available();
      for (int  thischar = 0; thischar < i; thischar++) {
        c = Serial3.read();
      }
      if ( (millis() - start_time) > timeout ) 
      {
        state = 0;
      }
    }
}

String RN42::command (String sCommand, char cTerminator, long timeout)
{
  String    inData="";
  long start_time = millis();
  int state = 0;
  Serial3.print(sCommand);
  while (!state)
  {
    int len = Serial3.available();      
    for (int  thischar = 0; thischar < len; thischar++) {
        char c = (char) Serial3.read();
//              Serial.print("0x");
//              Serial.println((int) c, HEX);
        if ((c == '\n')) {// || (c == '\r')) {
          state = 1;
          break;
        } else {
          if (state != 1) {
            inData += c;
          }
        }
    }

    if ( (millis() - start_time) > timeout ) 
    {
      state = 2;
      //Serial.println ("TimedOut");
    }
  }
  inData.trim();
  if ((state = 1)) flush(timeout);
  return inData;
}
于 2013-08-03T23:33:42.600 回答