超难--异步调用
public void startCheck(CommunityView parentForm)
{
CSettings readXml=new CSettings();
int threadId;
try
{
Comm.PortNum="com"+readXml.ComPort.Tables[0].Rows[0][0].ToString();
Comm.ReadTimeout=100000;
Comm.BaudRate=int.Parse(readXml.ComPort.Tables[0].Rows[0][1].ToString());
Comm.ByteSize=byte.Parse(readXml.ComPort.Tables[0].Rows[0][2].ToString());
string parityString=readXml.ComPort.Tables[0].Rows[0][3].ToString();
byte parity;
switch(parityString)
{
case "偶":
parity=0;
break;
case "奇":
parity=1;
break;
case "无":
parity=2;
break;
case "标志":
parity=3;
break;
default:
parity=4;
break;
}
Comm.Parity=parity;
string stopbits=readXml.ComPort.Tables[0].Rows[0][4].ToString(); if(stopbits=="1")
stopbits="0";
else if(stopbits=="1.5")
stopbits="1";
else
stopbits="2";
Comm.StopBits=byte.Parse(stopbits);
bool comState=Comm.Open();
if(comState==true)
{ parentForm.menuItem8.Enabled=false;
parentForm.menuItem9.Enabled=true;
parentForm.Start.Enabled=false;
parentForm.Stop.Enabled=true;
//byte[] abcd = null;
AsyncCallback t1 = new AsyncCallback(Comm.Read);
}
}
catch(Exception e)
{
MessageBox.Show(e.Message.ToString(),"warning");
}
}
public void Read(IAsyncResult asyncResult)
{
DataParser myData=new DataParser();
//int NumBytes
byte[] BufBytes=null;
byte[] OutBytes;
BufBytes = new byte[1];
if (hComm!=INVALID_HANDLE_VALUE)
{
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesRead=0;
StreamWriter sw= new StreamWriter("datafile\\inspect.data",true,System.Text.Encoding.ASCII);
sw.WriteLine();
sw.Write(System.DateTime.Now.ToString()+" : ");
sw.Close();
do
{
BytesRead=1;
ReadFile(hComm,BufBytes,1,ref BytesRead,ref ovlCommPort);
OutBytes = new byte[BytesRead];
Array.Copy(BufBytes,OutBytes,BytesRead);
myData.creatDataFile(OutBytes);
}while (BytesRead > 0);
}
else
{
//throw(new ApplicationException("串口未打开!"));
MessageBox.Show("串口未打开");
}
}
public void Write(byte[] WriteBytes)
{
if (hComm!=INVALID_HANDLE_VALUE)
{
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesWritten = 0;
WriteFile(hComm,WriteBytes,WriteBytes.Length,ref BytesWritten,ref ovlCommPort);
}
else
{
throw(new ApplicationException("串口未打开!"));
}
}
哪个能把它写成异步调用的方法呢主要是写read方法的函数异步
问题点数:100、回复次数:12Top
1 楼jiezhi(风满袖)回复于 2004-09-01 12:44:02 得分 10
http://www.codeproject.com/dotnet/DotNetComPorts.asp
一个封装好的操作串口的类。Top
2 楼khpcg(欢乐英雄)回复于 2004-09-01 12:55:54 得分 10
it is too dificult for me to help you.i am sorryTop
3 楼zhushizu(从来就没有救世祖)回复于 2004-09-01 13:42:30 得分 10
是呀,我也是想了好久。。关注中。。。Top
4 楼zhjboss(小张)回复于 2004-09-01 16:58:34 得分 0
还有高手吗?Top
5 楼jimh(Jimmy)回复于 2004-09-01 17:18:29 得分 0
关注一下。Top
6 楼simonllf(simon)回复于 2004-09-01 17:49:00 得分 0
关注Top
7 楼storm97(风暴不再)回复于 2004-09-01 17:51:54 得分 60
试一下:
添加一个delegate
delegate void AsyncCommSerial ();
把你的Read方法的参数去掉
public void Read()
{
......
}
把原来的AsyncCallback t1 = new AsyncCallback(Comm.Read);
改成:
AsyncCommSerial async = new AsyncCommSerial(Comm.Read);
async.BeginInvoke(null,null);Top
8 楼zhjboss(小张)回复于 2004-09-02 08:05:58 得分 0
还有更好的方法吗?Top
9 楼zhjboss(小张)回复于 2004-09-02 18:26:33 得分 0
不行啊Top
10 楼jamzh(Show me the money!!!)回复于 2004-09-02 19:12:25 得分 0
好象有个begininvoke,和endinvoke的方法,你到MSDN中去搜索下Top
11 楼zhpsam109(JACKY.昊昊)回复于 2004-09-02 19:20:26 得分 10
学习!Top
12 楼storm97(风暴不再)回复于 2004-09-03 12:41:03 得分 0
你说不行,程序有什么提示吗?Top




