📄 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, "使用经验", "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, "网站图片", "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运行方式[转载]</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中串口数据的读取与写入有较大的不同。由于串口不知道数据何时到达,因此有两种方法可以实现串口数据的读取。
<P> 一、线程实时读串口;</P>
<P> 二、事件触发方式实现。 </P>
<P>
由于线程实时读串口的效率不是十分高效,因此比较好的方法是事件触发的方式。在SerialPort类中有DataReceived事件,当串口的读缓存有数据到达时则触发DataReceived事件,其中SerialPort.ReceivedBytesThreshold属性决定了当串口读缓存中数据多少个时才触发DataReceived事件,默认为1。
</P>
<P>
另外,SerialPort.DataReceived事件运行比较特殊,其运行在辅线程,不能与主线程中的显示数据控件直接进行数据传输,必须用间接的方式实现。如下:</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用虚拟串口连接,它们之间可以相互传输数据。spSend发送数据 </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接受数据 </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)">发送区 </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)">接受区 </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)">数据发送按钮 </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对象在程序结束前必须关闭,在此说明 </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> 发表于:
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>网友评论</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>发表评论</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 + -