对于Windows应用程序,不会触发SerialPort.DataReceived,在Console App .net2.0中正常工作

时间:2013-06-16 08:11:12

标签: c# .net-2.0

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace ReadWieght
{
    public partial class Form1 : Form
    {
        public delegate void UpdateTextCallback(string text);

        private void Form1_Load(object sender, EventArgs e)
        {
            ReadSP();
        }
        void ReadSP()
        {

            SerialPort mySerialPort = null;
            mySerialPort = new SerialPort("COM1");
            mySerialPort.BaudRate = 2400;
            mySerialPort.Parity = Parity.None;
            mySerialPort.StopBits = StopBits.One;
            mySerialPort.DataBits = 8;
            mySerialPort.Handshake = Handshake.RequestToSend;
            mySerialPort.DtrEnable = true;
            mySerialPort.RtsEnable = true; 

            mySerialPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            try
            {
                mySerialPort.Open();
                // mySerialPort.Write(str.Trim()); 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            mySerialPort.Close();
        }

        // Updates the textbox text.
        private void UpdateText(string text)
        {
            // Set the textbox text.
            label1.Text = text;
        }


        //static string text = null;
        private  void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            this.Invoke(new UpdateTextCallback(this.UpdateText), new object[] { "DATA RECIEVED" });


            try
            {
                SerialPort sp = (SerialPort)sender;
                byte[] comBuffer = new byte[sp.BytesToRead];
                sp.Read(comBuffer, 0, sp.BytesToRead);
                Console.WriteLine(Encoding.ASCII.GetString(comBuffer));
                // char[] chr = Encoding.ASCII.GetString(comBuffer);
                string str = Encoding.ASCII.GetString(comBuffer);
                if (str.Length > 7)
                {
                    str = str.Substring(2, 6);

                    this.Invoke((MethodInvoker)delegate
                    {
                        label1.Text = str; // runs on UI thread
                    }); 
                }
                Encoding.ASCII.GetString(comBuffer);

            }
            catch (Exception ex)
            {
               // MessageBox.Show(ex.Message);
                this.Invoke((MethodInvoker)delegate
                {
                    label1.Text = ex.Message; // runs on UI thread
                }); 
            }

        }

        public Form1()
        {
            InitializeComponent();
        }






}
}

2 个答案:

答案 0 :(得分:2)

mySerialPort变量放在ReadSP函数之外,否则当该函数完成时它将被销毁。在另一个函数中关闭端口。

答案 1 :(得分:0)

你已经注释掉了写入串口的线路,所以它看起来就像你没有沿着串口发送任何数据所以也许这就是为什么没有收到请求?

为了进行测试,我建议使用“回送”,这基本上是连接rx和amp;串行端口上的tx行。您可以使用金属回形针 - 只需将每个末端粘在针脚2和针脚2中。 3.