Go Down

Topic: Change Baud Rate at Runtime (Read 8491 times) previous topic - next topic

tparty

Feb 26, 2016, 02:21 am Last Edit: Feb 26, 2016, 04:42 am by tparty
I'm using an Arduino Nano and trying to change the baud rate to talk with a wifi chip (HF-LP100). I've successfully changed from 11500 to 9600 but I am unable to change from 9600 to 115200. How I'm trying to do it

Code: [Select]
Serial.begin(9600);
delay(1000);

if (digitalRead(PD7) == 1) { //this is wifi status
  Serial.end();
  delay(500);

  Serial.begin(115200);
  delay(1000);
 
  //code to change wifi chip is here already know this works

  Serial.end();
  delay(500);

  Serial.begin(9600);
  delay(1000);
}


I have successful made the wifi chip change baud rates with the initial nano baud rate set to 115200 and then successfully sent commands to the chip after I've changed it to 9600, so that means I've changed the Nano baud rate to 9600 as well.

I've also tried this with the 'Serial.flush()' command before 'Serial.end(); but that didn't change anything. I've looked through several posts on here but haven't found a solution from them. Thanks for any help!

Edit: So I'm not quite sure why but it seems to be working correctly now. I put Serial.flush(); back in and delay(2); but it didn't work with these earlier...so code is like this

Code: [Select]
  Serial.flush();
  delay(2);
  Serial.end();
  delay(500);
 
  //Start the serial port and wait for it to initialize
  Serial.begin(115200);
  delay(3000);

  //code to change wifi chip is here already know this works

  Serial.flush();
  delay(2);
  Serial.end();
  delay(500);

  Serial.begin(9600);
  delay(3000);
}

chucktodd

I'm using an Arduino Nano and trying to change the baud rate to talk with a wifi chip (HF-LP100). I've successfully changed from 11500 to 9600 but I am unable to change from 9600 to 115200. How I'm trying to do it

Edit: So I'm not quite sure why but it seems to be working correctly now. I put Serial.flush(); back in and delay(2); but it didn't work with these earlier...so code is like this

Code: [Select]
 Serial.flush();
  delay(2);
  Serial.end();
  delay(500);
  
  //Start the serial port and wait for it to initialize
  Serial.begin(115200);
  delay(3000);

  //code to change wifi chip is here already know this works

  Serial.flush();
  delay(2);
  Serial.end();
  delay(500);

  Serial.begin(9600);
  delay(3000);
}

You don't need to use the Serial.end() function, Just do a Serial.flush() then Serial.begin(newBaud);

The delays are not need either.

The problem you are probably having, is that when you issue Serial.end() it releases the TX pin.  depending on how you had previously configured pin 1, it could have defaulted back to an INPUT pin.  Since it is connected to another input it would float.  Probably falling to 0v which is interpreted as a START bit by whatever it is connected to, then you reinit the Serial hardware.   Which configures pin1 as an OUTPUT, who's idle state is HIGH.  So your device see a start bit followed by a 0xff, no Stop bits, incorrect parity bit.   This probably cause the attached device to enter an error cycle.

That is why your device sometimes does not respond correctly, you are sending it garbage while you are changing baud rate.

When you want to change Baud rate just do this:
Code: [Select]

Serial.flush(); // wait for last transmitted data to be sent
Serial.begin(newBaudRate);
while(Serial.available()) Serial.read(); // empty  out possible garbage from input buffer
// if the device was sending data while you changed the baud rate, the info in the input buffer
// is corrupted.



Don't use Serial.end() unless you want to use the TX, RX pins for something other than Serial.

Chuck.
Currently built mega http server, Now converting it to ESP32.

Go Up