专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > WinCE

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

发布时间:2010-06-13 22:28:55 文章来源:www.iduyao.cn 采编人员:星星草
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")
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: