日期:2014-05-18  浏览次数:20934 次

SerialPort串口通信中DataReceived事件不能触发?
C# code

  //定义串口
        private SerialPort comm;     
        private long received_count = 0;//接收计数
        private long send_count = 0;//发送计数
        byte c = 51;
        public Form1()
        {
            InitializeComponent();
           
             
        
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            comm = new SerialPort("COM4", 1200);
           
            //添加事件注册
            comm.DataReceived += comm_DataReceived;
            comm.ReceivedBytesThreshold = 1;
            
        }

        //数据接收
        void comm_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int n = comm.BytesToRead;//先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
            byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据
            received_count += n;//增加接收计数
            comm.Read(buf, 0, n);//读取缓冲数据          
            //因为要访问ui资源,所以需要使用invoke方式同步ui。
            this.Invoke((EventHandler)(delegate
            {
                //依次的拼接出16进制字符串   
                string ReceivedStr = GetHexStrFromByte(buf);                
                this.label1.Text = GetAm(ReceivedStr);
               
                
            }));
        }
        

       
        //数据发送
        private void btnGetReader_Click(object sender, EventArgs e)
        {
            string meterNo = "000801386009";
                        if (!comm.IsOpen)
            {
                comm.Open();
            }

          
            string sendText = "FEFEFE" + strMeterNo + strControlCode + strDataLength + strDataFlag + strData + strVerifyCode;
            byte[] sendByte = GetBytesFromHex(sendText);
            //定义一个变量,记录发送了几个字节
            int n = sendByte.Count();
            comm.Write(sendByte, 0, sendByte.Count());
            lblSendText.Text = sendText;
            lblSendCount.Text = n.ToString();         
        }




不知道错在哪里了?如果错了请大家指出来下!或者大家能给相关的代码更好了!先谢谢大家了!

------解决方案--------------------
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.Collections;
using System.Text.RegularExpressions;
using System.IO.Ports;
namespace SerialportSample
{
public partial class SerialportSampleForm : Form
{


private SerialPort comm = new SerialPort();
private StringBuilder builder = new StringBuilder();//避免在事件处理方法中反复的创建,定义到外面。
private long received_count = 0;//接收计数
private long send_count = 0;//发送计数
private bool Listening = false;//是否没有执行完invoke相关操作
private bool Closing = false;//是否正在关闭串口,执行Application.DoEvents,并阻止再次invoke
private List<byte> buffer = new List<byte>(4096);//默认分配1页内存,并始终限制不允许超过
private byte[] binary_data_1 = new byte[9];//AA 44 05 01 02 03 04 05 EA

public SerialportSampleForm()
{
InitializeComponent();
}



void comm_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (Closing) return;//如果正在关闭,忽略操作,直接返回,尽快的完成串口监听线程的一次循环
try
{
Listening = true;//设置标记,说明我已经开始处理数据,一会儿要使用系统UI的。
int n = comm.BytesToRead;//先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据
received_count += n;//增加接收计数
comm.Read(buf, 0, n);//读取缓冲数据

///////////////////////////