📄 在vc++6_0中用win32 api实现串行通信-软件-vc-天极yesky.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0040)http://soft.yesky.com/50/2214050_1.shtml -->
<HTML lang=zh-CN xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>在VC++6.0中用Win32 API实现串行通信-软件-VC-天极Yesky</TITLE>
<META content="在VC++6.0中用Win32 API实现串行通信," name=description>
<META content="在VC++6.0中用Win32 API实现串行通信," name=keywords>
<META content="天极Yesky | 全球中文IT第一门户" name=author>
<META content="天极Yesky | 全球中文IT第一门户" name=Copyright>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=Content-Language content=zh-CN><LINK media=all
href="在VC++6_0中用Win32 API实现串行通信-软件-VC-天极Yesky.files/y_mobile_content.css"
type=text/css rel=stylesheet>
<SCRIPT src="在VC++6_0中用Win32 API实现串行通信-软件-VC-天极Yesky.files/showlay.js"></SCRIPT>
<META content="MSHTML 6.00.2900.3199" name=GENERATOR></HEAD>
<BODY><!--页面头部--><!--头部-->
<DIV>
<SCRIPT
src="在VC++6_0中用Win32 API实现串行通信-软件-VC-天极Yesky.files/kingsoft-kuanban-071105.js"></SCRIPT>
</DIV>
<DIV id=topmenu><A href="http://www.yesky.com/">Yesky首页</A>| <A
href="http://product.yesky.com/">产品报价</A>| <A
href="http://cseek.yesky.com/">行情</A>| <A href="http://mobile.yesky.com/">手机</A>
| <A href="http://digital.yesky.com/">数码</A> | <A
href="http://notebook.yesky.com/">笔记本</A> | <A
href="http://pc.yesky.com/">台式机</A> | <A href="http://diy.yesky.com/">DIY硬件</A>
| <A href="http://oa.yesky.com/">外设</A> | <A href="http://net.yesky.com/">网络</A>
| <A href="http://dh.yesky.com/">数字家庭</A> | <A
href="http://lab.yesky.com/">评测</A> | <A href="http://soft.yesky.com/">软件</A> |
<A href="http://e.yesky.com/">e时代</A> | <A href="http://game.yesky.com/">游戏</A>
| <A href="http://pic.yesky.com/">图片</A> | <A
href="http://desktop.yesky.com/">壁纸</A> | <A href="http://q.yesky.com/">群乐</A> |
<A href="http://my.yesky.com/">社区</A> | <A href="http://blog.yesky.com/">博客</A>
| <A href="http://www.mydown.com/">下载</A></DIV>
<DIV id=topbox>
<DIV class=tleft><A href="http://www.yesky.com/"><IMG alt=天极Yesky_全球中文IT第一门户
src="在VC++6_0中用Win32 API实现串行通信-软件-VC-天极Yesky.files/logo.gif" border=0></A></DIV>
<DIV class=tad><SPAN id=ad1></SPAN></DIV>
<DIV id=search>
<DIV>
<FORM name=searchform action=http://search.chinabyte.com/search method=get
target=_blank><INPUT id=searchzz onmouseover=this.focus() onfocus=this.select()
name=q><INPUT type=hidden value=GB2312 name=encoding><INPUT type=hidden
value=connect name=dir><INPUT type=hidden value=20002 name=cid><INPUT id=search_btn type=submit value=天极搜索> </FORM></DIV>
<DIV class=tdq>
<DIV class=rightarea><A href="http://bj.yesky.com/">北京</A> <A
href="http://hd.yesky.com/">上海</A> <A href="http://gd.yesky.com/">广州</A> <A
href="http://sz.yesky.com/">深圳</A><BR><A href="http://cd.yesky.com/">成都</A> <A
href="http://cq.yesky.com/">重庆</A> <A href="http://sy.yesky.com/">沈阳</A> <A
href="http://xa.yesky.com/">西安</A> </DIV>
<DIV class=rightarea_right><A
href="http://my.yesky.com/">用户<BR>登录</A></DIV></DIV></DIV>
<DIV class=tright>
<DIV class=box1>
<SCRIPT src="在VC++6_0中用Win32 API实现串行通信-软件-VC-天极Yesky.files/content_text_ad.js"
type=text/javascript></SCRIPT>
</DIV></DIV></DIV>
<SCRIPT src="在VC++6_0中用Win32 API实现串行通信-软件-VC-天极Yesky.files/flash.js"
type=text/javascript></SCRIPT>
<DIV id=childmenu><A href="http://soft.yesky.com/">软件频道></A><A
href="http://design.yesky.com/">设计</A><A href="http://tools.yesky.com/">工具</A><A
href="http://os.yesky.com/">系统</A><A href="http://dev.yesky.com/">开发</A><A
href="http://soft.yesky.com/security/">安全</A><A
href="http://soft.yesky.com/office/">办公</A><A
href="http://homepage.yesky.com/">陶吧</A><A
href="http://edu.yesky.com/">IT教育</A><A
href="http://vista.yesky.com/">Vista频道</A>|<A
href="http://soft.yesky.com/lesson/">软件频道产品中心</A><A
href="http://q.yesky.com/group/search.do?cid=661">群乐</A><A
href="http://www.mydown.com/soft/">软件下载</A><A
href="http://q.yesky.com/group/202019"><FONT color=#ff0000>Vista群乐</FONT></A><A
href="http://www.ibm.com/developerworks/cn/edu/j-dw-java-junit4.html?S_TACT=105AGX52&S_CMP=top-yesky">深入探索JUnit
4</A></DIV><!--导航条-->
<DIV id=location>
<DIV class=loleft>您现在的位置: <A href="http://www.yesky.com/" target=_self>天极网</A>
> <A href="http://soft.yesky.com/" target=_self>软件频道</A> > 在VC++6.0中用Win32
API实现串行通信</DIV>
<DIV class=adright><SPAN id=ad2></SPAN></DIV></DIV><!--内容块开始-->
<DIV id=conbox><!--左边开始-->
<DIV id=conleft>
<DIV id=contopla1>
<DIV id=__001>
<DIV class=qkong></DIV>
<DIV class=qw1>全文</DIV>
<DIV class=zkong>
<DIV class=kl></DIV>
<DIV class=bj><A href="http://soft.yesky.com/50/2214050_1.shtml#pls"
target=_self>评论</A></DIV>
<DIV class=kr></DIV><!-- Error -->
<SCRIPT type=text/javascript>
var titiepic ="#";
if (titiepic!="#")
{
document.write("<div class=\"kl\"></div><div class=\"bj\"><a href=\"#\" target=\"_blank\">图片</a></div><div class=\"kr\"></div>");
}
</SCRIPT>
<!-- Error --></DIV></DIV></DIV>
<DIV class=topadbg><SPAN id=ad10></SPAN></DIV><A name=top></A>
<DIV id=contitle>
<H1>在VC++6.0中用Win32 API实现串行通信</H1></DIV>
<DIV id=conauthor><SPAN>2005-11-29 13:08 </SPAN><SPAN>作者: 刘涛 </SPAN><SPAN>出处:
天极开发 </SPAN><SPAN>责任编辑:<A title=向本编辑提问
href="http://comments.yesky.com/t/·½ÖÛ/6,324/2214050.shtml" target=_blank>方舟</A>
</SPAN></DIV>
<DIV class=topadbg><SPAN id=ad9></SPAN></DIV>
<DIV id=conneirong><SPAN id=ad3></SPAN>
<DIV class=guanggao><SPAN id=contentAdv></SPAN></DIV> <B>三、程序代码</B><BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>//////////////////////////////////////////////////////////////<BR><BR>#if
!defined(_COMM_ACCESS_FUNCTIONS_AND_DATA)<BR>#define
_COMM_ACCESS_FUNCTIONS_AND_DATA<BR>#if _MSC_VER > 1000<BR>#pragma
once<BR>#endif // _MSC_VER > 1000<BR>#define EVENTCHAR 0x0d<BR>#define
MAXBLOCKLENGTH 59<BR><BR>extern BYTE XwCom;<BR>extern BYTE
sCom1[5],sCom2[MAXBLOCKLENGTH+12];<BR>extern
sCom3[MAXBLOCKLENGTH+12];<BR>extern BYTE opation;<BR>extern short
ComNum;<BR><BR>#define FC_DTRDSR 0x01<BR>#define FC_RTSCTS 0x02<BR>#define
FC_XONXOFF 0x04<BR>#define ASCII_BEL 0x07<BR>#define ASCII_BS
0x08<BR>#define ASCII_LF 0x0A<BR>#define ASCII_CR 0x0D<BR>#define
ASCII_XON 0x11<BR>#define ASCII_XOFF 0x13<BR><BR>class
CComStatus<BR>{<BR> public:<BR> HANDLE m_hCom;<BR> BYTE
m_bComId;<BR> BYTE m_bByteSize;<BR> BYTE m_bStopBits;<BR> BYTE
m_bParity;<BR> DWORD m_dwBaudRate;<BR><BR> //WORD
m_fChEvt;<BR><BR> char m_bEvtChar;<BR> DWORD m_fBinary;<BR> BOOL
m_bConnected;<BR> BOOL m_fXonXoff;<BR> BOOL m_bFlowCtrl;<BR> OVERLAPPED
m_rdos;<BR> OVERLAPPED
m_wtos;<BR><BR> //functions<BR><BR> CComStatus();<BR> CComStatus(BYTE
bComId,BYTE bByteSize,BYTE bStopBits,BYTE bParity,<BR> DWORD
dwBaudRate,/*WORD fChEvt,*/char bEvtChar,DWORD fBinary);<BR> BOOL
OpenConnection();<BR> BOOL CloseConnection();<BR> BOOL
SetupConnection();<BR> BOOL IsConnected();<BR>};<BR><BR>UINT
CommWatchProc( LPVOID lpData );<BR>BOOL WriteCommBlock( CComStatus&
comDev, LPSTR lpByte , DWORD dwBytesToWrite);<BR>int
ReadCommBlock(CComStatus& comDev,LPSTR lpszBlock, int nMaxLength
);<BR>int ReadCommBlockEx(CComStatus& comDev,LPSTR lpszBlock, int
nMaxLength,DWORD
dwTimeOut);<BR>#endif<BR><BR>///////////////////////////////////////////////////////////////////////<BR><BR>#include
"stdafx.h"<BR>#include "com232.h"<BR><BR>BYTE XwCom=0x40;<BR>BYTE
sCom1[5],sCom2[MAXBLOCKLENGTH+12],sCom3[MAXBLOCKLENGTH+12];<BR>BYTE
opation;<BR>short ComNum;<BR>CComStatus::CComStatus()<BR>{<BR> m_hCom =
NULL;<BR> m_bComId =
(char)ComNum;//COM1<BR> m_bByteSize=8;<BR> m_bStopBits=ONESTOPBIT;<BR> m_bParity=NOPARITY;<BR> m_dwBaudRate=9600;<BR> m_bEvtChar=EVENTCHAR;<BR> m_fBinary=1;<BR> m_bConnected
= FALSE;<BR> m_bFlowCtrl = FC_XONXOFF ;<BR> m_fXonXoff =
FALSE;<BR>}<BR><BR>CComStatus::CComStatus(BYTE bComId,BYTE bByteSize,BYTE
bStopBits,BYTE bParity,DWORD dwBaudRate,/*WORD fChEvt,*/char
bEvtChar,DWORD fBinary)<BR>{<BR> m_hCom = NULL;<BR> m_bComId =
bComId;<BR> m_bByteSize=bByteSize;<BR> m_bStopBits=bStopBits;<BR> m_bParity=bParity;<BR> m_dwBaudRate=dwBaudRate;<BR> m_bEvtChar=bEvtChar;<BR> m_fBinary=fBinary;<BR> m_bConnected
= FALSE;<BR> m_bFlowCtrl = FC_XONXOFF ;<BR> m_fXonXoff =
FALSE;<BR>}<BR><BR>BOOL CComStatus::OpenConnection()<BR>{<BR> char
csCom[10];<BR> COMMTIMEOUTS CommTimeOuts ;<BR> if((m_bComId < 0) ||
(m_bComId > 4))<BR> return FALSE;//从COM1到COM4<BR> if(m_hCom)//if
already open<BR> return
FALSE;<BR><BR> //OVERLAPPED包含异步I/O信息<BR><BR> m_rdos.Offset =
0;<BR> m_rdos.OffsetHigh = 0;<BR> m_rdos.hEvent =
CreateEvent(NULL,TRUE,FALSE,NULL);<BR> if(m_rdos.hEvent ==
NULL)<BR> return FALSE;<BR> m_wtos.Offset = 0;<BR> m_wtos.OffsetHigh =
0;<BR> m_wtos.hEvent =
CreateEvent(NULL,TRUE,FALSE,NULL);<BR> if(m_wtos.hEvent ==
NULL)<BR> {<BR> CloseHandle(m_rdos.hEvent);<BR> return
FALSE;<BR> }<BR><BR> wsprintf(csCom,"COM%d",m_bComId);<BR><BR> m_hCom =
CreateFile(csCom,GENERIC_READ | GENERIC_WRITE, 0,NULL,
OPEN_EXISTING,ILE_ATTRIBUTE_NORMAL |
FILE_FLAG_OVERLAPPED,NULL);<BR><BR> if(m_hCom == INVALID_HANDLE_VALUE)
{<BR> //dwError = GetLastError();<BR> // handle error <BR> return
FALSE;<BR> }<BR> else<BR> {<BR> SetCommMask( m_hCom, EV_RXCHAR ) ; // get
any early notifications<BR> SetupComm( m_hCom, 4096, 4096 ) ; // setup
device buffers<BR> // purge any information in the
buffer<BR><BR> PurgeComm( m_hCom, PURGE_TXABORT | PURGE_RXABORT
|PURGE_TXCLEAR | PURGE_RXCLEAR ) ;<BR><BR> // set up for overlapped
I/O<BR><BR> DWORD dwTemp = 1000 / (this->m_dwBaudRate /
8);<BR> CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF
;<BR> CommTimeOuts.ReadTotalTimeoutMultiplier = 0;//((dwTemp > 0) ?
dwTemp : 1);<BR> CommTimeOuts.ReadTotalTimeoutConstant = 1000
;<BR><BR> // CBR_9600 is approximately 1byte/ms. For our purposes,
allow<BR> // double the expected time per character for a fudge
factor.<BR><BR> CommTimeOuts.WriteTotalTimeoutMultiplier
=2*CBR_9600/this->m_dwBaudRate;//( npTTYInfo )
;<BR> CommTimeOuts.WriteTotalTimeoutConstant = 0;//1000
;<BR><BR> SetCommTimeouts( m_hCom, &CommTimeOuts )
;<BR> }<BR> if(!SetupConnection())<BR> {<BR> CloseConnection();<BR> return
FALSE;<BR> }<BR> EscapeCommFunction( m_hCom, SETDTR );<BR> m_bConnected =
TRUE;<BR> return TRUE;<BR>}<BR><BR>BOOL
CComStatus::CloseConnection()<BR>{<BR> if (NULL == m_hCom)<BR> return (
TRUE ) ;<BR> // set connected flag to FALSE<BR> m_bConnected =
FALSE;<BR> // disable event notification and wait for thread<BR> // to
halt<BR> SetCommMask( m_hCom, 0 ) ;<BR> EscapeCommFunction( m_hCom, CLRDTR
) ;<BR> // purge any outstanding reads/writes and close device
handle<BR> PurgeComm( m_hCom, PURGE_TXABORT | PURGE_RXABORT |
PURGE_TXCLEAR | PURGE_RXCLEAR ) ;<BR> CloseHandle( m_hCom ) ;<BR> m_hCom =
NULL;<BR><BR> // change the selectable items in the
menu<BR><BR> CloseHandle(m_rdos.hEvent);<BR> CloseHandle(m_wtos.hEvent);<BR> return
( TRUE ) ;<BR>}<BR><BR>BOOL CComStatus::SetupConnection()<BR>{<BR> BOOL
fRetVal ;<BR> BYTE bSet ;<BR> DCB dcb ;<BR> if(m_hCom == NULL)<BR> return
FALSE; <BR> dcb.DCBlength = sizeof( DCB ) ;<BR> GetCommState( m_hCom,
&dcb ) ;<BR> dcb.BaudRate = this->m_dwBaudRate;<BR> dcb.ByteSize =
this->m_bByteSize;<BR> dcb.Parity =
this->m_bParity;<BR> dcb.StopBits = this->m_bStopBits
;<BR> dcb.EvtChar = this->m_bEvtChar ;<BR> // setup hardware flow
control<BR> bSet = (BYTE) ((m_bFlowCtrl & FC_DTRDSR) != 0)
;<BR> dcb.fOutxDsrFlow = bSet ;<BR> if (bSet)<BR> dcb.fDtrControl =
DTR_CONTROL_HANDSHAKE ;<BR> else<BR> dcb.fDtrControl = DTR_CONTROL_ENABLE
;<BR> bSet = (BYTE) ((m_bFlowCtrl & FC_RTSCTS) != 0)
;<BR> dcb.fOutxCtsFlow = bSet ;<BR> if (bSet)<BR> dcb.fRtsControl =
RTS_CONTROL_HANDSHAKE ;<BR> else<BR> dcb.fRtsControl = RTS_CONTROL_ENABLE
;<BR> // setup software flow control<BR> bSet = (BYTE) ((m_bFlowCtrl &
FC_XONXOFF) != 0) ;<BR> dcb.fInX = dcb.fOutX = bSet ;<BR> dcb.XonChar =
ASCII_XON ;<BR> char xon = ASCII_XON ;<BR> dcb.XoffChar = ASCII_XOFF
;<BR> char xoff = ASCII_XOFF ;<BR> dcb.XonLim = 100 ;<BR> dcb.XoffLim =
100 ;<BR> // other various settings<BR> dcb.fBinary = TRUE
;<BR> dcb.fParity = TRUE ;<BR> fRetVal = SetCommState( m_hCom, &dcb )
;<BR> return ( fRetVal ) ;<BR>} // end of SetupConnection()<BR><BR>BOOL
CComStatus::IsConnected()<BR>{<BR> return m_bConnected;<BR>}<BR><BR>UINT
CommWatchProc( LPVOID lpData )<BR>{<BR> DWORD dwEvtMask ;<BR> //NPTTYINFO
npTTYInfo = (NPTTYINFO) lpData ;<BR> OVERLAPPED os ;<BR> int nLength
;<BR> //BYTE abIn[ MAXBLOCK + 1] ;<BR><BR> CComStatus * pCom = (CComStatus
*)lpData;<BR> memset( &os, 0, sizeof( OVERLAPPED ) ) ;<BR> // create
I/O event used for overlapped read<BR><BR> os.hEvent = CreateEvent( NULL,
// no security<BR> TRUE, // explicit reset req<BR> FALSE, // initial
event reset<BR> NULL ) ; // no name<BR><BR> if (os.hEvent ==
NULL)<BR> {<BR> MessageBox( NULL, "Failed to create event for thread!",
"TTY Error!",MB_ICONEXCLAMATION | MB_OK ) ;<BR> return ( FALSE )
;<BR> }<BR> if (!SetCommMask( pCom->m_hCom, EV_RXCHAR ))<BR> return (
FALSE ) ;<BR> char buf[256];<BR> while ( pCom->m_bConnected
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -