title: C# Visual studio Computer program for sending data by Serial to arduino (C)
body: <p>I am trying to make a program for controlling axis servos but have a small problem with sending values to the Arduino through serial (USB). It seems data don't arrive in the correct format. For purpose of testing, I made a special Arduino code to see what's wrong.</p>
  <p>Arduino code:</p>
  <pre><code> void setup() {
    // put your setup code here, to run once:
     Serial.begin(9600);
  
  
  }
  String received;
  void loop() {
   
    // put your main code here, to run repeatedly:
    if(Serial.available()&gt;0)
    {
    received=Serial.readString();
    }
    
    Serial.println(&quot;received: &quot;+received);
    
   
  }
  </code></pre>
  <p>C# code:</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using System.IO.Ports;
  using System.Threading;
  
  namespace ServoControl
  {
      public partial class Form1: Form
      {
          public Form1()
          {
              InitializeComponent();
          }
          
          SerialPort Sp = new SerialPort();
          String AngleX = &quot;0&quot;;
          String AngleY = &quot;0&quot;;
          String AngleZ = &quot;0&quot;;
          String rec = &quot;&quot;;
          private void Form1_Load(object sender, EventArgs e)
          {
              
              Sp.PortName = &quot;COM8&quot;;
              Sp.BaudRate = 9600;
              Sp.DataReceived += Sp_DataReceived;
             
          }
  
          private void Sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
          {
  
              rec=Sp.ReadLine();
  
          }
          //when value changed on slider
          private void xAxisServo_Scroll(object sender, EventArgs e)
          {
              if (AngleX!= xAxisServo.Value.ToString()) 
              { 
                  AngleX = xAxisServo.Value.ToString(); valueXaxis.Text = AngleX; 
              }
  
              if (Sp.IsOpen == true &amp; int.Parse(AngleX) &lt;= 180)
              {
                  Sp.WriteLine(AngleX.ToString());
              }
  
              else
              {
                  Sp.Open();
              }
          }
          //when value changed in value box
          private void valueXaxis_TextChanged(object sender, EventArgs e)
          {
              if (int.TryParse(valueXaxis.Text, out int res)==true &amp;&amp; int.Parse(valueXaxis.Text)&lt;=180) 
              { 
                  AngleX = valueXaxis.Text;
                  xAxisServo.Value = int.Parse(AngleX); 
              }
  
              if (int.TryParse(valueXaxis.Text, out int res2) == true &amp;&amp; int.Parse(valueXaxis.Text) &gt; 180) 
              { 
                  AngleX = &quot;180&quot;;
                
              }
  
  
              if (Sp.IsOpen == false) 
              { 
                  Sp.Open(); 
              }
  
              if (Sp.IsOpen == true) 
              { 
                  Sp.WriteLine(AngleX.ToString());
  
              }
             
          }
  
          private void timer1_Tick(object sender, EventArgs e)
          {
              textBox1.Text = rec;
          }
  
      }
  }
  </code></pre>
  <p>I was able to send one value for axis X by function Serial.parseInt(); I was able to read a value from a string in Arduino, but I need to send more than one and I have problem with sending and receiving string.</p>
  <p>I have tested a lot with Arduino serial monitor and it seems when sending through builtin monitor in Arduino IDE it works as expected got correct string back, but when using C# checking through textbox1 it shows only received and when not interrupted shows an only bunch of nonsense changing every second.
  In reaction to that, I've tried to replace WriteLine with Write because I don't know the background of Arduino very well so I don't how Arduino proceeds data using <code>Serial.readString();</code>.</p>
  <p>I've tried to find how builtin serial monitor in Arduino IDE works if in C# or if not in C# at least how are data send.<br />
  Any idea why I am getting different results with IDE serial monitor and C#.</p>