当前位置: 代码迷 >> WinCE >> WinCE5 C# serialport控件的 接收数据 有关问题
  详细解决方案

WinCE5 C# serialport控件的 接收数据 有关问题

热度:464   发布时间:2016-04-28 13:56:26.0
WinCE5 C# serialport控件的 接收数据 问题!
问题说明:
  我在WinCE5环境里,使用vs2005(C#)编写串口Demo ,使用serialport控件,现在数据接收不行,查看资料后说需要适用委托,我按照例子更改后仍然不能接收,将接收这一段的代码贴出来,请大家指点!
  using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO.Ports;
using System.Threading;

namespace DrawList
{
  public partial class TestCOM : Form
  {
  //[DllImport("kernel32.dll", EntryPoint="CreateFile")]  

  delegate void HandleInterfaceUpdateDelegate(string text); //委托
  HandleInterfaceUpdateDelegate interfaceUpdateHandle;

  public TestCOM()
  {
  InitializeComponent();
  Set_SerialPort(0);
  }

  byte[] WriteBuf = new byte[255];
  byte[] ReadBuf = new byte[255];
  int TimeNum = 0;
  private void button1_Click(object sender, EventArgs e)
  {
  try
  {
  textBox1.Text = "";
  String[] Sendstr = textBox2.Text.Split(' ');
  int writelen = Sendstr.Length;
  for (int i = 0; i < Sendstr.Length; i++)
  {
  WriteBuf[i] = Convert.ToByte(Sendstr[i], 16);
  }
   
  Set_SerialPort(0);
  serialPort1.Open();
   
  serialPort1.Write(WriteBuf, 0,writelen);
  //serialPort1.WriteLine(textBox2.Text);

  serialPort1.Close();
  }
  catch (Exception ex)
  {
  textBox1.Text = ex.Message.ToString();
  }  
  }
  void Set_SerialPort(int bl)
  {
  if (bl == 0)
  {
  //serialPort1.PortName = "COM" + textBox3.Text;
  if (comboBox3.Text != "")
  {
  serialPort1.PortName = comboBox3.Text;
  }
  else
  {
  serialPort1.PortName = "COM3";
  }
  if (comboBox1.Text == "")
  {
  serialPort1.BaudRate = 9600;
  }
  else
  {
  serialPort1.BaudRate = Convert.ToInt32(comboBox1.Text);
  }

  serialPort1.DataBits = Convert.ToInt32(numericUpDown1.Value);

  if (serialPort1.DataBits == 7)
  {
  serialPort1.StopBits = System.IO.Ports.StopBits.Two;
  }
  if (serialPort1.DataBits == 8)
  {
  serialPort1.StopBits = System.IO.Ports.StopBits.One;
  }
  if (serialPort1.DataBits == 9)
  {
  serialPort1.StopBits = System.IO.Ports.StopBits.None;
  }
  if (comboBox2.Text == "Odd")
  {
  serialPort1.Parity = System.IO.Ports.Parity.Odd;
  }
  else if (comboBox2.Text == "Even")
  相关解决方案