0

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 

}

Edited by anuran

2
Contributors
2
Replies
6
Views
5 Years
Discussion Span
Last Post by anuran
0

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.

0

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?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.