im using this code to send strings to serial port but this is very slow,the other end of communciation arduino board is not able to get those strings at realtime,how can i fasten this communication? there is no problem in arduino board or arduino code,it works fine for its sample examples and also works fast for serial monitor communication which is inbuilt with arduino programmer ide.

msvcpp code

#include "stdafx.h"
#include "conio.h"
#include "iostream"
#include "windows.h"
#include "WinDef.h"
#include "WinUser.h"
#include "windowsx.h"
#using <mscorlib.dll>
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "User32.lib")
using namespace System;
using namespace System::IO::Ports;

int main(array<System::String ^> ^args)
{ String^ answer;
  String^ answernew;
  POINT coord;

  answernew="hello";
    // arduino settings
    SerialPort^ arduino;
    arduino = gcnew SerialPort("COM5", 115200);
    // open port
    try
    {
        arduino->Open();

        while(1)
        {   
            GetCursorPos(&coord);
            answer=Convert::ToString(int(coord.x/7.80));
            //7.80 is scaleing factor of screenwidth by max angle of servo motor

            //if mouse moves to new location
            if(String::Compare(answer,answernew))
            {
            answernew=answer;

            Sleep(1200);
            //1 is motor no im trying to control 
            Console::WriteLine(answernew);
            arduino->Write(String::Concat("1,",answernew));
            }
        }
        // close port to arduino
        arduino->Close();
    }
    catch (IO::IOException^ e  )
    {
        Console::WriteLine(e->GetType()->Name+": Port is not ready");
    }
    catch (ArgumentException^ e)
    {
        Console::WriteLine(e->GetType()->Name+": incorrect port name syntax, must start with COM/com");
    }

    Sleep(500);
    return 0;
}


//arduino code
#include <Servo.h> 

Servo myservo1;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 //maximum 176 to minimum 1

void setup() 
{ 
  myservo1.attach(7);  // attaches the servo on pin 9 to the servo object 
  Serial.begin(115200);
} 


void loop() 
{  if(Serial.available()>0)
    {int servo=Serial.parseInt(); 

     int pos=Serial.parseInt();
   //   myservo1.write(pos);
     if(pos>0&&pos<176)
     switch(servo)
     {
     case 1:myservo1.write(pos);break; 
     case 2:;//myservo1.write(pos);break; 
     case 3:;//myservo1.write(pos);break;                  // tell servo to go to position in variable 'pos' 
     }
     Serial.flush();
    }
                       // waits 15ms for the servo to reach the position 

}

Serial communication is inherently sloooow, which is one reason hardware manufactures switched to USB. This is one are in which MS-DOS 6.X did a lot better than MS-Windows or *inix because MS-DOS was pretty close to a real-time operating system and programmers had complete access to the hardware and ports.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.