Serial comm quite slow

Hello, I've recently been working on an arduino project which is interfaced on a computer in C#, communication between arduino is an XBee so serial in other words. I have noticed that it takes around a second or so to transfer a very little amount of data over from the computer to the arduino. Xbees are set at 57.6k baud so it shouldn't be a problem. They were at 9600 and it was roughly the same speed so I assumed something else is slowing it down but don't know what. Here's the arduino code and the C# code.

#include <LiquidCrystal.h>
String line;

LiquidCrystal lcd(22, 23, 24, 25, 26, 27);

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, world!");
  Serial.begin(9600);
  Serial3.begin(57600);
}

void loop() 
{

  if(Serial3.available()>2)    
  {  
    line = Serial3.readString();
    lcd.clear();
    lcd.setCursor(0,0);
    Serial.print(line);
    lcd.print(line);
    if (line == "O 13")
      {
         digitalWrite(29,HIGH); 
      }
    else
      {
         digitalWrite(29,LOW); 
      }
  }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Recognition;

namespace testvoicerec
{
    public partial class Form1 : Form
    {
        SpeechRecognitionEngine speechRecon = new SpeechRecognitionEngine();

        public Form1()
        {
            InitializeComponent();
            serialPort1.Open();
        }
 

        private void Form1_Load(object sender, EventArgs e)
        {
            speechRecon.SetInputToDefaultAudioDevice();

            Choices colors = new Choices();
            colors.Add(new string[] { "stop", "blink", "13", "12", "11", "10", "9", "8", "7", "clear" });

            GrammarBuilder gb = new GrammarBuilder();

            gb.Append(new GrammarBuilder(colors), 1, 3);

            Grammar g = new Grammar(gb);
            speechRecon.LoadGrammar(g);

            speechRecon.SpeechRecognized +=
              new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
            speechRecon.RecognizeAsync(RecognizeMode.Multiple);

        }
               
        void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if (e.Result.Text == "clear")
            {
                label1.Text = "";
            }
            else if (e.Result.Text.Contains("blink"))
            {
                serialPort1.Write(e.Result.Text.Replace("blink", "O"));
                label1.Text = e.Result.Text.ToString();
            }
        }


        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            serialPort1.Close();
        }

    }


}

C# code isn't the best as I am currently testing a few things at once, but even if I try to put the serialPort1.Write(); on a button or something, I still get the same delay so the problem isn't from the speech recognition.

All that gets transfered over is like "O 13" or "O 12" so any reason why it would take approximatly a second to transfer?

Thanks

  Serial.begin(9600);
  Serial3.begin(57600);

I guess the computer is on Serial so I don't wonder that the communication is still slow.

All that gets transfered over is like "O 13" or "O 12" so any reason why it would take approximatly a second to transfer?

How do you measure that second? What triggers the stop?

The Serial(9600) was for the serial monitor, the XBee/computer is on the Serial3. I had a multimedia timer on the interface starting whenever I would send and stop whenever I'd receive from the arduino. I erased that part after I had an approximate of the time. As I said, I was testing multiple things at once.

I had a multimedia timer on the interface starting whenever I would send and stop whenever I'd receive from the arduino.

I never heard of such a device, do you have a link to it? What does it measure? Does it start when the first bit is sent to the XBee and stop when the XBee on the Arduino side gets the first bit? Do you have schematics of how you connected that timer to your setup?

At the moment I guess that the problem has to do with your measurement method but that's just a wild guess because I'm not sure I got the correct overview of your setup. Am I right that you have and Arduino Mega 2560 with an XBee connected to Serial3 and you have another Xbee connected to your Windows machine. You try to send some commands to your Arduino by the way of the XBees and the communication speed is measured by some "multimedia timer" connected to your PC and the Arduino. If that's not correct, please describe in more detail or post a picture of the setup.

Multimedia timer is basically a C# Dll that is a more accurate version of the basic Timer. I started it at the same time as the serialPort1.Write() and it ended with an event of DataReceived. So technically it counted the time for the data to reach the arduino and the time it took for the answer to reach back the computer. I redid the timer (using just a normal timer tho) and I get values around 650 ms. Which is still way too slow for my project.

Yeah the setup you stated is correct.

Here I added back a piece of the original code if it helps you

C#

void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if (e.Result.Text == "clear")
            {
                label1.Text = "";
            }
            else if (e.Result.Text.Contains("blink"))
            {
                timer1.Interval = 10;
                serialPort1.Write(e.Result.Text.Replace("blink", "O"));
                timer1.Start();
                label1.Text = e.Result.Text.ToString();
            }
        }
 private void timer1_Tick(object sender, EventArgs e)
        {
            timeCnt += 10;
            label2.Text = timeCnt.ToString();
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            timer1.Stop();
            listBox1.Items.Add(timeCnt.ToString());         
        }

Arduino

if(Serial3.available())    
  {  
    line = Serial3.readString();
    lcd.clear();
    lcd.setCursor(0,0);
    Serial3.print("Received");
    lcd.print(line);
    if (line == "O 13")
      {
         digitalWrite(29,HIGH); 
      }
    else
      {
         digitalWrite(29,LOW); 
      }
  }

The led on the arduino lights up around the same time as my program receives the answer of the arduino, so I am assuming the issue is on the receiving part of the arduino? I am no expert on arduinos or serial communication, I know the basics but that's about it. Thanks

Alright, I found it. The Serial ports have a timeout delay of 1000ms by default. I changed it to 50ms using Serial3.setTimeout(50) and now it is nearly instantaneous.