serial communication between computer and arduino uno via serial cable

I wanted to do serial communication between computer and arduino uno via serial cable.
The serial cable that I mentioned is usually used for unloading programs to arduino.
I thought using this cable, I could achieve connection between my laptop(visual studio) and arduino uno.
And it seemed to work fine but time to time it malfunctioned. And I just don't know why.

The purpose of doing this is to make my servos turn according to
the cartesian coordinates sent by my lap top in form of
(x coordinate number) , (y coordinate number) .
ex) 123,234.345,234.123,232. ............................
The "coordinates" are coordinates of a ball in the video taken by my camera attached to my laptop
The coodinates of the ball are calculated by using OpenCV

my arduino gets this "string" and saves it until comma(",") or period(".") is met.
when it meets comma, it converts the string it has saved so far into a number and saves the value
in the "int" variable named "DrawX". And erases(clears) the saved string.
when it meets period, it converts the string it has saved so far into a number and saves the value
in the "int" variable named "DrawY". And erases(clears) the saved string.
And then the arduino turns servo to 180 degrees if value in the "DrawX"is over 200.
if the value is below 200 or same as 200, it makes the servo to go 0 degree.

When I actually ran the code, It sometimes worked and sometimes didn't.
So I checked arduino part alone. Putting in coordinates by using serial monitor
(while not running any programs using serial communication on the computer.)
And found out that it show errors when ever I entered same coordinate twice.
And also my arduino got disconnected, making it no longer able to upload programs by using arduino app.

The error messages and codes are shown below. And attached file is the video showing the results

=============================================================================================
The code

#include <Servo.h>
String inString = "";    // string to hold input
int DrawX, DrawY= 0;
Servo myservo;
Servo myservo2;
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
   myservo.attach(9);
   myservo2.attach(10);
  // send an intro:

}
void loop() {
  int inChar;
  // Read serial input:
  if (Serial.available() > 0) {
    inChar = Serial.read();
  }
  if (isDigit(inChar)) {
    // convert the incoming byte to a char 
    // and add it to the string:
    inString += (char)inChar; 
  }
  \  if (inChar == ',') {
    // do something different for each value of currentColor:
      DrawX = inString.toInt();
      // clear the string for new input:
      inString = ""; 
  }
  // if you get a "." you know you've got
  // the last color, i.e. blue:
  if (inChar == '.') {
    DrawY = inString.toInt();
    // print the colors:
    Serial.print("DrawX: ");
    Serial.print(DrawX);
    Serial.print(", DrawY: ");
    Serial.print(DrawY);
    Serial.print("\n");
    if(DrawX>200)
    {
      myservo.write(0);
      myservo2.write(0);
      delay(2000);
      
    }
    
    else
    {
      myservo.write(180);
      myservo2.write(180);
      delay(2000);
    }
    
    // clear the string for new input:
    inString = ""; 
  }
}

=============================================================================================

=============================================================================================
error messages

???? ??? ???: 6,272 ??? (?? 32,256 ???)
java.io.IOException: Input/output error in writeArray
at gnu.io.RXTXPort.writeArray(Native Method)
at gnu.io.RXTXPort$SerialOutputStream.write(RXTXPort.java:1124)
at processing.app.Serial.write(Serial.java:517)
at processing.app.Serial.write(Serial.java:540)
at processing.app.SerialMonitor.send(SerialMonitor.java:200)
at processing.app.SerialMonitor.access$100(SerialMonitor.java:32)
at processing.app.SerialMonitor$3.actionPerformed(SerialMonitor.java:89)
at javax.swing.JTextField.fireActionPerformed(JTextField.java:492)
at javax.swing.JTextField.postActionEvent(JTextField.java:705)
at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:820)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1636)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2851)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2886)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2814)
at java.awt.Component.processEvent(Component.java:6040)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:704)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:969)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:841)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:668)
at java.awt.Component.dispatchEventImpl(Component.java:4502)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)

Adding a character to a string takes next to no time. That is NOT what you are doing. Adding a character to a String can take time. THAT is what you are doing. Case matters. A great deal. There really is no excuse for using Strings in this case. You KNOW that there is a maximum limit on the size of a string that represents a number that the servo can actually move to.

  int inChar;
  // Read serial input:
  if (Serial.available() > 0) {
    inChar = Serial.read();
  }
  if (isDigit(inChar)) {
    // convert the incoming byte to a char
    // and add it to the string:
    inString += (char)inChar;
  }

