• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

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
0
Clint112799
Asked:
Clint112799
1 Solution
 
chensuCommented:
It is the DOS program's responsibility to close the COM port.
0
 
Gus012498Commented:
Did you try ShellExecute to start the dos program?
0
 
Clint112799Author Commented:
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
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Clint112799Author Commented:
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



0
 
dmaroffCommented:
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.
0
 
Gus012498Commented:
If you open the Dos Program from the batch file, try this code:

      osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);           //  Fill in size
      GetVersionEx( &osvi );                               //  Get OS version info
                                        //  Start a DOS box
if( osvi.dwPlatformId == VER_PLATFORM_WIN32_NT )          
//  If it's WinNT
sprintf(commandLine,"cmd /C %s",
    commandString);//  Use CMD.EXE
else                                    //  Use command.com for Win95
sprintf(commandLine,"c:\\command.com /C %s",commandString);
bStatus = CreateProcess( NULL, commandLine, NULL, NULL, TRUE,0,
      NULL, NULL, &si, &pi );

I hope this helps

Gus
0
 
Clint112799Author Commented:
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
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now