C#串口通信Arduino

时间:2016-12-18 12:43:54

标签: c# arduino serial-port

我有一个项目需要通过串口与Arduino进行通信。我面临的问题是,我无法连续打印从richtextbox上的多行上的串行监视器收到的数据。当我按下“Reveice”按钮时,我只收到一个值,然后再次按下“接收”按钮将覆盖该行。

我想解决这个问题几天,但这是我第一次用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;

namespace aplicatie_comanda_v1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            getAvilablePorts();


        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

            serialPort1.Close();
            progressBar1.Value = 0;
            button3.Enabled = true;
            button1.Enabled = false;
            receive.Enabled = false;
            richTextBox1.Clear();

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
        void getAvilablePorts()
        {
            string[] ports = SerialPort.GetPortNames();
            comboBox1.Items.AddRange(ports);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {


        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.Text == "" || comboBox2.Text == "" && serialPort1 != null && serialPort1.IsOpen)
                {
                    richTextBox1.Text = "Select COM port and BAUD rate !";
                    serialPort1.Close();
                }
                else
                {
                    string cmd = Convert.ToString(comboBox1.Text);
                    int baud = Convert.ToInt32(comboBox2.Text);
                    serialPort1.PortName = cmd;
                    serialPort1.BaudRate = baud;
                    serialPort1.DtrEnable = true;
                    serialPort1.RtsEnable = true;
                    serialPort1.Open();
                    progressBar1.Value = 100;
                    button1.Enabled = true;
                    button2.Enabled = true;
                    textBox1.Enabled = true;
                    button3.Enabled = false;

                }
            }
            catch (UnauthorizedAccessException)
            {
                richTextBox1.Text = "Unauthorized !";
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            string text = textBox1.Text;
            serialPort1.Write(text);

        }

        private void receive_Click(object sender, EventArgs e)
        {
            try
            {
                richTextBox1.Text = serialPort1.ReadLine() + "\n";

            }
            catch (TimeoutException)
            {
                richTextBox1.Text = "Timeout !";
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {

            serialPort1.Write("w");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            serialPort1.Write("s");
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {
            serialPort1.Write("a");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            serialPort1.Write("d");
        }

        private void button12_Click(object sender, EventArgs e)
        {
            serialPort1.Write("b");
        }

        private void button13_Click(object sender, EventArgs e)
        {
            string cmd = Convert.ToString(trackBar1.Value);
            serialPort1.Write(cmd);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            serialPort1.Write("q");
        }

        private void button11_Click(object sender, EventArgs e)
        {
            serialPort1.Write("e");
        }

        private void button9_Click(object sender, EventArgs e)
        {
            serialPort1.Write("z");
        }

        private void button10_Click(object sender, EventArgs e)
        {
            serialPort1.Write("c");
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {


        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            while (serialPort1.IsOpen)
            {
                try
                {
                    string date = serialPort1.ReadLine();
                    richTextBox1.Text = date + "\n";

                }
                catch (TimeoutException)
                {
                    richTextBox1.Text = "Timeout !";
                }
            }
        }
    }
}

最终应用的打印屏幕:hole punching

谢谢!

1 个答案:

答案 0 :(得分:2)

我还没有在C#中编写任何串行应用程序,但已经做了一些涉及Java< - >的项目。 Arduino沟通。 我的第一个猜测是你用接收的行覆盖现有的行。

richTextBox1.Text = serialPort1.ReadLine() + "\n";
相反,你会想要:

richTextBox1.Text += serialPort1.ReadLine() + "\n";

另外你应该看看MSDN上的这篇文章: https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived(v=vs.110).aspx

这显示了如何使用事件连续接收Arduino中的文本。