Re: serial port Writefile: sometimes 998 ERROR_NOACCESS
Here`s my actual initialization of all these serial writer threads:
I`ve added one CWinThread* array which contains all thread objects for
the CASocThread (which will send information to these serial ports) as
well as a shutdown_event array to shutdown these threads under un-
normal conditions.
BOOL CASocThread::InitSerialThreads()
{
for(int i=0; i<16; i++) //the number 16 will be removed /
replaced by a #define
{
m_wSerialThreads[i] =
(CSerialWriterThread*)AfxBeginThread(RUNTIME_CLASS(CSerialWriterThread),
THREAD_PRIORITY_NORMAL,
0,
CREATE_SUSPENDED);
if (m_wSerialThreads[i] == NULL)
{ // failed to start
TRACE("m_writerThread failed\n");
return FALSE;
}
CString port;
if(i < 10)
port.Format(_T("\\\\.\\COM%d"), i);
else
port.Format(_T("\\.\\COM%d"), i);
HANDLE hCom = ::CreateFile(port, // filename
GENERIC_READ | GENERIC_WRITE, // desired access
0, // exclusive
NULL, // security irrelevant
OPEN_EXISTING, // it has to be there
0, //FILE_FLAG_OVERLAPPED, // open asynchronous
NULL); // template file
if (hCom == INVALID_HANDLE_VALUE)
{
DWORD err = ::GetLastError();
return FALSE;
}
//shutdown event: does every serial writer thread needs its own
shutdownevent?
m_ShutdownEvents[i] = ::CreateEvent(NULL, // security
TRUE, // manual-reset
FALSE, // not signaled
NULL); // anonymous
m_wSerialThreads[i]->parms = new CSerialParameters(hCom,
AfxGetMainWnd(), m_ShutdownEvents[i]);
m_wSerialThreads[i]->ResumeThread();
}
return TRUE;
}
If added this function here:
CASocThread init:
m_asocThread=
(CASocThread*)AfxBeginThread(RUNTIME_CLASS(CASocThread),
THREAD_PRIORITY_NORMAL,
0,
CREATE_SUSPENDED);
if (m_asocThread== NULL)
{ // failed to start
return NET_Err_AfxBeginThread;
}
//start serial port threads ((INIT OF THE SERIAL PORT THREADS)
m_asocThread->InitDmxThreads();
//add specific params
ASocThParams *params = new ASocThParams (AfxGetMainWnd(),
(CWnd *) m_pNodeListWnd,
(CXmlFile *) m_prop);
m_asocThread->SetParameters((LPVOID) params);
// Now start the thread.
m_asocThread->ResumeThread();
Is this initialization of both threads are ok?
best regards
Hans