Hey Guys,

I am pretty new to C#.

Below is some code that i am using to send data serially to an Arduino controller. It just has some push buttons which move the wheels/motors etc of an RC car i have. I have put some text boxes onto the form and want to let any numbers put in the boxes to be sent to the controller in the same way im sending the button press information.

Im a bit of a n00b and dont know how to do it.

Do i nest the text box data inside a button loop or something

any help would be appreciated. Thanks

Michael D

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace ArduinoController
{
    public partial class Form1 : Form
    {
        SerialPort port;

        public Form1()
        {
            InitializeComponent();
            port = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
            port.Open();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            port.Write("i");     //Value 105 sent serially to Arduino
        }

        private void button2_Click(object sender, EventArgs e)
        {
            port.Write("o");     //Value 111 sent serially to Arduino
        }

        private void button3_Click(object sender, EventArgs e)
        {
            port.Write("p");     //Value 112 sent serially to Arduino
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
          ??????????????
        }

    }
}
foreach (char c in TextBox2.Text.ToCharArray()) {
    port.Write(c);
}

This will send each character in the textbox (if your Write method supports characters, otherwise convert them to strings :))

You might want to pick a different event to send on, as TextChanged will fire when the text changes. So if the user is trying to type "123", you will get "1", "12" and "123".

foreach (char c in TextBox2.Text.ToCharArray()) {
    port.Write(c);
}

This will send each character in the textbox (if your Write method supports characters, otherwise convert them to strings :))

You might want to pick a different event to send on, as TextChanged will fire when the text changes. So if the user is trying to type "123", you will get "1", "12" and "123".

Cool, i could have a button to send textbox data after the user has entered it

does this code look ok?

private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
return;
string strAdd = textBox1.Text;
port.Write(strAdd);
}

Sure, but it can be shorter:

private void button3_Click(object sender, EventArgs e) {
    if (textBox1.Text != String.Empty) {
        port.Write(textBox1.Text);
    }
}

Line 2 makes sure there is something in the textbox by 'asking' if it is not (!=) the same as String.Empty. Line 3 accesses the text field directly, no need to move the text into a variable.

Sure, but it can be shorter:

private void button3_Click(object sender, EventArgs e) {
    if (textBox1.Text != String.Empty) {
        port.Write(textBox1.Text);
    }
}

Line 2 makes sure there is something in the textbox by 'asking' if it is not (!=) the same as String.Empty. Line 3 accesses the text field directly, no need to move the text into a variable.

Hey Thanks Momerath,

ill implement this code and update you how it goes

:)

Hey The code works. pretty sweet.

Thanks for the help

:icon_surprised:

Hey Momerath,

i was just wondering if you (or anyone else) could help with another problem i am having.

I am having problems with the data types i am sending to the arduino controller.

When i put a letter in the text box i can get the corresponding decimal value of the ASCII code and tell my controller program to do something when "i" (which equals = 105) is seen at the serial port.

One of my commands is move wheels left on button press. If i put the wright letter in the textbox i.e. "i (=105)" it will do the wheel turn. So i thought if i convert the string from a string to a decimal then send that serially the controller should accept the decimal number and turn the wheels left. But im not getting what i want. Is there a way to see what im sending to the controller?

Here is the code i am using to change the string to a decimal

private void button3_Click(object sender, EventArgs e)
        {
            decimal Distance = 0;

            if (textBox2.Text != String.Empty)
            {
                Distance = System.Convert.ToDecimal(textBox2.Text);
                port.Write("Distance");
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.