It's rather silly to store a character in an int, and then cast the int to a char. Use the correct variable type to hold the value, and no casting is needed.

Delay()ing for 2 seconds after each servo move seems pointless. The servos should move as soon as new data arrives (to something other than 180). The sender should be responsible for not sending data too often.

@PaulS

(English is my second language so I might sound strange.... )

Oh since I modified the code from the example(I'm newbie) given by the arduino app(version 1.0.5)
I guess I did not pay attention to the code enough....I used the example " Strings > StringToIntRGB"
So you are saying that I should write

char inChar instead of int inChar.

and not use casting operator "(char)"
I understand...
but then by changing that, can the disconnection problem and malfunctioning problem I'm experiencing be solved?

And for the delay, if you will see the example code in the app, there are also delays there.
without a bit of a delay, the servo will not move. It needs some time delays to move. (it needs time to move)
I once erased the delays from the example code provided by the app. And the servo just shook.
And the range of input value for servo is 0~180 so the input values are not the problem I guess.

And I also think that the delay have nothing to do with disconnection problem or malfunctioning.
doesn't the values sent from computer simply get lost while the arduino is executing delay function?
The computer will be sending coordinates in a string . One string at a time. right below is the code
written on visual studio.(code written for the transmission of my coordinates from by laptop in c++)

std::ostringstream ostr1;
   std::ostringstream ostr2;
   std::ostringstream ostr3;
   std::ostringstream ostr4;
   ostr1.str("");    // initializing
   ostr2.str("");
   ostr3.str("");
   ostr4.str("");
ostr1 << DrawX ;
   std::string str1 = ostr1.str();
   ostr1.str("");                    // clear
 
   ostr2 << ",";
   std::string str2 = ostr2.str();
   ostr2.str("");
 
   ostr3 <<DrawY;
   std::string str3 = ostr3.str();
   ostr3.str("");
 
   ostr4 << ".";
   std::string str4 = ostr4.str();
   ostr4.str("");
 
   std::string str5;
   str5 = str1 + str2 + str3+ str4;
   std::cout<<str5<<std::endl;
    WriteFile(hComm,&str5,sizeof(str5),&dwWritten,0);

=================================================
the example code in the app StringToIntRGB

/*
  Serial RGB controller
 
 Reads a serial input string looking for three comma-separated
 integers with a newline at the end. Values should be between 
 0 and 255. The sketch uses those values to set the color 
 of an RGB LED attached to pins 9 - 11.
 
 The circuit:
 * Common-anode RGB LED cathodes attached to pins 9 - 11
 * LED anode connected to pin 13
 
 To turn on any given channel, set the pin LOW.  
 To turn off, set the pin HIGH. The higher the analogWrite level,
 the lower the brightness.
 
 created 29 Nov 2010
 by Tom Igoe
 
 This example code is in the public domain. 
 */

String inString = "";    // string to hold input
int currentColor = 0;
int red, green, blue = 0;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // send an intro:
  Serial.println("\n\nString toInt() RGB:");
  Serial.println();
  // set LED cathode pins as outputs:
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  // turn on pin 13 to power the LEDs:
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
}

void loop() {
  int inChar;

  // Read serial input:
  if (Serial.available() > 0) {
    inChar = Serial.read();
  }

  if (isDigit(inChar)) {
    // convert the incoming byte to a char 
    // and add it to the string:
    inString += (char)inChar; 
  }

  // if you get a comma, convert to a number,
  // set the appropriate color, and increment
  // the color counter:
  if (inChar == ',') {
    // do something different for each value of currentColor:
    switch (currentColor) {
    case 0:    // 0 = red
      red = inString.toInt();
      // clear the string for new input:
      inString = ""; 
      break;
    case 1:    // 1 = green:
      green = inString.toInt();
      // clear the string for new input:
      inString = ""; 
      break;
    }
    currentColor++;
  }
  // if you get a newline, you know you've got
  // the last color, i.e. blue:
  if (inChar == '\n') {
    blue = inString.toInt();

    // set the levels of the LED.
    // subtract value from 255 because a higher
    // analogWrite level means a dimmer LED, since
    // you're raising the level on the anode:
    analogWrite(11,  255 - red);
    analogWrite(9, 255 - green);
    analogWrite(10, 255 - blue);

    // print the colors:
    Serial.print("Red: ");
    Serial.print(red);
    Serial.print(", Green: ");
    Serial.print(green);
    Serial.print(", Blue: ");
    Serial.println(blue);

    // clear the string for new input:
    inString = ""; 
    // reset the color counter:
    currentColor = 0;
  }

}


