winCE5.0下串口程序无法接收数据!,
winCE5.0下串口程序无法接收数据!,请高手帮忙!! SOS...
下面是我在VS2005下用C#编写的,用在winCE5.0下的一个串口程序,在编译时出现了以下错误:
——“System.Windows.Form.Form”并不包含“CheckForIllegalCrossThreadCalls”的定义——如果去掉这句话“Form.CheckForIllegalCrossThreadCalls = false;” 编译可以成功,可以发送字符串,但是不能接收,会出现这样的提示"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;
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);
//处理超时错误
}
}
//异步读取
private void AsyReceiveData(object serialPortobj)
{
SerialPort serialPort = (SerialPort)serialPortobj;
System.Threading.Thread.Sleep(500);
try
{
txtReceive.Text = txtReceive.Text + 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)
{
}
}
}
这个程序可以实现在windows XP上的串口通信,能发送字符串也能接收字符串。但是我把它转换成winCE5.0的程序,编译时出现了这样的错误:
——“System.Windows.Form.Form”并不包含“CheckForIllegalCrossThreadCalls”的定义——
如果去掉这句话“Form.CheckForIllegalCrossThreadCalls = false;”,编译可以成功,可以发送字符串,但是不能接收, 会出现这样的提示"Control.Invoke必须用于与在独立线程上创建的控件交互"!请问哪位高手可以帮一下我吗?实在没办法了!!!SOS。。。。。。
问题补充:
这个程序不是小弟本人写的,我对这个程序还不是很懂,请各位帮忙把解决方案写得稍祥细一些,谢谢了!!
[解决办法]
CheckForIllegalCrossThreadCalls在CE下不能用吧,你可以换成替代的API试试阿