Uno R4 Minima: Serial issues

Hi there,
I'm encountering issues with the serial communication on Arduino R4 Minima.
I have a project, that runs fine on the Arduino R3. This project communicates wit a PC (C# program) via serial port.
I am unable to get it to run on the R4.

It seems, that R4 behaves different than on R3 concerning the serial port. My test program initializes the serial port to a certain baudrate and then changes the baudrates. The test results are:

a) Regardless what baudrate I set initially by the software, Arduino always seems to set the baudrate according to the baudrate in the Serial Monitor. Successive changes to the baudrate seems not to have an effect. (see attached screen shot)

b) I can see the output from the test program only on the Serial Monitor of the Arduino IDE and on the Serial Monitor of MS Visual Studio with VisualMicro extensions. If I use a different Terminal Program, I cannot see the output generated by the R4 test program??

c) I can simulate the serial protocol of my original communication program using Arduino Serial Monitor. This seems to work.

My questions:
q1) What is the trick (or behind the scenes), that the R4 always comminicates with the baudrate that is set in the Serial Monitor?

q2) What do I have to make, that the communication with the PC works?

Thanks for your Help
Michael

Here is my test program:

// the setup function runs once when you press reset or power the board
void setup() {
    Serial.begin(1200);
    while (!Serial);

    Serial.println("Setup: Set first baudrate: 1200");
    Serial.println("Setup: done ...");
}

// the loop function runs over and over again until power down or reset<
void loop() {
    doDisplayLoop();

    Serial.println("Set new baudrate:  115200");
    Serial.end();
    Serial.begin(115200);
    doDisplayLoop();

    Serial.println("Set new baudrate:  19200");
    Serial.end();
    Serial.begin(19200);
    doDisplayLoop();

    Serial.println("Set new baudrate:  9600");
    Serial.end();
    Serial.begin(9600);
    doDisplayLoop();

    Serial.println("Set new baudrate:  1200");
    Serial.end();
    Serial.begin(1200);
    doDisplayLoop();

    Serial.println("Set new baudrate:  300");
    Serial.end();
    Serial.begin(300);
    doDisplayLoop();
}

void doDisplayLoop() {
    for (int i = 1; i <= 5; i++) {
        PrintSerial();
        delay(500);
    }
}

void PrintSerial() {
    long time = millis();
    int Baud = Serial.baud();
    int StopBits = Serial.stopbits();
    int ParityType = Serial.paritytype();
    int NumBits = Serial.numbits();

    Serial.print("Time=");
    Serial.print(time);

    Serial.print("  Baud=");
    Serial.print(Baud);

    Serial.print("  NumBits=");
    Serial.print(NumBits);

    Serial.print("  StopBits=");
    Serial.print(StopBits);

    Serial.print("  ParityType=");
    Serial.print(ParityType);

    Serial.println();
}


On the minima the Serial object is done directly by the usb subsystem of the processor. It always communicates at USB speed. The baud rate does not matter as far as this communication is concerned.

However, it does communicate back to you the host has specified. In most cases it does not matter. But in some cases it can. For example maybe the host sets a specific baud rate to tell the processor to reboot.

Or in some sketches like using minima as a usb to serial adapter, your sketch might monitor this setting and then set one or more hardware serial ports to the new baud

As already stated, there is no UART communication between the MCU and the PC - it's all USB communication. And USB doesn't need a baudrate - so the baudrate setting in Serial.begin is ignored ( it can even be omitted). If you set a baudrate at the PC side, this information is sent to the R4, but normally it will ignore it. In your sketch it is simply printed.

That should simply work. I tried with putty, and it worked.

[Edit] The R3 is different, because there is a UART communication betweend MCU (ATmega328) and the UART-USB converter (Mostly ATMega16u2 or CH340). The converter sets its baudrate to the value it receives from the PC, and the MCU must be set to the same baudrate.

@KurtE, @MicroBahner,

Thank you for clarifying. But in the end, my C# program to communicate with the R4 Minima still didn't work.

Thanks to an other user and another forum entry I found the solution. You have to setup the serial port on the PC side (C# program) in a special way:

mySerialPort = new SerialPort(ComboPort.SelectedValue.ToString());
mySerialPort.BaudRate = 115200;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.DtrEnable = true; // needed to work with R4 Arduino
mySerialPort.RtsEnable = false;
mySerialPort.Encoding = ASCIIEncoding.ASCII;
mySerialPort.ReadTimeout = 500;
mySerialPort.WriteTimeout = 500;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

Thank you guys,

Best regards,
Michael

I believe this is the critical line.

1 Like

Yes exactly, this is the critical setting, but I don't really know, what this means in an USB communication. As far as I know, DTR, RTS, DCT, etc. are unique in a standard serial connection.

It means nothing for USB communication. But the information is transmitted to the R4 Minima, and the question is whether it analyses and respects it. Obviously, Serial() only sends data if the information ‘DTR enabled’ has been received.

In the end the R4 emulates a standard serial connection over USB.

@MicroBahner
Thanks for clarification :+1: