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

求解 c# SerialPort 接收老是要过很久才能收到
程序如下,电脑发下去之后,设备马上有返回,但是我的接收事件要等很久才能收到,高手指点

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;
using System.IO.Ports;
using System.Threading;

namespace WindowsFormsApplication2
{
  public partial class Form1 : Form
  {
  SerialPort SP1 = new SerialPort();
  List<string> xieyi = new List<string>();

  public Form1()
  {

  InitializeComponent();
  SP1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
   
  }

   

  private void Form1_Load(object sender, EventArgs e)
  {
  string rdline;
  int i;
  comboBox1.SelectedItem=comboBox1.Items[0];
  comboBox2.SelectedItem = comboBox2.Items[0];

  try
  {
  FileStream sFile = new FileStream(".\\d645.txt", FileMode.Open);
  StreamReader sr = new StreamReader(sFile);

  rdline = sr.ReadLine();
  while(rdline!=null)
  {
  xieyi.Add(rdline);
  rdline = sr.ReadLine();
  }

  label1.Text = xieyi[0];

  for (i=1;i<xieyi.Count;i++)
  {
  checkedListBox1.Items.Add(xieyi[i].Substring(0,4));
  checkedListBox1.SetItemChecked(i-1, true);

  }

  }
  catch (Exception ee)
  {
  MessageBox.Show("加载文件失败:"+ee);
  }


   
  }

  private void button1_Click(object sender, EventArgs e)
  {
  try
  {
   
  SP1.PortName = comboBox1.SelectedItem.ToString();
  SP1.BaudRate = Convert.ToInt32(comboBox2.SelectedItem.ToString());
  SP1.Parity = Parity.Even;

  SP1.ReceivedBytesThreshold = 1;
  SP1.Open();
  }
  catch (Exception ee) { MessageBox.Show(comboBox1.SelectedItem.ToString() + "失败:"+ee); }
   
  }

  private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
  {

  }

  private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  {

  }

  private void button2_Click(object sender, EventArgs e)
  {

  }

  private void label2_Click(object sender, EventArgs e)
  {

  }

  private void button2_Click_1(object sender, EventArgs e)
  {
  int i=0;
  while (checkedListBox1.GetItemChecked(i))
  {
  string s = xieyi[i+1];
  int len = s.Length / 2;
  byte[] bytes = new byte[len];
  for (int j = 0; j < len; j++)
  {
&n