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;
using System.Threading;namespace ETT985
{
    public partial class Nmea : Form
    {
        MsgDispatcher msgDispatcher;
        SerialPort m_CommPort;
        string portname ="COM3";
        public Nmea()
        {
            
            InitializeComponent();
            msgDispatcher = new MsgDispatcher();
            
        }        private void Nmea_Load(object sender, EventArgs e)
        {
            m_CommPort = new SerialPort(portname, 4800, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
            msgDispatcher.nmeaSelected();
            if (!m_CommPort.IsOpen)
            {
                m_CommPort.Open();
            }
      
            m_CommPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);        }
        private void Nmea_Close(object sender, EventArgs e)
        {
            SelectTestForm.f.Show();
            msgDispatcher.getmain();
        }        private void button1_Click(object sender, EventArgs e)
        {
            string msg =null;
            msg = textBox1.Text.ToString();
            m_CommPort.WriteLine(msg);
        }
        void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            label2.Text= m_CommPort.ReadLine().ToString();
            MessageBox.Show(label2.Text.ToString());
        }        private void button2_Click(object sender, EventArgs e)
        {        }
 
    }
}
这个是我form的.cs文件
    // 
            // Nmea
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1134, 592);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.label2);
            this.Name = "Nmea";
            this.Text = "Nmea";
            this.Load += new System.EventHandler(this.Nmea_Load);
            this.Closed += new System.EventHandler(this.Nmea_Close);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label1;
        private System.IO.Ports.SerialPort serialPort1;
        private System.Windows.Forms.Button button2;
这个是design文件。个人感觉是事件没添加,不过看了好多人的讲解都没有添加这个好像这个确实发出去数据了,但是我没接收到串口通信c#对象

解决方案 »

  1.   

    个人感觉是没有添加事件,但是看了好多案例都没有添加,还有说加serialport控件的,加了没看出来有什么用。。
    希望有人帮忙回答下,谢谢
      

  2.   

    1. 断点看看,DataReceived事件触发了吗。
    2. 不能在这个事件里面直接操作Form。
      

  3.   

    3楼说的正确,1、可以通过程序添加端点。另外一端使用串口调试工具发送数据,看接收线程是否执行。
    2、 数据接收线程中不能修改操作窗体控件(需要情况下,使用Invoke方法)。
      

  4.   

     数据接收线程中不能修改操作窗体控件(需要情况下,使用Invoke方法)。举个例子??
      

  5.   

    线程中通过Invoke 方法调用刷新界面处理。
    具体可以网上搜下。
    给你一个链接,你参考下,应该是你要的东西。http://www.cnblogs.com/yjg8116/archive/2011/12/22/2298398.html