Re: serial port Writefile: sometimes 998 ERROR_NOACCESS
Hello,
On 24.12.2010 20:35, mfc wrote:
BOOL CASocThread::InitSerialThreads()
{
for(int i=0; i<16; i++) //the number 16 will be removed /
replaced by a #define
You should also think about a static member of the CAsocThread class
which defines the number of threads, or a parameter to the constructor
(if applicable).
{
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;
}
So when one thread fails to start then the m_wSerialThreads array will
contain some threads which are initialized at least one NULL and the
rest will be random data from your memory ?!
You could make the m_wSerialThreads a list and only add created threads,
this way a lot of parts of the software won't need to know
how many threads are there before hand. Because the fixed size array
for sure guarantees a lot of code depending on the size of the
fixed size array.
CString port;
if(i< 10)
port.Format(_T("\\\\.\\COM%d"), i);
else
port.Format(_T("\\.\\COM%d"), i);
This should be unnecessary. The log form of comport names
can be used for comports below and above 10.
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;
}
You return and there will be a thread created with invalid/random
thread->params data.
Again I would suggest to add a thread to your collection only after
it has been successfully created and completely initialized.
Best regards,
Oliver