Visual Studio 2010 C#DataReceived似乎没有触发

时间:2012-06-25 22:52:23

标签: c# visual-studio-2010 arduino

Visual Studio 2010 C#代码:

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;

namespace YamanPonics
{
    public partial class Form1 : Form
    {
        string RxString;

        //Default SerialPortStatus equals TRUE when first starting up
        Boolean serialPortDisconnected = true;

        public Form1()
        {
            InitializeComponent();

            //Add available Serial COM ports to combobox
            foreach (string ports in System.IO.Ports.SerialPort.GetPortNames())
            {
                //MessageBox.Show("Serial port avialible" + " " + ports);
                comPortCmbBox.Items.Add(ports);
            }
        }

        private void DisplayText(object sender, EventArgs e)
        {
            serialMsgViewerRchTxt.AppendText(RxString);
            MessageBox.Show("Displayed Serial Text!");
        }

        private void serialPort1_DataReceived (object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {   
            RxString = serialPort1.ReadExisting();
            this.Invoke(new EventHandler(DisplayText));
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //If SerialPort1 IsOpen
            if (serialPort1.IsOpen)
            {
                //Close SerialPort1 communication
                serialPort1.Close();
            }
        }


        private void connectDisconnectBtn_Click(object sender, EventArgs e)
        {
            //Set arduinoComPort value to COM Port value
            string arduinoComPort = comPortCmbBox.Text;

            //if SerialPortStatus boolean equals FALSE then
            if (serialPortDisconnected &&  (arduinoComPort != ""))
            {

                //Set serialPort1 BaudRate value to default value of 38400(required for atlas-scientific sensors)
                serialPort1.BaudRate = 38400;

                //Set serialPort1 Read and Write timeout values
                serialPort1.ReadTimeout = 250;
                serialPort1.WriteTimeout = 250;

                //Set serialPort1 DataBits value
                serialPort1.DataBits = 8;

                //Open serialPort1 communication
                serialPort1.Open();

                //Change connectDisconnectBtn text to Disconnect
                connectDisconnectBtn.Text = "Disconnect";

                //Set serialPortDisconnected to FALSE
                serialPortDisconnected = false;

            }
            else //if SerialPortStatus bollean equals TRUE
            {
                //Close SerialPort1 communication
                serialPort1.Close();

                //Set connectDisconnectBtn text to Connect
                connectDisconnectBtn.Text = "Connect";

                //Set serialPortDisconnected to TRUE
                serialPortDisconnected = true;
            }
        }

        private void SendBtn_Click(object sender, EventArgs e)
        {
            //if serialPort1 IsOpen then
            if (serialPort1.IsOpen)
            {
                serialPort1.Write("{ph}");
            }
        }
    }
}

该代码是一个简单的串行连接/断开连接和发送/接收应用程序。连接,断开和发送正常工作。当我的arduino收到一个命令时,它会发回一个响应。我的2010 C#应用程序没有收到richtextbox中的响应,也不明白为什么。当我使用另一个串行终端程序时,我可以收到响应,因此我确实知道正在发送数据。我没有做什么才能成功收到回复?

1 个答案:

答案 0 :(得分:2)

您确定serialPort1.DataReceived事件是否正确连接?我看到你有处理程序方法,但我没有看到你订阅该事件。

你需要在某处:

serialPort1.DataReceived += serialPort1_DataReceived;