Hello everybody!
I have a little problem with serial ports.
I'm writing a program that verifies if a serial port,
(example: Com5), is open or not. I use the function
CreateFile() trying to open the port, if I can I close
it, but if I get the error: "ERROR_ACCESS_DENIED", (with
GetLastError()), the port is already open and I do some
stuff.
Ok, all this work fine, but I don't want to hear the
"TICK" from the modem when I call CreateFile() and I open
the port, because I try to open the port with a timer each
2 seconds, and hear that sound, it's not nice.
There is any function that tell me if the port is open or
not?.
This is the code:
HANDLE hComm;
char* strPort="Com5";
hComm = CreateFile( strPort,GENERIC_READ | GENERIC_WRITE,
0, 0,OPEN_EXISTING,FILE_FLAG_OVERLAPPED, 0);
if (hComm == INVALID_HANDLE_VALUE)
{
if (GetLastError()!= ERROR_ACCESS_DENIED)
ShowError(GetLastError()); //own function that show
the message error
else
{
//here I make some stuff.
...
}
}
else
{
//I could open the port, then, I close it.
CloseHandle(hComm);
}
A few years ago, I maked the same program with Visual
Basic, with the same basics: try to open the port to see
if it's already open or not; and I never heard the "TICK"
from the modem. I guess that I can do the same with VC++.
Thank you very much, and sorry about my english. : )
Regards from Argentina.
Demian.