/*
Here's a Processing sketch that will draw a color wheel and send a serial
 string with the color you click on:
 
 // Subtractive Color Wheel with Serial
 // Based on a Processing example by Ira Greenberg. 
 // Serial output added by Tom Igoe
 // 
 // The primaries are red, yellow, and blue. The secondaries are green, 
 // purple, and orange. The tertiaries are  yellow-orange, red-orange, 
 // red-purple, blue-purple, blue-green, and yellow-green.
 // 
 // Create a shade or tint of the subtractive color wheel using
 // SHADE or TINT parameters.
 
 // Updated 29 November 2010.
 
 
 
 import processing.serial.*;
 
 int segs = 12;
 int steps = 6;
 float rotAdjust = TWO_PI / segs / 2;
 float radius;
 float segWidth;
 float interval = TWO_PI / segs;
 
 Serial myPort;
 
 void setup() {
 size(200, 200);
 background(127);
 smooth();
 ellipseMode(RADIUS);
 noStroke();
 // make the diameter 90% of the sketch area
 radius = min(width, height) * 0.45;
 segWidth = radius / steps;
 
 // swap which line is commented out to draw the other version
 // drawTintWheel();
 drawShadeWheel();
 // open the first serial port in your computer's list
 myPort = new Serial(this, Serial.list()[0], 9600);
 }
 
 
 void drawShadeWheel() {
 for (int j = 0; j < steps; j++) {
 color[] cols = { 
 color(255-(255/steps)*j, 255-(255/steps)*j, 0), 
 color(255-(255/steps)*j, (255/1.5)-((255/1.5)/steps)*j, 0), 
 color(255-(255/steps)*j, (255/2)-((255/2)/steps)*j, 0), 
 color(255-(255/steps)*j, (255/2.5)-((255/2.5)/steps)*j, 0), 
 color(255-(255/steps)*j, 0, 0), 
 color(255-(255/steps)*j, 0, (255/2)-((255/2)/steps)*j), 
 color(255-(255/steps)*j, 0, 255-(255/steps)*j), 
 color((255/2)-((255/2)/steps)*j, 0, 255-(255/steps)*j), 
 color(0, 0, 255-(255/steps)*j),
 color(0, 255-(255/steps)*j, (255/2.5)-((255/2.5)/steps)*j), 
 color(0, 255-(255/steps)*j, 0), 
 color((255/2)-((255/2)/steps)*j, 255-(255/steps)*j, 0)
 };
 for (int i = 0; i < segs; i++) {
 fill(cols[i]);
 arc(width/2, height/2, radius, radius, 
 interval*i+rotAdjust, interval*(i+1)+rotAdjust);
 }
 radius -= segWidth;
 }
 }
 
 
 void drawTintWheel() {
 for (int j = 0; j < steps; j++) {
 color[] cols = { 
 color((255/steps)*j, (255/steps)*j, 0), 
 color((255/steps)*j, ((255/1.5)/steps)*j, 0), 
 color((255/steps)*j, ((255/2)/steps)*j, 0), 
 color((255/steps)*j, ((255/2.5)/steps)*j, 0), 
 color((255/steps)*j, 0, 0), 
 color((255/steps)*j, 0, ((255/2)/steps)*j), 
 color((255/steps)*j, 0, (255/steps)*j), 
 color(((255/2)/steps)*j, 0, (255/steps)*j), 
 color(0, 0, (255/steps)*j),
 color(0, (255/steps)*j, ((255/2.5)/steps)*j), 
 color(0, (255/steps)*j, 0), 
 color(((255/2)/steps)*j, (255/steps)*j, 0)
 };
 for (int i = 0; i < segs; i++) {
 fill(cols[i]);
 arc(width/2, height/2, radius, radius, 
 interval*i+rotAdjust, interval*(i+1)+rotAdjust);
 }
 radius -= segWidth;
 }
 }
 
 void draw() {
 // nothing happens here
 }
 
 void mouseReleased() {
 // get the color of the mouse position's pixel:
 color targetColor = get(mouseX, mouseY);
 // get the component values:
 int r = int(red(targetColor));
 int g = int(green(targetColor));
 int b = int(blue(targetColor));
 // make a comma-separated string:
 String colorString = r + "," + g + "," + b + "\n";
 // send it out the serial port:
 myPort.write(colorString );
 }
 
*/