Not sending Serial data in C# application

2019-01-29 12:44发布

问题:

I have two Arduinos that allow messages exchange via Serial Port using Serial Monitor.

If I use Serial Monitor in both sides everything works fine. If I use my C# application nothing happens. I tried to send from Serial Monitor for C# App and it works but not the reverse.

// ...
comPort1.Open();
// ...
private void comPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    this.Invoke(new EventHandler(processData));
}
private void processData(object sender, EventArgs e)
{
    string inData = comPort1.ReadExisting();
    msgBoxLog.AppendText(inData);
}
// ...
private void sendButton_Click(object sender, EventArgs e)
{
    string my_str = "my string";
    msgBoxLog.AppendText(msgBox.Text + my_str);
    comPort1.Write(msgBox.Text);
}

RtsEnable and DtrEnable are both Enabled

回答1:

Well, with Console.Write(msgBox.Text); I realized it was just a silly problem, I was not sending msgBox.Text as I wanted. It should be:

private void sendButton_Click(object sender, EventArgs e)
{
    string my_str = "my string";
    comPort1.Write(msgBox.Text); //Console.Write(msgBox.Text);
    msgBoxLog.AppendText(msgBox.Text + my_str);
}