Link to home
Start Free TrialLog in
Avatar of Clint112799
Clint112799

asked on

How to Close Com Ports in Windows?

Hello,
I have a program that is written in C++ and uses CreateProcess to open Dos programs that use the COm ports. Well the only way I can get this to work is by calling a Batch file from CreateProcess that opens the Dos program from the Batch File. One MAJOR problem I found was that the com port stays open after I closed the Dos program. I have check "Close on Exit" in the options and have even tried to put COM1AutoAssign=2 in the Windows 95\98 system.ini file. This doesn't work either. How can I close the com port once I have closed the Dos program??

Thanks
Clinton
Avatar of chensu
chensu
Flag of Canada image

It is the DOS program's responsibility to close the COM port.
Avatar of Gus012498
Gus012498

Did you try ShellExecute to start the dos program?
Avatar of Clint112799

ASKER

Hello Gus,
I have tried ShellExecute, spawn, and createProcess with no luck. Unless I have not done the code correctly, and that's why I haven't been able to get it working? With these spawn applications I found that it doesn't let the Dos program to use the COm ports. The only why I was able to get the com ports to work correctly with the Dos program was using a Batch file. But the side affect is that the com port doesn't close when I try to us it from WIndows. But If i open another Dos program I have no problems? ANy ideals on this? I've been trying to figure this one out for at lest a month.

Thanks
Clinton
Chensu,
Well thats the funny thing. If I use CreateProcess, spawn or ShellExecute it first of all causes the Dos program not to work with the COm ports. So I used a batch file which open the DOs programs and enable the Dos program to use the com ports. But it them cause the problem with tryiing to close the com port. But if I open the Dos program though WIndows Explorer the program works fine and even closes fine? THis is what has me so confused! Why does a spawn call cause these probelms but yet I can open and run the Dos application though windows explorer

Thanks
Clinton



Try this code. Im not sure why you need to do this because a program that uses a COM port should obviously have the capabilities in and of itself to open a port.  But this code snippet is how you would programmatically open a COM port for reading and writing in the Win32 environment.


/* Open the COM port for reading and   writing. */
hCom = CreateFile(
"COM2", GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);


/* Make sure handle is valid. */
if (hCom == INVALID_HANDLE_VALUE)
{    
  cout << GetLastError() << flush;
  return 0;
}
..
..
Open your program that uses the COM ports.
..
..
/* To close port use */
CloseHandle(hCom);



Im not sure if this would work because other programs should be blocked from using the port specified in CreateFile once the call has been executed.


However there are parameters called FILE_SHARE_READ and FILE_SHARE_WRITE used in CreateFile() to allow sharing of files between processes, however Im not sure if that applies to COM ports.

Good Luck.
ASKER CERTIFIED SOLUTION
Avatar of Gus012498
Gus012498

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Gus, dmaroff, chensu,

Thanks for the help on my program. I do have one thing that might be the reason I can't get this to work. There are two Articles from MSN: Q130402, Q138159. Since I don't know anything about WIndows programming I'm hoping that this will clear up some of the issues. Mybe this is what I'm runign into?

Gus,
As for the Batch File you suggested, do I still open it the same way as I did before with CreateProcess?

Thanks
Clinton