?? c#2.0中,serialport運行方式.htm
字號:
a35950 = ContentsTree.AddNode(a31899, "Basic", "article_35950.html");a35947 = ContentsTree.AddNode(a31899, "C/C++", "article_35947.html");a36799 = ContentsTree.AddNode(a31899, "CSharp", "article_36799.html");a36799.bOpen=true;var pm=a36799;while(pm=pm.pParent){pm.bOpen=true;}a36870 = ContentsTree.AddNode(a31899, "D", "article_36870.html");a35948 = ContentsTree.AddNode(a31899, "Delphi/Pascal", "article_35948.html");a36965 = ContentsTree.AddNode(a31899, "FLTK", "article_36965.html");a36966 = ContentsTree.AddNode(a31899, "FoxToolKit", "article_36966.html");a36968 = ContentsTree.AddNode(a31899, "GTK+", "article_36968.html");a35949 = ContentsTree.AddNode(a31899, "Java", "article_35949.html");a36967 = ContentsTree.AddNode(a31899, "QT", "article_36967.html");a37042 = ContentsTree.AddNode(a31899, "SmartWin", "article_37042.html");a36964 = ContentsTree.AddNode(a31899, "wxWindow", "article_36964.html");a35954 = ContentsTree.AddNode(a0, "程序技巧", "article_35954.html");a36745 = ContentsTree.AddNode(a35954, "使用經(jīng)驗", "article_36745.html");a36744 = ContentsTree.AddNode(a35954, "最新軟件", "article_36744.html");a36746 = ContentsTree.AddNode(a35954, "走走看看", "article_36746.html");a36823 = ContentsTree.AddNode(a0, "多媒體", "article_36823.html");a36826 = ContentsTree.AddNode(a36823, "Authorware", "article_36826.html");a36825 = ContentsTree.AddNode(a36823, "Director", "article_36825.html");a36824 = ContentsTree.AddNode(a36823, "Flash", "article_36824.html");a36738 = ContentsTree.AddNode(a0, "腳本語言", "article_36738.html");a38906 = ContentsTree.AddNode(a36738, "AutoHotKey", "article_38906.html");a36748 = ContentsTree.AddNode(a36738, "AutoItX", "article_36748.html");a36747 = ContentsTree.AddNode(a36738, "AWK", "article_36747.html");a36801 = ContentsTree.AddNode(a36738, "JScript", "article_36801.html");a38705 = ContentsTree.AddNode(a36738, "Lua", "article_38705.html");a36739 = ContentsTree.AddNode(a36738, "Python", "article_36739.html");a36741 = ContentsTree.AddNode(a36738, "Ruby", "article_36741.html");a36740 = ContentsTree.AddNode(a36738, "TCL", "article_36740.html");a38903 = ContentsTree.AddNode(a36738, "VBScript", "article_38903.html"); p2847 = ContentsTree.AddNode(p0, "網(wǎng)站圖片", "photo_2847.html"); f7078 = ContentsTree.AddNode(f0, "情感交流", "links_7078.html");f9166 = ContentsTree.AddNode(f0, "存儲空間", "links_9166.html");f7077 = ContentsTree.AddNode(f0, "技術交流", "links_7077.html");f9233 = ContentsTree.AddNode(f0, "交流合作", "links_9233.html");f9120 = ContentsTree.AddNode(f0, "客戶鏈接", "links_9120.html"); //document.write(ContentsTree.GetHTMLCode());document.getElementById("lefttree").innerHTML=ContentsTree.GetHTMLCode();</SCRIPT>
</TD></TR>
<TR>
<TD height=5></TD></TR></TBODY></TABLE><BR><A
href="http://www.cublog.cn/u/rss.php?id=15586"><IMG alt=""
src="C%232.0中,SerialPort運行方式_files/xmlrss.gif" border=0></A> <BR><BR></TD>
<TD width=10></TD>
<TD vAlign=top width=685>
<TABLE style="BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0
width="100%" align=center border=0>
<TBODY>
<TR>
<TD align=middle bgColor=#bbf0ff><BR><FONT style="FONT-SIZE: 14pt"
color=#000066><B>C#2.0中,SerialPort運行方式[轉(zhuǎn)載]</B></FONT> </TD>
<TR>
<TD>
<TABLE style="BORDER-COLLAPSE: collapse" borderColor=#bbf0ff
cellSpacing=1 cellPadding=0 width="100%" border=1>
<TBODY>
<TR>
<TD align=middle>
<TABLE
style="BORDER-COLLAPSE: collapse; WORD-WRAP: break-word"
cellSpacing=0 cellPadding=0 width=650 border=0>
<TBODY>
<TR>
<TD align=middle>
<TABLE
style="BORDER-COLLAPSE: collapse; WORD-WRAP: break-word"
cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD width=650>
<DIV id=art
style="MARGIN: 15px">[點評:這幾天一直用這個控件,可是老是有問題,也許這篇文章是解決問題最終的辦法了.]<BR>SerialPort中串口數(shù)據(jù)的讀取與寫入有較大的不同。由于串口不知道數(shù)據(jù)何時到達,因此有兩種方法可以實現(xiàn)串口數(shù)據(jù)的讀取。
<P> 一、線程實時讀串口;</P>
<P> 二、事件觸發(fā)方式實現(xiàn)。 </P>
<P>
由于線程實時讀串口的效率不是十分高效,因此比較好的方法是事件觸發(fā)的方式。在SerialPort類中有DataReceived事件,當串口的讀緩存有數(shù)據(jù)到達時則觸發(fā)DataReceived事件,其中SerialPort.ReceivedBytesThreshold屬性決定了當串口讀緩存中數(shù)據(jù)多少個時才觸發(fā)DataReceived事件,默認為1。
</P>
<P>
另外,SerialPort.DataReceived事件運行比較特殊,其運行在輔線程,不能與主線程中的顯示數(shù)據(jù)控件直接進行數(shù)據(jù)傳輸,必須用間接的方式實現(xiàn)。如下:</P><PRE style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: black 1px solid; PADDING-LEFT: 4px; PADDING-BOTTOM: 4px; BORDER-LEFT: black 1px solid; PADDING-TOP: 4px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: rgb(237,237,237)"><DIV><!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><IMG alt="" src="" align=top><SPAN style="COLOR: rgb(0,0,0)">SerialPort spSend; </SPAN><SPAN style="COLOR: rgb(0,128,0)">//</SPAN><SPAN style="COLOR: rgb(0,128,0)">spSend,spReceive用虛擬串口連接,它們之間可以相互傳輸數(shù)據(jù)。spSend發(fā)送數(shù)據(jù) </SPAN><SPAN style="COLOR: rgb(0,128,0)">
<IMG alt="" src="" align=top></SPAN><SPAN style="COLOR: rgb(0,0,0)">SerialPort spReceive; </SPAN><SPAN style="COLOR: rgb(0,128,0)">//</SPAN><SPAN style="COLOR: rgb(0,128,0)">spReceive接受數(shù)據(jù) </SPAN><SPAN style="COLOR: rgb(0,128,0)">
<IMG alt="" src="" align=top></SPAN><SPAN style="COLOR: rgb(0,0,0)">TextBox txtSend; </SPAN><SPAN style="COLOR: rgb(0,128,0)">//</SPAN><SPAN style="COLOR: rgb(0,128,0)">發(fā)送區(qū) </SPAN><SPAN style="COLOR: rgb(0,128,0)">
<IMG alt="" src="" align=top></SPAN><SPAN style="COLOR: rgb(0,0,0)">TextBox txtReceive; </SPAN><SPAN style="COLOR: rgb(0,128,0)">//</SPAN><SPAN style="COLOR: rgb(0,128,0)">接受區(qū) </SPAN><SPAN style="COLOR: rgb(0,128,0)">
<IMG alt="" src="" align=top></SPAN><SPAN style="COLOR: rgb(0,0,0)">Button btnSend; </SPAN><SPAN style="COLOR: rgb(0,128,0)">//</SPAN><SPAN style="COLOR: rgb(0,128,0)">數(shù)據(jù)發(fā)送按鈕 </SPAN><SPAN style="COLOR: rgb(0,128,0)">
<IMG alt="" src="" align=top></SPAN><SPAN style="COLOR: rgb(0,0,255)">delegate</SPAN><SPAN style="COLOR: rgb(0,0,0)"> </SPAN><SPAN style="COLOR: rgb(0,0,255)">void</SPAN><SPAN style="COLOR: rgb(0,0,0)"> HandleInterfaceUpdateDelegate(</SPAN><SPAN style="COLOR: rgb(0,0,255)">string</SPAN><SPAN style="COLOR: rgb(0,0,0)"> text); </SPAN><SPAN style="COLOR: rgb(0,128,0)">//</SPAN><SPAN style="COLOR: rgb(0,128,0)">委托,此為重點 </SPAN><SPAN style="COLOR: rgb(0,128,0)">
<IMG alt="" src="" align=top></SPAN><SPAN style="COLOR: rgb(0,0,0)">HandleInterfaceUpdateDelegate interfaceUpdateHandle;<BR><IMG alt="" src="" align=top><BR><IMG alt="" src="" align=top></SPAN><SPAN style="COLOR: rgb(0,0,255)">public</SPAN><SPAN style="COLOR: rgb(0,0,0)"> </SPAN><SPAN style="COLOR: rgb(0,0,255)">void</SPAN><SPAN style="COLOR: rgb(0,0,0)"> InitClient() </SPAN><SPAN style="COLOR: rgb(0,128,0)">//</SPAN><SPAN style="COLOR: rgb(0,128,0)">窗體控件已在初始化 </SPAN><SPAN style="COLOR: rgb(0,128,0)">
<IMG id=Codehighlighter_348_629_Open_Image onclick="this.style.display='none'; Codehighlighter_348_629_Open_Text.style.display='none'; Codehighlighter_348_629_Closed_Image.style.display='inline'; Codehighlighter_348_629_Closed_Text.style.display='inline';" alt="" src="" align=top><IMG id=Codehighlighter_348_629_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter_348_629_Closed_Text.style.display='none'; Codehighlighter_348_629_Open_Image.style.display='inline'; Codehighlighter_348_629_Open_Text.style.display='inline';" alt="" src="" align=top></SPAN><SPAN id=Codehighlighter_348_629_Closed_Text style="BORDER-RIGHT: rgb(128,128,128) 1px solid; BORDER-TOP: rgb(128,128,128) 1px solid; DISPLAY: none; BORDER-LEFT: rgb(128,128,128) 1px solid; BORDER-BOTTOM: rgb(128,128,128) 1px solid; BACKGROUND-COLOR: rgb(255,255,255)">...</SPAN><SPAN id=Codehighlighter_348_629_Open_Text><SPAN style="COLOR: rgb(0,0,0)">{<BR><IMG alt="" src="" align=top>interfaceUpdateHandle </SPAN><SPAN style="COLOR: rgb(0,0,0)">=</SPAN><SPAN style="COLOR: rgb(0,0,0)"> </SPAN><SPAN style="COLOR: rgb(0,0,255)">new</SPAN><SPAN style="COLOR: rgb(0,0,0)"> HandleInterfaceUpdateDelegate(UpdateTextBox); </SPAN><SPAN style="COLOR: rgb(0,128,0)">//</SPAN><SPAN style="COLOR: rgb(0,128,0)">實例化委托對象 </SPAN><SPAN style="COLOR: rgb(0,128,0)">
<IMG alt="" src="" align=top></SPAN><SPAN style="COLOR: rgb(0,0,0)">spSend.Open(); </SPAN><SPAN style="COLOR: rgb(0,128,0)">//</SPAN><SPAN style="COLOR: rgb(0,128,0)">SerialPort對象在程序結(jié)束前必須關閉,在此說明 </SPAN><SPAN style="COLOR: rgb(0,128,0)">
<IMG alt="" src="" align=top></SPAN><SPAN style="COLOR: rgb(0,0,0)">spReceive.DataReceived </SPAN><SPAN style="COLOR: rgb(0,0,0)">+=</SPAN><SPAN style="COLOR: rgb(0,0,0)"> Ports.SerialDataReceivedEventHandler(spReceive_DataReceived);<BR><IMG alt="" src="" align=top>spReceive.ReceivedBytesThreshold </SPAN><SPAN style="COLOR: rgb(0,0,0)">=</SPAN><SPAN style="COLOR: rgb(0,0,0)"> </SPAN><SPAN style="COLOR: rgb(0,0,0)">1</SPAN><SPAN style="COLOR: rgb(0,0,0)">;<BR><IMG alt="" src="" align=top>spReceive.Open();<BR><IMG alt="" src="" align=top>}</SPAN></SPAN><SPAN style="COLOR: rgb(0,0,0)">
<IMG alt="" src="" align=top><BR><IMG alt="" src="" align=top></SPAN><SPAN style="COLOR: rgb(0,0,255)">public</SPAN><SPAN style="COLOR: rgb(0,0,0)"> </SPAN><SPAN style="COLOR: rgb(0,0,255)">void</SPAN><SPAN style="COLOR: rgb(0,0,0)"> btnSend_Click(</SPAN><SPAN style="COLOR: rgb(0,0,255)">object</SPAN><SPAN style="COLOR: rgb(0,0,0)"> sender,EventArgs e)<BR><IMG id=Codehighlighter_687_723_Open_Image onclick="this.style.display='none'; Codehighlighter_687_723_Open_Text.style.display='none'; Codehighlighter_687_723_Closed_Image.style.display='inline'; Codehighlighter_687_723_Closed_Text.style.display='inline';" alt="" src="" align=top><IMG id=Codehighlighter_687_723_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter_687_723_Closed_Text.style.display='none'; Codehighlighter_687_723_Open_Image.style.display='inline'; Codehighlighter_687_723_Open_Text.style.display='inline';" alt="" src="" align=top></SPAN><SPAN id=Codehighlighter_687_723_Closed_Text style="BORDER-RIGHT: rgb(128,128,128) 1px solid; BORDER-TOP: rgb(128,128,128) 1px solid; DISPLAY: none; BORDER-LEFT: rgb(128,128,128) 1px solid; BORDER-BOTTOM: rgb(128,128,128) 1px solid; BACKGROUND-COLOR: rgb(255,255,255)">...</SPAN><SPAN id=Codehighlighter_687_723_Open_Text><SPAN style="COLOR: rgb(0,0,0)">{<BR><IMG alt="" src="" align=top>spSend.WriteLine(txtSend.Text);<BR><IMG alt="" src="" align=top>}</SPAN></SPAN><SPAN style="COLOR: rgb(0,0,0)">
<IMG alt="" src="" align=top><BR><IMG alt="" src="" align=top></SPAN><SPAN style="COLOR: rgb(0,0,255)">public</SPAN><SPAN style="COLOR: rgb(0,0,0)"> </SPAN><SPAN style="COLOR: rgb(0,0,255)">void</SPAN><SPAN style="COLOR: rgb(0,0,0)"> spReceive_DataReceived(</SPAN><SPAN style="COLOR: rgb(0,0,255)">object</SPAN><SPAN style="COLOR: rgb(0,0,0)"> sender,Ports.SerialDataReceivedEventArgs e)<BR><IMG id=Codehighlighter_814_1019_Open_Image onclick="this.style.display='none'; Codehighlighter_814_1019_Open_Text.style.display='none'; Codehighlighter_814_1019_Closed_Image.style.display='inline'; Codehighlighter_814_1019_Closed_Text.style.display='inline';" alt="" src="" align=top><IMG id=Codehighlighter_814_1019_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter_814_1019_Closed_Text.style.display='none'; Codehighlighter_814_1019_Open_Image.style.display='inline'; Codehighlighter_814_1019_Open_Text.style.display='inline';" alt="" src="" align=top></SPAN><SPAN id=Codehighlighter_814_1019_Closed_Text style="BORDER-RIGHT: rgb(128,128,128) 1px solid; BORDER-TOP: rgb(128,128,128) 1px solid; DISPLAY: none; BORDER-LEFT: rgb(128,128,128) 1px solid; BORDER-BOTTOM: rgb(128,128,128) 1px solid; BACKGROUND-COLOR: rgb(255,255,255)">...</SPAN><SPAN id=Codehighlighter_814_1019_Open_Text><SPAN style="COLOR: rgb(0,0,0)">{<BR><IMG alt="" src="" align=top></SPAN><SPAN style="COLOR: rgb(0,0,255)">byte</SPAN><SPAN style="COLOR: rgb(0,0,0)">[] readBuffer </SPAN><SPAN style="COLOR: rgb(0,0,0)">=</SPAN><SPAN style="COLOR: rgb(0,0,0)"> </SPAN><SPAN style="COLOR: rgb(0,0,255)">new</SPAN><SPAN style="COLOR: rgb(0,0,0)"> </SPAN><SPAN style="COLOR: rgb(0,0,255)">byte</SPAN><SPAN style="COLOR: rgb(0,0,0)">[spReceive.ReadBufferSize];<BR><IMG alt="" src="" align=top>spReceive.Read(readBuffer, </SPAN><SPAN style="COLOR: rgb(0,0,0)">0</SPAN><SPAN style="COLOR: rgb(0,0,0)">, readBuffer.Length);<BR><IMG id=Codehighlighter_973_1014_Open_Image onclick="this.style.display='none'; Codehighlighter_973_1014_Open_Text.style.display='none'; Codehighlighter_973_1014_Closed_Image.style.display='inline'; Codehighlighter_973_1014_Closed_Text.style.display='inline';" alt="" src="" align=top><IMG id=Codehighlighter_973_1014_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter_973_1014_Closed_Text.style.display='none'; Codehighlighter_973_1014_Open_Image.style.display='inline'; Codehighlighter_973_1014_Open_Text.style.display='inline';" alt="" src="" align=top></SPAN><SPAN style="COLOR: rgb(0,0,255)">this</SPAN><SPAN style="COLOR: rgb(0,0,0)">.Invoke(interfaceUpdateHandle, </SPAN><SPAN style="COLOR: rgb(0,0,255)">new</SPAN><SPAN style="COLOR: rgb(0,0,0)"> </SPAN><SPAN style="COLOR: rgb(0,0,255)">string</SPAN><SPAN style="COLOR: rgb(0,0,0)">[] </SPAN><SPAN id=Codehighlighter_973_1014_Closed_Text style="BORDER-RIGHT: rgb(128,128,128) 1px solid; BORDER-TOP: rgb(128,128,128) 1px solid; DISPLAY: none; BORDER-LEFT: rgb(128,128,128) 1px solid; BORDER-BOTTOM: rgb(128,128,128) 1px solid; BACKGROUND-COLOR: rgb(255,255,255)">...</SPAN><SPAN id=Codehighlighter_973_1014_Open_Text><SPAN style="COLOR: rgb(0,0,0)">{ Encoding.Unicode.GetString(readBuffer) }</SPAN></SPAN><SPAN style="COLOR: rgb(0,0,0)">);<BR><IMG alt="" src="" align=top>}</SPAN></SPAN><SPAN style="COLOR: rgb(0,0,0)">
<IMG alt="" src="" align=top><BR><IMG alt="" src="" align=top></SPAN><SPAN style="COLOR: rgb(0,0,255)">private</SPAN><SPAN style="COLOR: rgb(0,0,0)"> </SPAN><SPAN style="COLOR: rgb(0,0,255)">void</SPAN><SPAN style="COLOR: rgb(0,0,0)"> UpdateTextBox(</SPAN><SPAN style="COLOR: rgb(0,0,255)">string</SPAN><SPAN style="COLOR: rgb(0,0,0)"> text)<BR><IMG id=Codehighlighter_1064_1092_Open_Image onclick="this.style.display='none'; Codehighlighter_1064_1092_Open_Text.style.display='none'; Codehighlighter_1064_1092_Closed_Image.style.display='inline'; Codehighlighter_1064_1092_Closed_Text.style.display='inline';" alt="" src="" align=top><IMG id=Codehighlighter_1064_1092_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter_1064_1092_Closed_Text.style.display='none'; Codehighlighter_1064_1092_Open_Image.style.display='inline'; Codehighlighter_1064_1092_Open_Text.style.display='inline';" alt="" src="" align=top></SPAN><SPAN id=Codehighlighter_1064_1092_Closed_Text style="BORDER-RIGHT: rgb(128,128,128) 1px solid; BORDER-TOP: rgb(128,128,128) 1px solid; DISPLAY: none; BORDER-LEFT: rgb(128,128,128) 1px solid; BORDER-BOTTOM: rgb(128,128,128) 1px solid; BACKGROUND-COLOR: rgb(255,255,255)">...</SPAN><SPAN id=Codehighlighter_1064_1092_Open_Text><SPAN style="COLOR: rgb(0,0,0)">{<BR><IMG alt="" src="" align=top>txtReceive.Text </SPAN><SPAN style="COLOR: rgb(0,0,0)">=</SPAN><SPAN style="COLOR: rgb(0,0,0)"> text;<BR><IMG alt="" src="" align=top>}</SPAN></SPAN><SPAN style="COLOR: rgb(0,0,0)"> </SPAN></DIV></PRE></DIV></TD></TR></TBODY></TABLE>
<P style="MARGIN: 5px; LINE-HEIGHT: 150%"></P></TD></TR>
<TR>
<TD height=25><FONT color=#000066> 發(fā)表于:
2006-11-01,修改于: 2006-11-01
09:12 已瀏覽379次,有評論0條</FONT> <A id=star title=推薦這篇文章
onclick="NewWindows(this.href);return false;"
href="http://www.cublog.cn/u2/star.php?blogid=15586&artid=193359">推薦</A>
<A id=complaint title=投訴這篇文章
onclick="NewWindows(this.href);return false;"
href="http://www.cublog.cn/u2/complaint.php?blogid=15586&artid=193359">投訴</A>
</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<TABLE style="BORDER-COLLAPSE: collapse" borderColor=#bbf0ff cellSpacing=0
cellPadding=0 width="100%" align=center border=1>
<TBODY>
<TR>
<TD background=C%232.0中,SerialPort運行方式_files/bg_line.gif
height=28> <B>網(wǎng)友評論</B></TD></TR>
<TR>
<TD align=middle>
<TABLE style="COLOR: #0066cc; BORDER-COLLAPSE: collapse"
cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<TABLE style="BORDER-COLLAPSE: collapse" borderColor=#bbf0ff cellSpacing=0
cellPadding=0 width="100%" align=center border=1>
<TBODY>
<TR>
<TD background=C%232.0中,SerialPort運行方式_files/bg_line.gif
height=28> <B>發(fā)表評論</B></TD></TR>
<TR>
<TD bgColor=#ffffff height=1></TD></TR>
<TR>
<TD align=middle bgColor=#ffffff><IFRAME name=comment
src="C%232.0中,SerialPort運行方式_files/comment.htm" frameBorder=0
width=600 height=160></IFRAME></TD></TR></TBODY></TABLE><BR><BR></TD>
<TD width=10></TD></TR></TBODY></TABLE>
<TABLE style="BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 width=950
align=center border=0>
<TBODY>
<TR>
<TD height=50></TD></TR>
<TR>
<TD align=middle><BR>
<P>Copyright © 2001-2006 ChinaUnix.net All Rights Reserved<BR>
<P>感謝所有關心和支持過ChinaUnix的朋友們 <BR>頁面生成時間:0.01412
<P><A href="http://www.miibeian.gov.cn/" target=_blank>京ICP證041476號</A>
</P></TD></TR></TBODY></TABLE></CENTER></BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -