Open Com port failed (multi threading)

  • Thread starter Thread starter Droopy
  • Start date Start date
D

Droopy

Hi,

I wrote a C# program that is calling C++ legacy code to open a serial port
(using CreateFile (comPort,
GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL))
..
So far, it works.
Now, at startup, I start 4 C# threads.
These threads are opening Com ports #2, #3, #4 and #5.
Sometimes, these 4 Com ports are opened successfully but, most of the time,
1 Com port opening failed.
The GetLastError returns 5 (access denied).

So, my question is :

Is it safe to call 4 CreateFile (for different Com ports of course) in
parallel thread at the same time ?

Thanks in advance for your help,

Droopy.
 
Hi,

I wrote a C# program that is calling C++ legacy code to open a serial
port (using CreateFile (comPort,
GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NU
LL)) .
So far, it works.
Now, at startup, I start 4 C# threads.
These threads are opening Com ports #2, #3, #4 and #5.
Sometimes, these 4 Com ports are opened successfully but, most of the
time, 1 Com port opening failed.
The GetLastError returns 5 (access denied).

So, my question is :

Is it safe to call 4 CreateFile (for different Com ports of course) in
parallel thread at the same time ?

Thanks in advance for your help,

Droopy.

Who can help me ?
 
Yes it is. The "Access denied" error code has IMO nothing to do with the
number of threads, are you sure this error is returned by the CreateFile
call?

Willy.
 
Yes.
To be more precise, CreateFile returns INVALID_HANDLE_VALUE and
GetLastError () returns 5 (Access Denied)

Sorry for this late answer.
 
Who can help me ?
Have I to start it sequentially or add any timeout between thread
starting ?
 
Back
Top