简体   繁体   中英

C# Visual studio Computer program for sending data by Serial to arduino (C)

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.

Arduino 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()>0)
  {
  received=Serial.readString();
  }
  
  Serial.println("received: "+received);
  
 
}

C# 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 = "0";
        String AngleY = "0";
        String AngleZ = "0";
        String rec = "";
        private void Form1_Load(object sender, EventArgs e)
        {
            
            Sp.PortName = "COM8";
            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 & int.Parse(AngleX) <= 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 && int.Parse(valueXaxis.Text)<=180) 
            { 
                AngleX = valueXaxis.Text;
                xAxisServo.Value = int.Parse(AngleX); 
            }

            if (int.TryParse(valueXaxis.Text, out int res2) == true && int.Parse(valueXaxis.Text) > 180) 
            { 
                AngleX = "180";
              
            }


            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;
        }

    }
}

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.

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 Serial.readString(); .

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.
Any idea why I am getting different results with IDE serial monitor and C#.

Concatenate the Serial.read then add a terminating character to cut the concatenation:

char received;

while(Serial.available()>0)
{
  if(received=='#') // put the # in the end of your data
  { 
   Serial.println("received: "+String(received));
   break;
  }
  received+=Serial.read();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM