verifier
06-18-2003, 09:50 AM
Hello
My overlapped ReadFile doesnt complete on one machine, it do on another.
I've supplied the connect function, and the ThreadFunc that readfile
takes place in.
Please help.
DWORD Datatal::CSerialSocket::Connect()
{
//Disconnect if we are connected.
if (m_hComm)
{
CloseHandle(m_hComm);
}
//Lets connect to the port
char szPortName[10];
sprintf(szPortName, "\\\\.\\COM%d", m_dwComPort);
m_hComm = CreateFile(
szPortName, // Name
of port
GENERIC_READ|GENERIC_WRITE, // access ( read and write)
0, // (share) 0: cannot share
the COM port
0, // security (None)
OPEN_EXISTING, // creation : open_existing
FILE_FLAG_OVERLAPPED, // we want overlapped
operation FILE_FLAG_OVERLAPPED
0 // no templates file for
COM port...
);
// Check if we could open the port
if (m_hComm == INVALID_HANDLE_VALUE)
{
DWORD dwRes = GetLastError();
if (ERROR_ACCESS_DENIED == dwRes) WriteLog(1, "Connect
failed, Port is already open");
if (ERROR_FILE_NOT_FOUND == dwRes) WriteLog(1, "Connect
failed, The specified port do not exist");
WriteLog(1, "Connect failed, CreateFile, unspecified
error: %d", dwRes);
return dwRes;
}
//Com settings structure
DCB dcb = {0};
dcb.DCBlength = sizeof(DCB);
if (!GetCommState (m_hComm, &dcb))
{
CloseHandle(m_hComm);
DWORD dwRes = GetLastError();
WriteLog(1, "Connect failed, GetCommState, unspecified
error: %d", dwRes);
return dwRes;
}
dcb.fBinary=TRUE;
dcb.fDsrSensitivity=false;
dcb.fOutX=false;
dcb.fInX=false;
dcb.fNull=false;
dcb.fAbortOnError=TRUE;
dcb.fOutxCtsFlow=FALSE;
dcb.fOutxDsrFlow=false;
dcb.fDtrControl=DTR_CONTROL_DISABLE;
dcb.fDsrSensitivity=false;
dcb.fRtsControl=RTS_CONTROL_DISABLE;
dcb.fOutxCtsFlow=false;
dcb.fOutxCtsFlow=false;
// custom
dcb.BaudRate = m_dwBaudRate;
dcb.ByteSize = m_bByteSize;
dcb.Parity = m_bParity;
if ( m_dwStopBits == 1 )
dcb.StopBits = ONESTOPBIT;
else if (m_dwStopBits == 2 )
dcb.StopBits = TWOSTOPBITS;
else
dcb.StopBits = ONE5STOPBITS;
//Tell the comport how it should work.
if (!SetCommState (m_hComm,&dcb))
{
CloseHandle(m_hComm);
DWORD dwRes = GetLastError();
WriteLog(1, "Connect failed, SetCommState, unspecified
error: %d", dwRes);
return dwRes;
}
// We want to Receive events.
//SetCommMask( m_hComm, EV_RXCHAR|EV_TXEMPTY);
// Set some timeouts.
COMMTIMEOUTS timeouts;
if(!GetCommTimeouts (m_hComm, &timeouts))
{
CloseHandle(m_hComm);
DWORD dwRes = GetLastError();
WriteLog(1, "Connect failed, GetCommTimeouts,
unspecified error: %d", dwRes);
return dwRes;
}
WriteLog(2, "timeouts: ReadInterval: %d, ReadTotalConstant: %d,
ReadTotalMultiplier: %d, WriteTotalConstant: %d, WriteTotalMultiplier:
%d",
timeouts.ReadIntervalTimeout,
timeouts.ReadTotalTimeoutConstant,
timeouts.ReadTotalTimeoutMultiplier,
timeouts.WriteTotalTimeoutConstant,
timeouts.WriteTotalTimeoutMultiplier
);
//defaults, I think...
//ReadInterval: 10, ReadTotalConstant: 0, ReadTotalMultiplier:
0, WriteTotalConstant: 5000, WriteTotalMultiplier: 0
timeouts.ReadIntervalTimeout = 10; // time
between chars
timeouts.ReadTotalTimeoutConstant = 0; //
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 5000;
timeouts.WriteTotalTimeoutMultiplier = 0;
if(!SetCommTimeouts (m_hComm, &timeouts))
{
CloseHandle(m_hComm);
DWORD dwRes = GetLastError();
WriteLog(1, "Connect failed, SetCommTimeouts,
unspecified error: %d", dwRes);
return dwRes;
}
//Flush the port.
//PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR |
PURGE_RXABORT | PURGE_TXABORT);
// Done with connecting, launch the thread
ResetEvent(m_hStopEvent);
ResetEvent(m_hNewDataEvent);
// start the thread
if (!m_bThreadRunning)
{
WriteLog(1, "Connect OK, Trying to start the thread");
Start();
}
else
WriteLog(1, "Connect OK, Thread is already running");
//connect
HandleConnect();
return 0;
}
My overlapped ReadFile doesnt complete on one machine, it do on another.
I've supplied the connect function, and the ThreadFunc that readfile
takes place in.
Please help.
DWORD Datatal::CSerialSocket::Connect()
{
//Disconnect if we are connected.
if (m_hComm)
{
CloseHandle(m_hComm);
}
//Lets connect to the port
char szPortName[10];
sprintf(szPortName, "\\\\.\\COM%d", m_dwComPort);
m_hComm = CreateFile(
szPortName, // Name
of port
GENERIC_READ|GENERIC_WRITE, // access ( read and write)
0, // (share) 0: cannot share
the COM port
0, // security (None)
OPEN_EXISTING, // creation : open_existing
FILE_FLAG_OVERLAPPED, // we want overlapped
operation FILE_FLAG_OVERLAPPED
0 // no templates file for
COM port...
);
// Check if we could open the port
if (m_hComm == INVALID_HANDLE_VALUE)
{
DWORD dwRes = GetLastError();
if (ERROR_ACCESS_DENIED == dwRes) WriteLog(1, "Connect
failed, Port is already open");
if (ERROR_FILE_NOT_FOUND == dwRes) WriteLog(1, "Connect
failed, The specified port do not exist");
WriteLog(1, "Connect failed, CreateFile, unspecified
error: %d", dwRes);
return dwRes;
}
//Com settings structure
DCB dcb = {0};
dcb.DCBlength = sizeof(DCB);
if (!GetCommState (m_hComm, &dcb))
{
CloseHandle(m_hComm);
DWORD dwRes = GetLastError();
WriteLog(1, "Connect failed, GetCommState, unspecified
error: %d", dwRes);
return dwRes;
}
dcb.fBinary=TRUE;
dcb.fDsrSensitivity=false;
dcb.fOutX=false;
dcb.fInX=false;
dcb.fNull=false;
dcb.fAbortOnError=TRUE;
dcb.fOutxCtsFlow=FALSE;
dcb.fOutxDsrFlow=false;
dcb.fDtrControl=DTR_CONTROL_DISABLE;
dcb.fDsrSensitivity=false;
dcb.fRtsControl=RTS_CONTROL_DISABLE;
dcb.fOutxCtsFlow=false;
dcb.fOutxCtsFlow=false;
// custom
dcb.BaudRate = m_dwBaudRate;
dcb.ByteSize = m_bByteSize;
dcb.Parity = m_bParity;
if ( m_dwStopBits == 1 )
dcb.StopBits = ONESTOPBIT;
else if (m_dwStopBits == 2 )
dcb.StopBits = TWOSTOPBITS;
else
dcb.StopBits = ONE5STOPBITS;
//Tell the comport how it should work.
if (!SetCommState (m_hComm,&dcb))
{
CloseHandle(m_hComm);
DWORD dwRes = GetLastError();
WriteLog(1, "Connect failed, SetCommState, unspecified
error: %d", dwRes);
return dwRes;
}
// We want to Receive events.
//SetCommMask( m_hComm, EV_RXCHAR|EV_TXEMPTY);
// Set some timeouts.
COMMTIMEOUTS timeouts;
if(!GetCommTimeouts (m_hComm, &timeouts))
{
CloseHandle(m_hComm);
DWORD dwRes = GetLastError();
WriteLog(1, "Connect failed, GetCommTimeouts,
unspecified error: %d", dwRes);
return dwRes;
}
WriteLog(2, "timeouts: ReadInterval: %d, ReadTotalConstant: %d,
ReadTotalMultiplier: %d, WriteTotalConstant: %d, WriteTotalMultiplier:
%d",
timeouts.ReadIntervalTimeout,
timeouts.ReadTotalTimeoutConstant,
timeouts.ReadTotalTimeoutMultiplier,
timeouts.WriteTotalTimeoutConstant,
timeouts.WriteTotalTimeoutMultiplier
);
//defaults, I think...
//ReadInterval: 10, ReadTotalConstant: 0, ReadTotalMultiplier:
0, WriteTotalConstant: 5000, WriteTotalMultiplier: 0
timeouts.ReadIntervalTimeout = 10; // time
between chars
timeouts.ReadTotalTimeoutConstant = 0; //
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 5000;
timeouts.WriteTotalTimeoutMultiplier = 0;
if(!SetCommTimeouts (m_hComm, &timeouts))
{
CloseHandle(m_hComm);
DWORD dwRes = GetLastError();
WriteLog(1, "Connect failed, SetCommTimeouts,
unspecified error: %d", dwRes);
return dwRes;
}
//Flush the port.
//PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR |
PURGE_RXABORT | PURGE_TXABORT);
// Done with connecting, launch the thread
ResetEvent(m_hStopEvent);
ResetEvent(m_hNewDataEvent);
// start the thread
if (!m_bThreadRunning)
{
WriteLog(1, "Connect OK, Trying to start the thread");
Start();
}
else
WriteLog(1, "Connect OK, Thread is already running");
//connect
HandleConnect();
return 0;
}