BaudRate with two values

Hi, I need to change the baud rate to receive and transmit strings, I am using this code, but when I set the Serial.begin inside the loop, my code does not work, but if I remove this if the code runs, also I have inserted a delay after the baudrate change, but despite the fact that I am not sending anything to the serial port, there are some characters that are leaking, I do not understand where they have been generated from, the characters in hexadecimal format are:

FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A FE 0D 0A . . .

what can be the problem?

String bufferStringRX;

void setup() {
  Serial.begin(9600);
  Serial.println();
  Serial.println("Init Test");
  delay(100);
}

void loop() {
  Serial.begin(115200);
  delay(20);

  if (Serial.available() > 0) {
    bufferStringRX = Serial.readStringUntil('\n');

    if ((bufferStringRX != NULL)  ) {
      Serial.println(bufferStringRX);
      bufferStringRX = "";
      Serial.flush(); 
      Serial.end();
    }
  }
  delay(100);
}

Where is the output of Serial.print() supposed to go?

You can't change the Baud rate in mid-stream and expect the receiving device to recognize the change.

Nor can you expect the sending device to know what Baud rate is expected by the Arduino.

Hi,
What model Arduino are you using?
If a Mega, it has more than one serial I/O.
If a UNO or Nano, have you tried Software Serial library, to make an extra serisal port from I/O pins?

Can you tell us the application, what hardware are you using and what is communicating with what?

Tom... :smiley: :+1: :coffee: :australia:

After you send the data you would want to use:

Serial.flush(); // sends the rest of your string
Serial.end();  // ends the serial activity

to finish the transmission and reset the Serial port. Then you can call
Serial.begin(12345);

I am not sure of the practical use, or need, for that, but that is how you do it.

I am using a NANO and a serial port multiplexer.

I can't use the Serial library because I need it to be at 115200 and the Serial library only supports 9600, higher speeds give an error

I already made the change but the problem is still there,

Why are you changing it at all? Just set it to 115200 in setup() and leave it there.

Not solving the problem but I think that this is quite useless.

Maybe you can swap the functionalities around; Serial for 115200 and SoftwareSerial for 1200.

As I said, I'm using a NANO with a multiplexer that must communicate with two peripherals that have different speeds (115200, 19200), that's why I can't define a single speed, I just published the sketch to show how I try to change the speed within the loop,

From your descriptions, it's not quite clear how you're testing. Are you using serial monitor to see the result? If so, that means that you have 2 serial devices on one UART, the PC and the multiplexer (with sources).

Also, if you change the baudrate in serial monitor, the Nano will reset; it might explain the unexpected data (leftovers from the bootloader).

I suggest that you provide a schematic (photo of handdrawn one is fine).

Why not switch to a Teensy 3.2? It has 3 Hardware UARTS that will run at whatever baud you want plus the native USB for Serial debug printing. More powerful processor and more memory too.

Would you please be kind enough to upload my code to your Arduino and enter a simple string by console, you will notice that when you enter a string, it is printed to the console but there is also a Hex 0xFF \r\n character that repeats continuously, I don't understand why?


The squares are probably your "Init test", "abcdef" is the text that I entered.

Code used:

String bufferStringRX;

void setup() {
  Serial.begin(9600);
  Serial.println();
  Serial.println("Init Test");
  delay(100);
}

void loop() {
  Serial.begin(115200);
  delay(20);

  if (Serial.available() > 0) {
    bufferStringRX = Serial.readStringUntil('\n');

    if ((bufferStringRX != NULL)  ) {
      Serial.println(bufferStringRX);
      bufferStringRX = "";
      Serial.flush(); 
      Serial.end();
    }
  }
  delay(100);
}

Note: changing baudrate in IDE2.0 does not seem to reset the Nano; this differs from 1.8.x.
I made use of that by

  1. use 9600 baud
  2. close the serial monitor
  3. open the serial monitor; this resulted in the text Init Test
  4. change the baudrate to 115200
  5. typed message abcde and hit <ctrl><enter>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.