基于Active X的mfc串口通行小程序

一般串口可以通过Windows API和串口通信组件(ActiveX)两种方式来进行编写,

本文使用Active X控件,用VS2013编写了一个用于串口通信的MFC小程序。


详细程序见下载地址:http://download.csdn.net/detail/qwe15954250805/9901734


附上串口通信的相关书籍:http://download.csdn.net/detail/qwe15954250805/9902036

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
串口通信C程序 //Set CommTimeOuts BOOL SetCommTimeOuts ( VOID ) { //SetComm Input & Output Buffer SetupComm ( hSerial ,MAXLEN * 2 ,MAXLEN * 2 ); //Read TimeOut TimeOuts.ReadIntervalTimeout = MAXDWORD; //Set Total Read TimeOut as MAXDWORD (0xFFFFFFFF) TimeOuts.ReadTotalTimeoutConstant = 0; //Read TimeOut Const TimeOuts.ReadTotalTimeoutMultiplier = 0; //Return Riht now //Write TimeOut TimeOuts.WriteTotalTimeoutMultiplier = 50; //TotalTimeOut = TimeAgr*NumOfChars+const time TimeOuts.WriteTotalTimeoutConstant = 2000; //Set Write TimeOuts return ( FALSE != SetCommTimeouts ( hSerial,&TimeOuts ) ); } //Error MsgBox BOOL ErrMsg ( HWND hWnd,TCHAR *szErr ) { return ( FALSE != MessageBox ( hWnd,szErr,NULL,MB_OK | MB_ICONWARNING ) ); } //Read Comm BOOL ReadCom ( HWND hSet,BYTE InBuff[],INT BytesNum ) { DWORD dwBytesRead = 0; //Record The bytes already read INT iBytesToRead = 0; DWORD dwErrMask = 0; //Clear Memory ZeroMemory ( &ComState ,sizeof(ComState) ); ZeroMemory ( &OvLap ,sizeof(OvLap) ); OvLap.Offset = 0; OvLap.OffsetHigh = 0; OvLap.hEvent = CreateEvent (NULL,TRUE,FALSE,NULL); //Clear All sPort error ClearCommError (hSerial,&dwErrMask,&ComState); //Get The Bytes to read from buff iBytesToRead = min (2000,ComState.cbInQue); if ( 0 == iBytesToRead ) return FALSE; else; if ( FALSE == ReadFile (hSerial,InBuff,iBytesToRead,&dwBytesRead,&OvLap ) ) { if ( GetLastError () == ERROR_IO_PENDING ) { WaitForSingleObject (OvLap.hEvent,2000); PurgeComm (hSerial,PURGE_TXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ); } else; } else; return (TRUE); } ///Write Comm BOOL WriteCom ( BYTE *szBuff,DWORD dwBytes ) { DWORD dwErrMask = 0 ; DWORD dwBytesWritten = 0; ZeroMemory ( &OvLap ,sizeof(OvLap) ); ZeroMemory ( &ComState,sizeof(ComState) ); OvLap.Offset = 0; OvLap.OffsetHigh = 0; OvLap.hEvent = CreateEvent (NULL,TRUE,FALSE,NULL); //Clear All sPort error ClearCommError (hSerial,&dwErrMask,&ComState); if ( FALSE == WriteFile ( hSerial,szBuff,dwBytes,&dwBytesWritten ,&OvLap ) ) { if ( GetLastError () == ERROR_IO_PENDING ) { WaitForSingleObject (OvLap.hEvent,2000) ; PurgeComm (hSerial,PURGE_TXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ); } else; } else; return ( dwBytesWritten == dwBytes ); } TCHAR* Caps ( TCHAR *szStr ) { UINT i = 0; for ( i = 0 ; szStr[i] != '\0'; i ++ ) { if ( szStr[i] >= 'A' && szStr[i] <= 'Z' ) szStr[i] += 0x20; } return szStr; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值