Announcement

Collapse
No announcement yet.

How to tell if a serial port is "free"

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to tell if a serial port is "free"

    Hello all,

    Is there a way in PBCC40 to tell if a serial port (COM port) is already in use by another application under Windows?

    I would like to be able to tell not only if a serial port is available on a computer, but if an application is already using it.

    I wrote some simple code to scan for available serial ports:

    Code:
        STDOUT "The following COM ports are available:"
        STDOUT ""
        FOR x=1 TO 255
            a$="COM"+LTRIM$(STR$(x))
            COMM OPEN a$ AS #1
            ON ERROR GOTO Loop1
            PRINT "COM";x;" is available."
            COMM CLOSE #1
        Loop1:
        NEXT x
    I have found that the COMM OPEN statement is reliable in determining the existence of the serial ports present on a machine, however, even if I open a connection to an available COM port using another application like HyperTerminal, COMM OPEN still indicates that the port is available.

    Thanks!

  • #2
    > COMM OPEN still indicates that the port is available.
    Are you sure?
    Code:
    ....
       COMM OPEN a$ AS #1
       ON ERROR GOTO Loop1
    ....
    You need to execute the ON ERROR GOTO before executing the statement(s) for which an error trap is to be actve.

    You don't need to use ON ERROR, you can just test the system ERR variable.. AFAIK that will tell you if there was an error on the COMM OPEN regardless of the ON ERROR GOTO situation.

    ie. I think something like this should work
    Code:
        FOR x=1 TO 255
            ERRCLEAR               ' clear the ERR variable
            a$="COM"+LTRIM$(STR$(x))
            COMM OPEN a$ AS #1                 ' attempt OPEN
            IF ERR THEN                              ' OPEN failed 
               Print/Stdout USING$ ("Could not open COM # Err # &", X, ERR, ERROR$(ERR))
            ELSE   ' OPEN succeeds so we have to close the handle
               COMM CLOSE #1
           END IF
      NEXT
    MCM
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Michael your code wont work when you attempt to open a port above 9.

      The code below will.

      Code:
      FOR x=1 TO 255
          ERRCLEAR               ' clear the ERR variable
      
          IF x > 9 THEN
             a$ = "\\.\COM" + FORMAT$(x)
          ELSE
             a$ = "COM" + FORMAT$(x)
          END IF
      
          COMM OPEN a$ AS #1                 ' attempt OPEN
          IF ERR THEN                              ' OPEN failed 
             Print/Stdout USING$ ("Could not open COM # Err # &", X, ERR, ERROR$(ERR))
          ELSE   ' OPEN succeeds so we have to close the handle
             COMM CLOSE #1
          END IF
      NEXT
      Windows com ports get all funny past 9.
      Refer to the help regarding "OPEN COM"

      The "COM" + ltrim$(str$(x)) is ok of course, but format$ is a little clearer.

      I forgot to add that if you are cycling through the com ports in this manner, you really, really need to set the com timeouts via the api.
      Otherwise, if you bump into a Bluetooth Virtual Com Port (Bt can use up to 8 com ports in a standard installation on a PC) your program will hang.
      In fact, if you kill your app with Task Manager, it can still take some time to terminate and release the port.

      Best Regards
      Last edited by Gary Barnes; 4 Apr 2008, 12:20 PM. Reason: Blue tooth stuff
      Gary Barnes
      The Control Key

      If you are not part of the solution
      then you are either a gas, solid, plasma or some other form of matter.

      Comment


      • #4
        I personally don't know squat about COMM port programming.

        It looked to me like there was a misunderstanding re using "ON ERROR GOTO.." so I was addressing that.

        MCM
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          Originally posted by Gary Barnes View Post
          I forgot to add that if you are cycling through the com ports in this manner, you really, really need to set the com timeouts via the api.
          Otherwise, if you bump into a Bluetooth Virtual Com Port (Bt can use up to 8 com ports in a standard installation on a PC) your program will hang.
          In fact, if you kill your app with Task Manager, it can still take some time to terminate and release the port.

          Best Regards

          That, Flushing, and a few other things work much nicer with API. I've suggested these before as possible enhancements to the existing language, so they are aware of them. As for checking for them being in use...I think I had asked this before too and someone gave me API to do it...other than that all you have is error checking after OPEN.
          sigpic
          Mobile Solutions
          Sys Analyst and Development

          Comment


          • #6
            Many thanks to all. Michael, clearly I misunderstood the correct usage of the ON ERROR statement - definitely a case of RTFM..

            Thanks also for the additional information about scanning COM ports.

            Roger - I am new to Windows programming and am unsure about how API calls are actually implemented in PBCC. Other than the PB website and manual, is there a good source of information on calling APIs from PBCC that you would recommend from personal experience?

            Could you also point me in the right direction by advising which API call I would use in this specific case?

            Comment


            • #7
              I forgot to ask Gary; please could you explain why the following code in your example is necessary? I understand what it does but don't know why it is needed when COM port numbers greater than 9 are being specified.

              Code:
                  IF x > 9 THEN
                     a$ = "\\.\COM" + FORMAT$(x)
                  ELSE
                     a$ = "COM" + FORMAT$(x)
                  END IF
              Thanks, Robin.

              Please ignore this, I found the answer in the manual! Sorry.
              Last edited by Robin England; 5 Apr 2008, 11:35 AM. Reason: I found the answer! Sorry.

              Comment


              • #8
                Have'nt done any port programming in a long time, but remember that to get caller id to work other conditions had to be met. Found this code that also tested other states before attempting to use a device.
                Code:
                #COMPILE EXE
                #DIM ALL
                #INCLUDE "win32api.inc"
                GLOBAL ghComm AS LONG
                FUNCTION PBMAIN () AS LONG
                END FUNCTION
                SUB CallerID
                  LOCAL BufferBytes AS LONG
                  BufferBytes& = 4096
                  COMM SET #ghcomm, RXBUFFER = BufferBytes&
                  IF ERR THEN
                    ? "CAN'T SET RXBUFFER"
                    EXIT SUB
                  END IF
                 
                  COMM SET #ghcomm, BAUD = 1200
                  IF ERR THEN
                    ? "CAN'T SET BAUD"
                    EXIT SUB
                  END IF
                  COMM  SET #ghcomm, PARITY = %FALSE
                  IF ERR THEN
                    ? "CAN'T SET PARITY"
                    EXIT SUB
                  END IF
                  COMM SET #ghcomm, BYTE = 8
                  IF ERR THEN
                    ? "CAN'T SET DATA BYTES TO 8"
                    EXIT SUB
                  END IF
                  COMM SET #ghcomm, STOP = 0   '0 = 1 stop bit
                  IF ERR THEN
                    ? "CAN'T SET STOP BIT"+STR$(ERR)
                    EXIT SUB
                  END IF
                END SUB

                Comment

                Working...
                X