C# serial port does not show output in textbox

241 views Asked by At

Hi all I am pretty new to C# and Arduino. When i send to text to my Arduino it does not send it back to the textbox in the app. I can send it to my Arduino but I get the error when the message I send has to be in the textbox in the application.

Here is the part I am getting a error:

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{   
    Output.Text = serialPort1.ReadExisting();
}

Here is my code:

public partial class ArduinoSerial : Form
{
    public ArduinoSerial()
    {
        InitializeComponent();
        string[] serialPorts = System.IO.Ports.SerialPort.GetPortNames();
        cboPorts.Items.AddRange(serialPorts);
        cboBaud.Items.Add(2400);
        cboBaud.Items.Add(4800);
        cboBaud.Items.Add(9600);
        cboBaud.Items.Add(14400);
        cboBaud.Items.Add(19200);
        cboBaud.Items.Add(28800);
        cboBaud.Items.Add(38400);
        cboBaud.Items.Add(57600);
        cboBaud.Items.Add(115200);
        cboPorts.SelectedIndex = 0;
        cboBaud.SelectedIndex = 2;
    }

    private void cboPorts_SelectedIndexChanged(object sender, EventArgs e)
    {
        string[] serialPorts = System.IO.Ports.SerialPort.GetPortNames();
        cboPorts.Items.AddRange(serialPorts);
    }

    private void cboBaud_SelectedIndexChanged(object sender, EventArgs e)
    {
        cboBaud.Items.Add(2400);
        cboBaud.Items.Add(4800);
        cboBaud.Items.Add(9600);
        cboBaud.Items.Add(14400);
        cboBaud.Items.Add(19200);
        cboBaud.Items.Add(28800);
        cboBaud.Items.Add(38400);
        cboBaud.Items.Add(57600);
        cboBaud.Items.Add(115200);
    }

    private void btnStart_Click(object sender, EventArgs e)
    {
        serialPort1.PortName = cboPorts.SelectedItem.ToString();
        serialPort1.BaudRate = Convert.ToInt32(cboBaud.SelectedItem.ToString());

        if (!serialPort1.IsOpen)
        {
            btnStart.Enabled = false;
            btnStop.Enabled = true;
            serialPort1.Open();
        }
    }

    private void btnStop_Click(object sender, EventArgs e)
    {
        if (serialPort1.IsOpen)
        {
            btnStart.Enabled = true;
            btnStop.Enabled = false;
            serialPort1.Close();
        }
    }

    private void btnSend_Click(object sender, EventArgs e)
    {
        if (!serialPort1.IsOpen) return;

        serialPort1.Write(txtInput.Text + "\n");
    }

    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {   
        Output.Text = serialPort1.ReadExisting();
    }
}

My ino file

/*
 Name:      LOOP_C.ino
 Created:   9/8/2017 10:51:31 AM
 Author:    Jari
*/

// the setup function runs once when you press reset or power the board
void setup() {
    Serial.begin(9600);
}

// the loop function runs over and over again until power down or reset
void loop() {
    while (Serial.available()) {
        Serial.write(Serial.read());
    }
}

I am already trying for over 2 hours, I can't find the answer anywhere.

1

There are 1 answers

0
Tom Schardt On

Try this if you encounter a CrossThreadException:

            private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {   
                SetText(serialPort1.ReadExisting());
            }

            private void SetText(string text)
            {
                if (Output.InvokeRequired)
                {   
                    SetTextCallback d = new SetTextCallback(SetText);
                    this.Invoke(d, new object[] { text });
                }
                else
                {
                    Output.Text = text;
                }
            }

I donĀ“t have a compiler here so maybe you need to fix something. If so - Sorry