Monitor Com Ports Problem!

Hello,
I'm using Borland Builder C++ and have some code the monitors the status of the Com ports with two LEDS. Well I've found out that what I have done cause the com port not to release it to the Dos program once I open it with CreateProcess. If the Dos program does get hold of the COm port once I close the DOs program the com port stays open. Can someone look at my code or give some othe suggestion on how to monitor the COm ports correlty without casuing this problem?

Thanks
Clinton


//This is OpenPort1.cpp

//(c) Jon Jenkinson 1997
//Modified by Clinton Langan

//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

#include <dos.h>
#include "OpenPort1.h"
//#include "Serial.h"
#include "MainMenuSource.h"

//---------------------------------------------------------------------------
//   Important: Methods and properties of objects in VCL can only be
//   used in a method called using Synchronize, for example:
//
//      Synchronize(UpdateCaption);
//
//   where UpdateCaption could look like:
//
//      void __fastcall LineChecker::UpdateCaption()
//      {
//        Form1->Caption = "Updated in a thread";
//      }
//---------------------------------------------------------------------------
__fastcall LineChecker::LineChecker(bool CreateSuspended)
      : TThread(CreateSuspended)
{
//Note::
//This is the Constructor.  Actual constructor code should really
//go into the execute function.
}
//---------------------------------------------------------------------------
void __fastcall LineChecker::Execute()
{
//File handle required locally
HANDLE ReturnedHandle;

//---- Place thread code here ----

//Be pedantic.  You should really Set in use semaphore to
//check if we try and run twice at the same time.


do   //Unbreakable from here,  only released via main app(see last while)
{

      //=****Actual Line checking

      do      //While file is open by another user, (i.e. modem in use)
        {
            ReturnedHandle = CreateFile(//As per Win32 help !
                           "COM1",
                        GENERIC_READ | GENERIC_WRITE,
                        0,
                        NULL,
                        OPEN_EXISTING,
                        FILE_FLAG_SEQUENTIAL_SCAN,
                        NULL);
              IsActive = true;//turn the local online semaphore flag on
            Synchronize(UpdateLEDDisplay);//Change LED State
        }while(ReturnedHandle == INVALID_HANDLE_VALUE);

        //line is free,  so we've opened it by ourselves,  
      //we must close it to allow other programs access
      if(CloseHandle(ReturnedHandle))
            {
            //Application->MessageBox("Line Clear","MB",MB_OK);
                IsActive = false;//turn the local online flag off
            Synchronize(UpdateLEDDisplay);//turn the LED off
                sleep(1);//let someone else have access
            }
      else
            {//This should never happen,  but then again
       Application->MessageBox("Line Error,  Open already","MB",MB_OK);
            }

      //=*****End of actual line checking


}while(Terminated == false);//Application has terminated

FreeOnTerminate = true; //being pedantic
Terminate();            //being very pedantic
}
//---------------------------------------------------------------------------

void __fastcall LineChecker::UpdateLEDDisplay()
{//Set the state of the LED on form1 based on local semaphore
      switch(IsActive)
          {
        case true://turn LED on for open port "in uses"

            FormMain->On_Line->Visible = true;//Show online state
            FormMain->On_Line->Brush->Color = clRed;

              FormMain->OnOff = true;

            break;
        case false://turn LED off for closed port "not used"

            FormMain->On_Line->Visible = true;//Show online state
            FormMain->On_Line->Brush->Color = clGreen;
            ::Sleep(30); /*give us chance to see the cycling*/
            FormMain->On_Line->Repaint();
            FormMain->On_Line->Visible = true;//Show online state
            FormMain->On_Line->Brush->Color = clLime;

            FormMain->OnOff = true;

              break;
        default://ignore: This should be impossible,  but we really
            //should check
              break;
        }
}

//This is in my Main from with the LEDS

//set OnOff value
//Set Semaphore

OnOff = false;

//Create and set thread for the LED display running "Com1"

LineChecker *ThreadLineChecker;
ThreadLineChecker = new LineChecker(true);

////Create and set thread for the LED display running "Com2"

//LineChecker2 *ThreadLineChecker2;
//ThreadLineChecker2 = new LineChecker2(true);


