C#串行端口不会在文本框中显示输出



嗨,我对C#和arduino非常新。当我将文本发送到我的Arduino时,它不会将其发送回应用程序中的文本框。我可以将其发送到我的arduino,但是当我发送的消息必须在应用程序中的文本框中时,我会发现错误。

这是我遇到错误的部分:

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

这是我的代码:

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();
    }
}

我的ino文件

/*
 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());
    }
}

我已经尝试了2个多小时,在任何地方都找不到答案。

如果遇到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;
                }
            }

我在这里没有编译器,所以也许您需要修复一些内容。如果是这样 - 对不起

相关内容

  • 没有找到相关文章

最新更新