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")
{
serialPort1.Parity = System.IO.Ports.Parity.Even;
}
else if (comboBox2.Text == "None")
{
serialPort1.Parity = System.IO.Ports.Parity.None;
}
else if (comboBox2.Text == "")
{
serialPort1.Parity = System.IO.Ports.Parity.None;
}
interfaceUpdateHandle = new HandleInterfaceUpdateDelegate(AddText); //实例化委托对象
serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
serialPort1.ReceivedBytesThreshold = 1;
serialPort1.WriteBufferSize = 1000;
serialPort1.WriteTimeout = 1000;
serialPort1.ReadBufferSize = 1000;
serialPort1.ReadTimeout = 1000;
}
else if (bl == 1)
{
serialPort1.WriteBufferSize = 1000;
serialPort1.WriteTimeout = 1000;
serialPort1.ReadBufferSize = 1000;
serialPort1.ReadTimeout = 1000;
serialPort1.PortName = "COM3";
serialPort1.BaudRate = 9600;
serialPort1.DataBits = 8;
serialPort1.StopBits = StopBits.One;
serialPort1.Parity = Parity.None;
}
}
#region Get COM number
private void TestCOM_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;
string[] ports = SerialPort.GetPortNames();
for (int i=0; i < ports.Length; i++)
{
comboBox3.Items.Add(ports[i].ToString());
}
}
#endregion
private void button2_Click(object sender, EventArgs e)//Close this Form
{
timer1.Enabled = false;
serialPort1.Close();
Close();
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//StringBuilder datatext;
//datatext = new StringBuilder();
String datatext = "";
if (!serialPort1.IsOpen)
{
serialPort1.Open();
}
if (serialPort1.ReceivedBytesThreshold > 0)
{
byte[] tmpbuf = new byte[serialPort1.BytesToRead];
serialPort1.Read(tmpbuf, 0, serialPort1.BytesToRead);
Thread.Sleep(50);
for (int i = 0; i < serialPort1.BytesToRead; i++)
{
//datatext.Append(tmpbuf[i].ToString() + " " + i.ToString() + " ");
datatext = datatext + tmpbuf[i].ToString() + " " + i.ToString() + " ";
}
this.textBox1.Invoke(interfaceUpdateHandle, datatext.ToString());
//AddText(datatext);
}
}
private void AddText(string text)
{
textBox1.Text += text.ToString();
}
}
}
[解决办法]
用这个
if (textBox1..InvokeRequired)
{
this.lblMsg.Invoke(new EventHandler(this.AddText), new object[] { datatext.ToString(), EventArgs.Empty });
}
private void AddText(object sender,eventargs e)
{
textBox1.Text += sender.ToString();
}
good luck