简体   繁体   English

C# Visual Studio 用于通过串行方式向 arduino 发送数据的计算机程序 (C)

[英]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).我正在尝试制作一个用于控制轴伺服的程序,但在通过串行(USB)向 Arduino 发送值时遇到了一个小问题。 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 代码,看看有什么问题。

Arduino code: Arduino 代码:

 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: C# 代码:

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();我能够通过 function Serial.parseInt() 为轴 X 发送一个值; 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.我能够从 Arduino 中的字符串中读取一个值,但我需要发送多个,并且在发送和接收字符串时遇到问题。

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. 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每一秒都在变化。 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();对此,我尝试用 Write 替换 WriteLine,因为我不太了解 Arduino 的背景,所以我不知道 Arduino 如何使用 Serial.readString 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.我试图找到 Arduino IDE 中的内置串行监视器是如何工作的
Any idea why I am getting different results with IDE serial monitor and C#.知道为什么我使用 IDE 串行监视器和 C# 得到不同的结果。

Concatenate the Serial.read then add a terminating character to cut the concatenation:连接 Serial.read 然后添加一个终止字符来切断连接:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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