winCE5.0下串口程序无法接收数据!,请高手帮忙!! SOS...
下面是我在VS2005下用C#编写的,用在winCE5.0下的一个串口程序, 编译可以成功,可以发送字符串,但是不能接收,会出现这样的提示"Control.Invoke必须用于与在独立线程上创建的控件交互"!请问哪位高手可以帮一下我吗?实在没办法了!!!SOS。。。。。。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace TestSerialPort
{
public partial class frmTESTSerialPort : Form
{
//实例化串口对象(默认:COM1,9600,e,8,1)
SerialPort myserialPort = new SerialPort();
//串口控件初始化
public frmTESTSerialPort()
{
//Form.CheckForIllegalCrossThreadCalls = false; //如果加上这句话,在编译时出现了以下错误, //“System.Windows.Form.Form”并不包含“CheckForIllegalCrossThreadCalls”的定义
InitializeComponent();
}
//窗口载入
private void frmTESTSerialPort_Load(object sender, EventArgs e)
{
//更改参数
myserialPort.PortName = "COM1 ";
myserialPort.BaudRate = 9600;
myserialPort.Parity = Parity.None;
myserialPort.DataBits = 8;
myserialPort.StopBits = StopBits.One;
myserialPort.ReadBufferSize = 4096;
//上述步骤可以用在实例化时调用SerialPort类的重载构造函数
//SerialPort serialPort = new SerialPort("COM1 ", 9600, Parity.None, StopBits.One);
}
//开启串口
private void button1_Click(object sender, EventArgs e)
{
//打开串口(打开串口后不能修改端口名,波特率等参数,修改参数要在串口关闭后修改)
if (myserialPort.IsOpen == false)
{
myserialPort.Open();
MessageBox.Show("串口开启成功");
}
}
//发送数据
private void button2_Click(object sender, EventArgs e)
{
//发送数据
SendStringData(myserialPort);
}
//发送字符串数据
private void SendStringData(SerialPort serialPort)
{
try
{
serialPort.Write(txtSend.Text);
}
catch
{
MessageBox.Show("请先打开串口");
}
}
//发送二进制数据
private void SendBytesData(SerialPort serialPort)
{
byte[] bytesSend = System.Text.Encoding.Default.GetBytes(txtSend.Text);
serialPort.Write(bytesSend, 0, bytesSend.Length);
}
//接收\读取数据
public void button3_Click(object sender, EventArgs e)
{
//同步阻塞接收数据线程
Thread threadReceive = new Thread(new ThreadStart(SynReceiveData));//修改
threadReceive.Start();
//也可用异步接收数据线程
//Thread threadReceiveSub = new Thread(new ParameterizedThreadStart(AsyReceiveData));
//threadReceiveSub.Start(serialPort);
}
//同步阻塞读取
private void SynReceiveData()
{
MessageBox.Show("同步阻塞读取线程启动");
SerialPort serialPort = myserialPort;
System.Threading.Thread.Sleep(0);
try
{
//阻塞到读取数据或超时(这里为秒)
serialPort.ReadTimeout = 6000;
//下面代码要用同步的方式readtimeout起作用
byte[] bufReceive=new byte[1024];
int bytesRead = serialPort.Read(bufReceive, 0, bufReceive.Length);
byte[] bytesData = new byte[bytesRead];
for (int i = 0; i <= bytesRead - 1; i++)
{
bytesData = bufReceive;
}
txtReceive.Text =txtReceive.Text + System.Text.Encoding.Default.GetString(bytesData, 0, bytesRead);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
//处理超时错误
}
}
public delegate void SetText(string value);
private void SetTextBox(string value)
{
txtReceive.Text=txtReceive.Text+value;
}
//异步读取
private void AsyReceiveData(object serialPortobj)
{
SerialPort serialPort = (SerialPort)serialPortobj;
System.Threading.Thread.Sleep(500);
try
{
SetText set = new SetText(SetTextBox);
txtReceive.Invoke(set,serialPort.ReadExisting());
}
catch (Exception e)
{
MessageBox.Show(e.Message);
//处理错误
}
}
private void button4_Click(object sender, EventArgs e)
{
if (myserialPort.IsOpen == true)
{
myserialPort.Close();
MessageBox.Show("串口关闭成功");
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void txtSend_TextChanged(object sender, EventArgs e)
{
}
private void txtReceive_TextChanged(object sender, EventArgs e)
{
}
}
}
问题补充:
这个程序不是小弟本人写的,我对这个程序还不是很懂,请各位帮忙把解决方案写得稍祥细一些,谢谢了!!