请串口高手帮忙!

jxfzamdzyx 2004-04-11 08:31:31
请高手帮忙!当我按按钮时给个线程监视串口有无数据输入而后进行相应处理,再按一次按钮则取消监视,如何实现!
...全文
25 点赞 收藏 4
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangnanonnet 2004-04-12
然后循环等待字符事件就可以了
BOOL ZComm::WaitEventChar()
{
OVERLAPPED os ;
DWORD dwEvtMask,Errors;
COMSTAT Stat;

memset( &os, 0, sizeof( OVERLAPPED ));
os.hEvent = CreateEvent( NULL, // no security
TRUE, // explicit reset req
FALSE, // initial event nonsignaled.
NULL ) ; // no name
dwEvtMask = 0 ;
WaitCommEvent(idComDev,&dwEvtMask,&os);

if (WaitForSingleObject(os.hEvent,MAXTIMEOUT)==WAIT_TIMEOUT)
{
CloseHandle(os.hEvent);
return false;
}
else if ((dwEvtMask & EV_RXCHAR/*EV_RXFLAG*/) == /*EV_RXFLAG*/EV_RXCHAR)
{
CloseHandle(os.hEvent);

ClearCommError(idComDev,&Errors, &Stat);
return true;
}else
{
CloseHandle(os.hEvent);
return false;
}
}
回复
zhangnanonnet 2004-04-12
在线成中异步打开串口
ZComm::open(char *szPort,int Baud_rate, BOOL overlapped)
{
int retbuflength=0;

if (overlapped) { //异步方式打开串口
memset( &READ_OS, 0, sizeof( OVERLAPPED ) ) ;
memset( &WRITE_OS, 0, sizeof( OVERLAPPED ) ) ;

READ_OS.hEvent = CreateEvent( NULL, // no security
TRUE, // explicit reset req
FALSE, // initial event reset
NULL ) ; // no name
if (READ_OS.hEvent == NULL) return -1 ;

WRITE_OS.hEvent = CreateEvent( NULL, // no security
TRUE, // explicit reset req
FALSE, // initial event reset
NULL ) ; // no name
if (NULL == WRITE_OS.hEvent)
{
CloseHandle( READ_OS.hEvent ) ;
return -1 ;
}

idComDev=CreateFile( szPort, GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security attrs
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|
FILE_FLAG_OVERLAPPED, // overlapped I/O
NULL );

olap=TRUE;
} //同步方式打开串口
else idComDev=CreateFile( szPort, GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security attrs
OPEN_EXISTING,
0, //is overlapped or no
NULL );

if (idComDev==(HANDLE) -1){

DWORD RR=GetLastError();
CloseHandle(idComDev) ;
if (olap){
CloseHandle( READ_OS.hEvent ) ;
CloseHandle( WRITE_OS.hEvent ) ;
}
fCOMMOpened=0;
olap=FALSE;
return (0);
}

set_baudrate(Baud_rate);
fCOMMOpened=1;
SetupComm(idComDev,11520,2048);
PurgeComm(idComDev, PURGE_TXABORT | PURGE_RXABORT |
PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
SetCommMask(idComDev,EV_RXCHAR/* EV_RXFLAG*/ );
//set time out
SetTimeOut(10,100,500);
return (1);
}

BOOL ZComm::SetTimeOut(DWORD ReadIntervalTimeout,
DWORD ReadTotalTimeoutMultiplier,
DWORD ReadTotalTimeoutConstant)
{
COMMTIMEOUTS timeouts;

GetCommTimeouts(idComDev,&timeouts);
timeouts.ReadIntervalTimeout=ReadIntervalTimeout;
timeouts.ReadTotalTimeoutMultiplier=ReadTotalTimeoutMultiplier;
timeouts.ReadTotalTimeoutConstant=ReadTotalTimeoutConstant;
return SetCommTimeouts(idComDev,&timeouts);
}

ZComm::Close()
{
if (fCOMMOpened==1) {
fCOMMOpened=0;
CloseHandle(idComDev) ;
olap=FALSE;
if (olap){
CloseHandle( READ_OS.hEvent ) ;
CloseHandle( WRITE_OS.hEvent ) ;
}
return 1;
}
return 0;
}

int ZComm::set_baudrate(int baud_rate)
{
DCB dcb;
BOOL fsuccess;

fsuccess = GetCommState(idComDev, &dcb);
if (!fsuccess) return 0;

dcb.BaudRate =baud_rate;
dcb.ByteSize =8; //number of bits/byte, 4-8
dcb.Parity =NOPARITY;
dcb.StopBits =ONESTOPBIT;
//dcb.EvtChar=0x02;
//dcb.fNull=TRUE;

fsuccess=SetCommState(idComDev, &dcb);
if (!fsuccess) return 0;

return 1;
}
回复
jxfzamdzyx 2004-04-12
楼上兄弟可否留下QQ!想交流一下!QQ:329270348
回复
jxfzamdzyx 2004-04-11
大家是不是嫌分少呀!不够可再加!
回复
发动态
发帖子
硬件/系统
创建于2007-09-28

2590

社区成员

VC/MFC 硬件/系统
申请成为版主
社区公告
暂无公告