WriteFile says open comm port has invalid handle?

G

Guest

Howdy,

I'm trying to write data out the com port. I have taken the code from the
sample on the MSDN Library CD and used the parts that seem relevant. I can
open the com port with CreateFile and get a valid file handle back. But
when I try to write to it, the WriteFile comes back and tells me the handle
is invalid. The program has control of the port because using Hyperterminal,
I can't connect when the program is connected.

There must be something simple I'm missing but I can't see it.

Any ideas???
 
S

Severian

Howdy,

I'm trying to write data out the com port. I have taken the code from the
sample on the MSDN Library CD and used the parts that seem relevant. I can
open the com port with CreateFile and get a valid file handle back. But
when I try to write to it, the WriteFile comes back and tells me the handle
is invalid. The program has control of the port because using Hyperterminal,
I can't connect when the program is connected.

There must be something simple I'm missing but I can't see it.

Whittle your routine down to the smallest compilable code that
reproduces the error and let us see it.
 
G

Guest

Howdy,

Here is the code. It is a connect function that calls a config function, a
send function where the problem shows up, and a close function.

The connect produces a valid handle but the send always says the handle is
invalid. It may be that there is more to it than that but that is the only
message I get. The com port is com6. I have the sample code from MSDN and
that is the source of the original code. It works but it is in old style
windows with message maps etc. and I don't want to use that for the app.

Here is the code:

bool CComm485::CommOpenConnection()
{

HANDLE hCommWatchThread ;
DWORD dwThreadID ;
COMMTIMEOUTS CommTimeOuts ;
char szPort[30];
int iRetVal;

wsprintf( szPort, "%s%d", "COM", m_iCommPort ) ;

// open COMM device

m_hCommHandle =
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 );
if(m_hCommHandle ==INVALID_HANDLE_VALUE)
return ( FALSE ) ;
else
{
// get any early notifications

SetCommMask( m_hCommHandle, EV_RXCHAR ) ;

// setup device buffers
DWORD dwError;
if(!SetupComm( m_hCommHandle, 4096, 4096 ) )
dwError = GetLastError();

// purge any information in the buffer

PurgeComm( m_hCommHandle, PURGE_TXABORT | PURGE_RXABORT |
PURGE_TXCLEAR | PURGE_RXCLEAR ) ;

// set up for overlapped I/O

CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF ;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0 ;
CommTimeOuts.ReadTotalTimeoutConstant = 1000 ;
// CBR_9600 is approximately 1byte/ms. For our purposes, allow
// double the expected time per character for a fudge factor.
CommTimeOuts.WriteTotalTimeoutMultiplier = 2*CBR_9600/9600 ;
CommTimeOuts.WriteTotalTimeoutConstant = 0 ;
SetCommTimeouts( m_hCommHandle, &CommTimeOuts ) ;
}

iRetVal = SetUpConnection() ;

if (!iRetVal)
{
m_bConnected = TRUE ;

// Create a secondary thread
// to watch for an event.

/* if ( NULL == (hCommWatchThread =
CreateThread( (LPSECURITY_ATTRIBUTES) NULL,
0,
(LPTHREAD_START_ROUTINE) CommWatchProc,
(LPVOID) this,
0, &dwThreadID )))
{
m_bConnected = FALSE ;
CloseHandle( m_hCommHandle ) ;
iRetVal = -1 ;
}
else
*/ {
m_dwThreadID = dwThreadID ;
m_hTermWnd = (HWND) hCommWatchThread ;

// assert DTR

EscapeCommFunction( m_hCommHandle, SETDTR ) ;



}
}
else
{
m_bConnected = FALSE ;
CloseHandle( m_hCommHandle ) ;
}

// restore cursor
// test it here
// char* szBuffer;
// szBuffer = (char*) malloc(100);
// memset(szBuffer, 'X', 100);
// WriteCommBlock( npTTYInfo, szBuffer, 100);
// free(szBuffer);



m_bConnected = true;
return m_bConnected ; //(bool) OpenConnection(pInfo);
}

bool CComm485::CommCloseConnection()
{
m_bConnected = false;

// disable event notification and wait for thread
// to halt

SetCommMask( m_hCommHandle, 0 ) ;

// block until thread has been halted

while(m_dwThreadID != 0);

// kill the focus

// KillTTYFocus( hWnd ) ;

// drop DTR

EscapeCommFunction( m_hCommHandle, CLRDTR ) ;

// purge any outstanding reads/writes and close device handle

PurgeComm( m_hCommHandle, PURGE_TXABORT | PURGE_RXABORT |
PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
CloseHandle( m_hCommHandle ) ;
//return (bool) CloseConnection((NPTTYINFO) m_pTTYInfo);
return true;
}

int CComm485::SendResponse(LPSTR szResponse, int iLength)
{
NPTTYINFO pInfo = (NPTTYINFO) m_pTTYInfo;

DWORD dwBytesWritten;

// WriteCommBlock( pInfo, szResponse , iLength);
int fWriteStat = WriteFile( m_hCommHandle, szResponse, iLength,
&dwBytesWritten, &m_osWrite ) ;

if (!fWriteStat)
{
DWORD dwLastError = GetLastError();
char szErrMsg[100];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,dwLastError,NULL,szErrMsg,
100,NULL);
dwLastError = 0;
}
return 0;
}

int CComm485::SetUpConnection()
{
BOOL fRetVal ;
DCB dcb ;
;

dcb.DCBlength = sizeof( DCB ) ;

if(!GetCommState( m_hCommHandle, &dcb ) )
{
DWORD dwLastError = GetLastError();
char szErrMsg[100];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,dwLastError,NULL,szErrMsg,
100,NULL);
dwLastError = 0;
}

dcb.BaudRate = CBR_9600 ;
dcb.ByteSize = 8 ;
dcb.Parity = NOPARITY ;
dcb.StopBits = ONESTOPBIT ;

// setup hardware flow control

dcb.fDtrControl = DTR_CONTROL_DISABLE ;
dcb.fRtsControl = RTS_CONTROL_DISABLE ;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDtrControl = FALSE;
dcb.fDsrSensitivity = FALSE;
dcb.fRtsControl = FALSE;

// setup software flow control


// bSet = 0; // no flow control
dcb.fInX = dcb.fOutX = FALSE ;
// dcb.XonChar = ASCII_XON ;
// dcb.XoffChar = ASCII_XOFF ;
// dcb.XonLim = 100 ;
// dcb.XoffLim = 100 ;

// other various settings
dcb.fAbortOnError = FALSE;

dcb.fBinary = TRUE ;
dcb.fParity = TRUE ;

fRetVal = SetCommState( m_hCommHandle, &dcb ) ;
DWORD dwError;
if(!fRetVal)
{
dwError = GetLastError();
char szErrMsg[100];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,dwError,NULL,szErrMsg,
100,NULL);
dwError = 0;
return -1;
}

return 0;

}

Any help would be appreciated.

Frank Perry
 
S

Severian

Howdy,

Here is the code. It is a connect function that calls a config function, a
send function where the problem shows up, and a close function.

The connect produces a valid handle but the send always says the handle is
invalid. It may be that there is more to it than that but that is the only
message I get. The com port is com6. I have the sample code from MSDN and
that is the source of the original code. It works but it is in old style
windows with message maps etc. and I don't want to use that for the app.

Here is the code:

<snip>

That was not enough to compile and test, so all I can do is guess.

1) Have you checked in the debugger that the m_hCommHandle is the same
when you call WriteFile? (Not corrupted or changed by something.)

2) Is m_osWrite properly intialized, including the event handle?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top