简体   繁体   中英

C# Serial Port Communication Arduino

I have a project where I need to communicate with Arduino over the serial ports. The problem I face is that I cannot print continously the data I receive from the serial monitor on multiple lines on a richtextbox. When I press the button "Reveice" I do receive only one value and after this, pressing again the Receive button will overwrite the line.

I'm tring to fix this for few days, but it's my first time programming in c# so I'm asking for your help.

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

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

Print screen of the final app: http://i.imgur.com/5f8EOly.png

Thank you !

I haven't written any Serial applications in C# yet, but already did a few projects involving Java <-> Arduino communication. My first guess would be that you overwrite the existing line with the received line.

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

instead you would want:

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

Also you should take a look at this article on MSDN: https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived(v=vs.110).aspx

This shows how you could use Events to continuously receive text from the Arduino.

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