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 

}

Recommended Answers

All 2 Replies

The default speed of most serial ports is 9600 baud (about 8kbits/sec). You need to configure the speed on both ends (the computer as well as arduino board) to a higher speed. Most rs232 serial ports will support about 115kbps these days.

i did that change but still no effect. there is problem with serial communication done by msvc++ side. i need to implement any other serial communication way in windows use some other api.but i dont know which one to?

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.