//and start it going

ThreadLineChecker->Resume();

//ThreadLineChecker2->Resume();

On_Line->Visible = true;//Show online state

//On_Line2->Visible = true;//Show online state




Clinton112299Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexVirochovskyCommented:
Hi , clinton. I see , that this Q. is connected with you's previous .
Is this Q acually jet, or you solve this problem?
0
Clinton112299Author Commented:
Hello Alex,

No one has solved this one yet. The problem is again that I'm opeing a Dos program to use the com ports. I have tried the above code to monitor the Com ports status with LEDs on my mainform but found out that the code interfears with Dos trying to open the com ports. But once it does open the com port is doesn't let windows get it back. Once I disabled the com status code I have no problems. I would like to get something working just to show the customer (user) that the port is open or not.

Thanks
Clinton
0
AlexVirochovskyCommented:
I think, that status of COM port
youi must test in Dos, becouse
test in Windows really itrerrupt
Dos operation!  You way may be as:
1. You write small dos utility , using
status= bioscom (3, 0, 1)//for port1
and save result to disk
2. In BCB you start this apps(
with help PIF file, of couse!), and
after some delay test existing result
file on disk. If file exists, read status,
no: delay  
3. I use this way in my practice, but
some experts use pipes for connecting with Dos apps(only in NT). If you apps works in NT , you
can try pipes: this is faster, of couse,
but not so easy(from DOS side).
Alex
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Clinton112299Author Commented:
Alex,
I only use this in Windows95\98. Do you have any examples of this is done in C and Dos. I never wrote code for Dos. Is there a site that talks about this.


Thanks
Clinton

Have a Merry Christmas and Happy new Year
0
AlexVirochovskyCommented:
Clinton, Have a Merry Christmas and Happy new Year, too, but you must know: I am Hebrew, and we don't felicitate Christmas , about new year:
new year 5760 was began in September ("rosh ashana").
About our problem : text in dos you
can wirte in BCB , too, as console
Apps. Next is simple code(if you
can't make exe file, i will send you
write yoy EMail
// execution comstat.exe 1 comstat.dat
#include <bios.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void main(int np, char *prm[])
{
   FILE *fOut;
   int nCom = 0;
   char szFileName[255];

   strcpy(szFileName,"comstat.dat");
   if (np > 1)
      nCom = atoi(prm[1]) - 1;
   if (np > 2)
     strcpy(szFileName, prm[2]);
   fOut = fopen (szFileName, "wt");
   if (fOut)
     {
       unsigned int nStat = _bios_serialcom(_COM_STATUS,nCom,0);
       fprintf(fOut,"%d",nStat);
       fclose(fOut);
     }
}      

result (for me)  24576
Interpretation of status see
in description of _bios_serialcom
(or bioscom)

BTW: I send it as reply, becouse i
wrote code specially for you!
Alex
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Clinton112299Author Commented:
Hello Alex,

I havn't had the time to try your code on my Com Status problem. I finally did some work today which ended me up with errors. I think that C++ doesn't like the _ in front of some of the code. Can you please send me the .exe when you have the time. I'll try and figure out why it doesn't like the code. It complies but gets stuck on .obj.

Thanks
Clinton
0
AlexVirochovskyCommented:
Yes, i can send you exe, write you EMail.
0
Clinton112299Author Commented:
Thanks
clangan@hitechgaming.nb.ca
0
Clinton112299Author Commented:
Hello Alex,

I have tried the program and the status value comes back as 24576. If I open Hyperterm and put com1 in use and run the program is still comes back as 24576. Shouldn't this value change depending on the port?

Thanks
Clinton
0
AlexVirochovskyCommented:
hi, i send you file bcdos with help of all dos functions(and all statuses!).
About comstat: i 've tested it and send you again exe and 4 files(by ports)
In my case
1: empty, 2: mouse, 3: empty, 4 : internet
Test on you computer. Alex

0
Clinton112299Author Commented:
Alex,
Thanks for all your help with this. I havn't had the time to get this going as yet but hope that I can get something working.

Thanks again for all your help

Sinceraly

Clinton
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Software

From novice to tech pro — start learning today.