CreateFile打开串口很慢,该怎么处理
CreateFile打开串口很慢
我用这个方法打开串口好慢,每次都好像卡在那一样,要3-5秒才能开好,程序如下,有办法加快么,谢谢各位了
------解决方案--------------------
串口的打开速度不是上层应用程序能够控制得了的,那是内核驱动的事了,你就省省心了
------解决方案--------------------
单步调试下试试 看看是具体哪里的问题!
------解决方案--------------------
CreateFile 已经是直接打开串口设备驱动了。
不关语言的事了,这个打开速度1秒不到
我用这个方法打开串口好慢,每次都好像卡在那一样,要3-5秒才能开好,程序如下,有办法加快么,谢谢各位了
- C/C++ code
BOOL OpenPort(int BaudRate,int DataBits,int StopBits,int Parity,HAND *temphand) { COMMTIMEOUTS CommTimeOuts; //打开串口 (*temphand).hComm=CreateFile((*temphand).Port,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,0,0); if((*temphand).hComm==INVALID_HANDLE_VALUE) { //AfxMessageBox(_T("无法打开端口或端口已经打开!请检查USB是否连接! ")); return FALSE; } GetCommState((*temphand).hComm,&dcb); dcb.BaudRate=BaudRate; dcb.ByteSize=DataBits; dcb.Parity=Parity; dcb.StopBits=StopBits; dcb.fParity=FALSE; dcb.fBinary=TRUE; dcb.fDtrControl=0; dcb.fRtsControl=0; dcb.fOutX=0; dcb.fInX=0; dcb.fTXContinueOnXoff=0; dcb.fAbortOnError = true; //设置状态参数 SetCommMask((*temphand).hComm,EV_RXCHAR); //串口事件:接收到一个字符 SetupComm((*temphand).hComm,1024,1024); //设置接收与发送的缓冲区大小 if(!SetCommState((*temphand).hComm,&dcb)) //设置串口的DCB { AfxMessageBox(_T("无法按当前参数配置端口,请检查参数!")); ClosePort(temphand); return FALSE; } //设置超时参数 GetCommTimeouts((*temphand).hComm,&CommTimeOuts); CommTimeOuts.ReadIntervalTimeout=1; //接收字符间最大时间间隔;原值100 CommTimeOuts.ReadTotalTimeoutMultiplier=1; CommTimeOuts.ReadTotalTimeoutConstant=1; //读数据总超时常量;原值100 CommTimeOuts.WriteTotalTimeoutMultiplier=0; CommTimeOuts.WriteTotalTimeoutConstant=0; if(!SetCommTimeouts((*temphand).hComm,&CommTimeOuts)) { AfxMessageBox(_T("无法设置超时参数!")); ClosePort(temphand); return FALSE; } //AfxMessageBox(_T("打开成功")); // PurgeComm((*temphand).hComm,PURGE_TXCLEAR|PURGE_RXCLEAR); //清除收/发缓冲区 return TRUE; }
------解决方案--------------------
串口的打开速度不是上层应用程序能够控制得了的,那是内核驱动的事了,你就省省心了
------解决方案--------------------
单步调试下试试 看看是具体哪里的问题!
------解决方案--------------------
CreateFile 已经是直接打开串口设备驱动了。
不关语言的事了,这个打开速度1秒不到