为什么无法关闭串口啊??
代码如下:
全局变量:
BYTE cRBuff[512];
DWORD cBytes;
BYTE ch;
CString strRecv;
HANDLE hComPort;
bool ExitFlag;
DWORD dwThreadID;
HANDLE m_hCommThread;
函数开始:
void TestThread()
{
DWORD Errors;
COMSTAT ComStat;
hComPort=CreateFile(TEXT("COM2:"),GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
if (hComPort == NULL)
{//如果无法创建串口,取错误信息,显示提示
DWORD dwError = GetLastError();
AfxMessageBox(TEXT("Unable to open the serial port\n"));
return;
}
SetCommMask(hComPort, EV_RXCHAR | EV_BREAK);
//SetupComm(hComPort,READBUFLEN,WRITEBUFLEN);
COMMTIMEOUTS CommTimeouts; // Comm port timeouts structure
// Retrieve the timeouts parameters for all read and write operations on the port.
GetCommTimeouts (hComPort, &CommTimeouts);
// Change the COMMTIMEOUTS structure settings.
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
// Set the timeouts parameters for all read and write operations on the port.
if (!SetCommTimeouts (hComPort, &CommTimeouts))
{
// Could not create the read thread.
MessageBox (NULL, TEXT("Unable to set the time-out parameters"),
TEXT("Error"), MB_OK);
// dwError = GetLastError ();
return;
}
// DWORD dwError;
DCB PortDCB;
// Get the default port setting information.
GetCommState(hComPort, &PortDCB);
// Change the DCB structure settings.
//PortDCB.DCBlength = sizeof (DCB);
PortDCB.BaudRate = CBR_4800; // Current baud
PortDCB.ByteSize = 8; // Number of bits/byte, 4-8
PortDCB.Parity = ODDPARITY; // 0-4=no,odd,even,mark,space
PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
// Configure the port according to the specifications of the DCB
// structure.
if (! SetCommState(hComPort, &PortDCB))
{
MessageBox (NULL, TEXT("Unable to configure the CommState"),
TEXT("Error"), MB_OK);
// dwError = GetLastError ();
return;
}
// Direct the port to perform extended functions SETDTR and SETRTS
// SETDTR: Sends the DTR (data-terminal-ready) signal.
// SETRTS: Sends the RTS (request-to-send) signal.
// EscapeCommFunction (hComPort, SETDTR);
// EscapeCommFunction (hComPort, SETRTS);
//
DWORD dwCommMask = 0;
int ctrBuff=0;
while(!ExitFlag) //退出线程的标记,用于退出按钮
{
// strRecv=TEXT("");
WaitCommEvent(hComPort, &dwCommMask, NULL);
if (! ClearCommError(hComPort, &Errors, &ComStat))
continue;
if (dwCommMask & EV_BREAK) break;
if (dwCommMask & EV_RXCHAR)
{
ReadFile(hComPort, &ch, 1, &cBytes,NULL);
//memset(&cRBuff, 0, sizeof (cRBuff));
if (ctrBuff==512)
{
strRecv=(CString)cRBuff;
ctrBuff=0;
}
cRBuff[ctrBuff]=ch;
ctrBuff++;
}
}
SetCommMask(hComPort,NULL);
if (CloseHandle(hComPort)!=0) AfxMessageBox(TEXT("CloseHandle success.")); //关闭串口返回成功
return;
}
为什么每次第2次执行程序都是无法打开端口呢,而且很慢,资源耗得很严重??
另外:我的串口是一直有数据进来的...
问题点数:60、回复次数:11Top
1 楼legendhui(秋天的叶子)回复于 2005-03-07 22:17:23 得分 10
你的程序有内存泄露,你在退出进程前,应该关闭所有资源Top
2 楼samp_miao(九天揽月)回复于 2005-03-08 08:41:40 得分 8
if (hComPort == NULL)
{//如果无法创建串口,取错误信息,显示提示
DWORD dwError = GetLastError();
AfxMessageBox(TEXT("Unable to open the serial port\n"));
return;
}
打开串口失败时,返回INVALID_HANDLE_VALUE,不是NULL.
但是应该是其他原因吧,Top
3 楼yuanbocsut(井冈星火)回复于 2005-03-08 10:27:03 得分 2
upTop
4 楼tudou614(魔蟹座的SATAN)回复于 2005-03-08 10:27:11 得分 2
UPTop
5 楼vmaster(似水浮云)回复于 2005-03-08 10:47:50 得分 0
在线等...Top
6 楼crystal521(【云淡风轻】)回复于 2005-03-08 13:04:54 得分 5
你第二次打开线程时有没有关闭上次的线程?Top
7 楼crystal521(【云淡风轻】)回复于 2005-03-08 13:17:55 得分 15
改了一下
void TestThread()
{
DWORD Errors;
COMSTAT ComStat;
hComPort=CreateFile(TEXT("COM2:"),GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
if (hComPort == INVALID_HANDLE_VALUE)
{//如果无法创建串口,取错误信息,显示提示
DWORD dwError = GetLastError();
AfxMessageBox(TEXT("Unable to open the serial port\n"));
return;
}
SetCommMask(hComPort, EV_RXCHAR | EV_BREAK);
COMMTIMEOUTS CommTimeouts; // Comm port timeouts structure
GetCommTimeouts (hComPort, &CommTimeouts);
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
if (!SetCommTimeouts (hComPort, &CommTimeouts))
{
MessageBox (NULL, TEXT("Unable to set the time-out parameters"),
TEXT("Error"), MB_OK);
return;
}
DCB PortDCB;
GetCommState(hComPort, &PortDCB);
PortDCB.BaudRate = CBR_4800; // Current baud
PortDCB.ByteSize = 8; // Number of bits/byte, 4-8
PortDCB.Parity = ODDPARITY; // 0-4=no,odd,even,mark,space
PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
if (! SetCommState(hComPort, &PortDCB))
{
MessageBox (NULL, TEXT("Unable to configure the CommState"),
TEXT("Error"), MB_OK);
return;
}
DWORD dwCommMask = 0;
int ctrBuff=0;
while(!ExitFlag)
{
WaitCommEvent(hComPort, &dwCommMask, NULL);
if (! ClearCommError(hComPort, &Errors, &ComStat))
continue;
if (dwCommMask & EV_BREAK) break;
if (dwCommMask & EV_RXCHAR)
{
ReadFile(hComPort, &ch, ComStat.cbInQue, &cBytes,NULL);
if (ctrBuff==512)
{
strRecv=(CString)cRBuff;
ctrBuff=0;
}
cRBuff[ctrBuff]=ch;
ctrBuff++;
}
}
SetCommMask(hComPort,NULL);
if (CloseHandle(hComPort)!=0) AfxMessageBox(TEXT("CloseHandle success.")); //关闭串口返回成功
AfxEndThread(0);
return;
}Top
8 楼crystal521(【云淡风轻】)回复于 2005-03-08 13:19:45 得分 8
还有检查一下你的cRBuff和ch类型及转换,看有没有内存泄漏Top
9 楼vmaster(似水浮云)回复于 2005-03-08 13:39:28 得分 0
线程有自己返回,那算不算自动退出呢,还是要加上这一句呢?:
PostThreadMessage(GetCurrentThreadId(),WM_QUIT,0,0); //退出线程
Top
10 楼vmaster(似水浮云)回复于 2005-03-08 13:57:01 得分 0
还是不行,运行一次可以,但再运行7,8遍就不行了,突然变得好慢...
请问怎么检查内存泄漏的??Top
11 楼nuaawenlin(飘人)回复于 2005-03-09 10:20:41 得分 10
有内存泄露
在调试窗口中可以看到Top
相关问题
- 做串口通信时为什么无法得到返回信息
- 为什么mscomm控件的oncomm事件无法触发?我的串口无法接收到数据.
- 为什么mscomm控件的oncomm事件无法触发?我的串口无法接收到数据.
- 求助: 请教高手为什么使用spcomm控件无法接收usb专串口的数据?
- 连接Modem的串口无法打开,为什么?用GetLastError返回错误120:即This function is not supported on this system.
- 模拟串口为什么打不开?~~~~~~~~~~
- 初学者提问:为什么应用无法关闭
- 为什么我的Applet运行后,无法关闭???
- connection.close()为什么无法关闭执行中的存储过程
- connection.close()为什么无法关闭执行中的存储过程




