Serial port and serial monitor

Hello,

I need to communicate from my Arduino Leonardo to my PC and vice versa. I use QSerialPort and it works fine.

I manage to communicate in both directions and to detect then to open the port where the microcontroller is.

But for this, to read from the PC, I must first open the Arduino IDE Serial Monitor. Without it, I can not read it. I can open it but it seems the Arduino is sending nothing. Does the monitor do anything special?

Would anyone have an explanation or a solution?

Thank you.

Post your Arduino program.

Is your PC setting things like DTR and RTS (never can remember which is important) correctly when it opens the Serial port.

...R

#include <Keyboard.h>

const int BUTTON = 2;
int save = 0, r, nb;

void setup() {
  pinMode(BUTTON, INPUT_PULLUP);
  Keyboard.begin();
  Serial.begin(9600);
  while (!Serial);
}

void execAction(char command, byte value) {
  switch (command)
  {
    case 1:
      Keyboard.press(value);
      break;
    case 2:
      Keyboard.release(value);
      break;
    case 3:
      Keyboard.write(value);
      break;
  }
}

void readData(int nb) {
  char* str = new char[nb];

  Serial.readBytes(str, nb);
  for (int i = 0; i < nb; i += 2) {
    if (str[i + 1] < 0) {
      int a = 255 + str[i + 1];
      execAction(str[i], a);
    }
    execAction(str[i], str[i + 1]);
  }
  delete[] str;
}

void loop() {
  r = !digitalRead(BUTTON);
  if (r && !save) {
    Serial.write(42);
  }
  save = r;
  if (nb = Serial.available())
    readData(nb);
}

My PC is just open the port with QSerialPort.

Nyhillius:
My PC is just open the port with QSerialPort.

What detailed serial port settings does QSerialPort use?

I use Linux so I am not able to advise about specific Windows or Mac issues. What operating system is on your PC?

...R

Robin2:
What detailed serial port settings does QSerialPort use?

I use Linux so I am not able to advise about specific Windows or Mac issues. What operating system is on your PC?

...R

Thank you for your answer Robin.

I use Windows 10 and QSerialPort can find settings for the serial port. I also use the USB Serial port on the Leonardo.

QSerialPort can find settings for the serial port.

So, what settings does it find?

Unlike the other Arduinos, the Leonardo requires that the DTREnabled flag be set to true. The Serial Monitor does this. Most applications do not. The default value for the flag is false.

I found it! Just need to set setDataTerminalReady to true !

Thanks.

Nyhillius:
I found it! Just need to set setDataTerminalReady to true !

Thanks.

I'm reasonably certain that setDataTerminalReady maps to DTREnabled that the rest of the world uses.