[SOLVED]Slow response via Serial Communication

Hello!
So I just started using arduino again since i got an amazing thing i wanted to try out.
So on my PC im going to scan for objects using a program i wrote which then will send the information to move my mouse to the arduino so it handles the mouse movement.

Everything works great when it reads it as an int but if I use string it takes about 1 second to execute my command and move my mouse.

I found out if I use the "Serial.setTimeout(50)" it does it instant but do I risk loosing out on information using that?

Here is my arduino code

#include <Mouse.h>
int LED = 13;



void setup(){
  Serial.begin(345600);
  Serial.setTimeout(50);
  pinMode(LED, OUTPUT);
  Mouse.begin();
}

void loop(){
  String data = Serial.readString();
  if(data == "GGNOOB") {
    Mouse.move(1000, 200);
  }
}

Here is my C++ code:

#include "pch.h"


using namespace System;
using namespace System::IO::Ports;

int main(array<System::String ^> ^args)
{
    


    SerialPort connection("COM13", 345600);
    connection.Open();
    while (true)
    {
        int input = Convert::ToInt32(Console::ReadLine());
        if (input == 1) 
            connection.Write("GGNOOB");
        
        else
        
            connection.Write("K");

        



    }

    return 0;
}

Its really important that it doesnt loose the data sent so I would love if someone could help me out here!

Also random extra question: Can you somehow choose what COM port it always should use?

Edit:

Try the WriteLine method on the PC side instead.

What COM port number that's used is up to the operating system. Typically built in serial ports are assigned the same number, but USB to serial converters vary wildly. The best you can do is give the user the ability to choose or to identify it at runtime.

Have a look at Robin's Serial Input Basics - updated to get ideas how to approach serial communication.

// Edit

Yes, there is a risk. I'll advise you to go through the above link, but Serial.readBytesUntil() might be a usable alternative with the standard timeout; you will have to add a terminating character (e.g. '\n') at the end of the data that you send.

Thank you for the reply :slight_smile:
I found another way to do it :slight_smile:

Again thank you so much for linking me that website!

Thank you for the reply :slight_smile:
I found another way to do it :slight_smile:

1 Like

[quote="sylian1337, post:1, topic:895500"]

void loop(){
  String data = Serial.readString();
  if(data == "GGNOOB") {



    Mouse.move(1000, 200);
  }
}

The above codes shoulde be formatted as follow as data can only read from Serial Buffer once they are avaiable.

void loop()
{
   byte n = Serial.available();
   {
       if(n !=0)
       {
           String data = Serial.readString();
           if(data == "GGNOOB") 
           {
                Mouse.move(1000, 200);
           }
       }
   }
}

Does Serial Monitor of Arduino UNO support the above baud rate?

That should be easy to check in Serial Monitor; just scroll through the baudrate options at de bottom of the screen.