Announcement

Collapse
No announcement yet.

Find Modem

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

  • Find Modem

    Searching for code to find Comport with a modem attached, all that I found (plenty hits) were more than either I wanted to fool with, or more than I understood. So here's a simple version I came up with. Probably lots of bad stuff is gonna happen (BSOD's, GPF's, ...) but it works on my machine at least (so far).

    '
    Code:
    'Find Modem.bas  - 'XP SP3  - PBWin 8.03
    #Compile Exe  
    #Include "WIN32API.INC"
     
    Sub Find_Modem_Comport
       Local ctr&, byts&, ComPort$, ncomm&, prts$, n$
       ncomm = FreeFile
       ErrClear
       For ctr = 0 To 9             
         COMPORT$ = "COM" & Using$("# ", ctr)
     
        Comm Open COMPORT$ As #nComm
        Sleep 100 'JIC it needs the time to open 
     
        If Err Then 'no modem attached here
           Close ncomm  'JIC
           ErrClear     'set to 0 for next try
          Else                                
           'Got a modem maybe
           Comm Set #nComm, Baud     = 115200   ' 9600 baud
           Comm Set #nComm, Byte     = 8        ' 8 bits
           Comm Set #nComm, Parity   =  %False   ' No parity
           Comm Set #nComm, Stop     = 0        ' 1 stop bit
           Comm Set #nComm, TxBuffer = 4096     ' 4k transmit buffer
           Comm Set #nComm, RxBuffer = 4096     ' 4k receive buffer
     
          'n$ = "ATDT 1234567" & $CrLf '<< Hear dial tone and number dialed but returns nothing
          'n$ = "+++"    '<< returns nothing
          n$ = "AT" & $CrLf  'returns OK
          Comm Send #nComm, n$  
          Sleep 10 'wait for reply
                    'nothing returns regardless of how Long 
                    'except For "AT" which return "OK"
     
          byts = Comm(#nComm, RxQue)
          Comm Recv #nComm, byts, n$
          Comm Send #nComm, "ATH0" & $CrLf 'hang up JIC it dials
     
           If byts > 0 Then  'Valid modem available
             ? "Received " & n$,, COMPORT$ & " is GOOD "  
           End If
        End If                         
    '
        Close ncomm
       Next ctr        
       Close
    End Sub
     
     
    Function PBMain
    Call Find_Modem_Comport
    End Function
    '
    Last edited by Gösta H. Lovgren-2; 11 Aug 2008, 01:10 PM.
    It's a pretty day. I hope you enjoy it.

    Gösta

    JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
    LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

  • #2
    There are free sources at http://www.codeproject.com/KB/system/DevMgr.aspx and http://69.10.233.10/KB/system/SimpleSetup.aspx which might be helpful. Once you find the modem(s), check that it is ready for use (or there might be a method you can call to have Windows query the modem for you).

    If this project is just for yourself or a single client/employer, you have the advantage that you can check ahead of time on what serial devices are in use. But if this is for the general public, don't send AT commands to every serial device. There are lots of serial devices that are not modems, and you can't predict what would happen when someone runs your program.
    Erich Schulman (KT4VOL/KTN4CA)
    Go Big Orange

    Comment


    • #3
      Originally posted by Erich Schulman View Post
      There are free sources at http://www.codeproject.com/KB/system/DevMgr.aspx and http://69.10.233.10/KB/system/SimpleSetup.aspx which might be helpful. Once you find the modem(s), check that it is ready for use (or there might be a method you can call to have Windows query the modem for you).
      Thanks the links, Erich. They look way more than I want to, or can, handle right now though.


      If this project is just for yourself or a single client/employer, you have the advantage that you can check ahead of time on what serial devices are in use. But if this is for the general public, don't send AT commands to every serial device. There are lots of serial devices that are not modems, and you can't predict what would happen when someone runs your program.
      Excellent point. While it won't have a wide distribution (probably just family) it's back to the drawing boards for me. Thanks your reply.

      ====================================================
      "I'm not a member of any organized political party,
      I'm a Democrat!"
      Will Rogers (1879-1935)
      ====================================================
      It's a pretty day. I hope you enjoy it.

      Gösta

      JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
      LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

      Comment


      • #4
        After a further search, I found this:

        It won't work with a For/Next loop though.

        I'm wondering if it would have the same "conflict" as Erich pointed out with my code?
        '
        Code:
        'http://www.powerbasic.com/support/pbforums/showthread.php?t=6468&highlight=modem+detection
        'by Peter Lameijn
        '==================================================================================================
        ' Modem detection example. PST_xxx equates are in Winbase.h
        '==================================================================================================
        #Include "win32api.inc"
        %PST_MODEM = &h00000006&
        Function PBMain () As Long
        Local lRet&,lComm&,lCommProp As CommProp
        Local Comport$, ctr&, p_ptr&
         
         '<<< This commented section works fine.
        '  lComm& = CreateFile("COM1",%GENERIC_READ Or %GENERIC_WRITE,0,ByVal %Null,%OPEN_EXISTING,%FILE_FLAG_OVERLAPPED,ByVal %Null)
        '  If lRet = %INVALID_HANDLE_VALUE Then Exit Function
        '  lRet = GetCommProperties(lComm&,lCommProp)
        '  If lCommProp.dwProvSubType = %PST_MODEM Then ? "Modem on Com 2"
        '  CloseHandle lComm&
        '
        '  lComm& = CreateFile("COM2",%GENERIC_READ Or %GENERIC_WRITE,0,ByVal %Null,%OPEN_EXISTING,%FILE_FLAG_OVERLAPPED,ByVal %Null)
        '  If lRet = %INVALID_HANDLE_VALUE Then Exit Function
        '  lRet = GetCommProperties(lComm&,lCommProp)
        '  If lCommProp.dwProvSubType = %PST_MODEM Then ? "Modem on Com 2"
        '  CloseHandle lComm&
        '
        '  lComm& = CreateFile("COM3",%GENERIC_READ Or %GENERIC_WRITE,0,ByVal %Null,%OPEN_EXISTING,%FILE_FLAG_OVERLAPPED,ByVal %Null)
        '  If lRet = %INVALID_HANDLE_VALUE Then Exit Function
        '  lRet = GetCommProperties(lComm&,lCommProp)
        '  If lCommProp.dwProvSubType = %PST_MODEM Then ? "Modem on Com 3"
        '  CloseHandle lComm&
        '
        '  lComm& = CreateFile("COM4",%GENERIC_READ Or %GENERIC_WRITE,0,ByVal %Null,%OPEN_EXISTING,%FILE_FLAG_OVERLAPPED,ByVal %Null)
        '  If lRet = %INVALID_HANDLE_VALUE Then Exit Function
        '  lRet = GetCommProperties(lComm&,lCommProp)
        '  If lCommProp.dwProvSubType = %PST_MODEM Then ? "Modem on Com 4"
        '  CloseHandle lComm&
         
         
        '<<< However this does not work,  it gives a "Parameter Mismatch error            
         For ctr = 0 To 9
           'Doesn't work
        '  Comport$ = Left$("COM" & Using$("#", ctr), 4) & $Nul
        '  lComm& = CreateFile(Comport$, %GENERIC_READ Or %GENERIC_WRITE,0,ByVal %Null,%OPEN_EXISTING,%FILE_FLAG_OVERLAPPED,ByVal %Null)
         
           'Nor does this
          p_Ptr = StrPtr(Comport$) 'didn't work either
          lComm& = CreateFile(p_Ptr, %GENERIC_READ Or %GENERIC_WRITE,0,ByVal %Null,%
         
        OPEN_EXISTING,%FILE_FLAG_OVERLAPPED,ByVal %Null)
          If lRet = %INVALID_HANDLE_VALUE Then Exit Function
          lRet = GetCommProperties(lComm&,lCommProp)
          If lCommProp.dwProvSubType = %PST_MODEM Then ? "Modem on " & Comport$
          CloseHandle lComm&
         
         Next ctr                     
         
         
          ? "Done",,FuncName$
        End Function
        '
        Note there are other methods on there as well.
        Last edited by Gösta H. Lovgren-2; 11 Aug 2008, 06:04 PM. Reason: Cleanup on Aisles 7, 12, 25, ...
        It's a pretty day. I hope you enjoy it.

        Gösta

        JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
        LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

        Comment


        • #5
          Just to spurn on debate
          Code:
                byts = Comm(#nComm, RxQue)
                Comm Recv #nComm, byts, n$
                Comm Send #nComm, "ATH0" & $CrLf 'hang up JIC it dials
          I have YET to see anything reply to a modem call (unless just echoing what was sent to it) unless something was attached.

          Now just WHAT is attached that is a matter of what the core idea of the application is.....
          Engineer's Motto: If it aint broke take it apart and fix it

          "If at 1st you don't succeed... call it version 1.0"

          "Half of Programming is coding"....."The other 90% is DEBUGGING"

          "Document my code????" .... "WHYYY??? do you think they call it CODE? "

          Comment


          • #6
            Originally posted by Cliff Nichols View Post
            Just to spurn on debate
            Code:
                  byts = Comm(#nComm, RxQue)
                  Comm Recv #nComm, byts, n$
                  Comm Send #nComm, "ATH0" & $CrLf 'hang up JIC it dials
            I have YET to see anything reply to a modem call (unless just echoing what was sent to it) unless something was attached.
            Dunno what you mean by "attached" but in this case the n$ contains "OK" received from the modem according to the MsgBox:
            Code:
                  If byts > 0 Then  'Valid modem available
                     ? "Received " & n$,, COMPORT$ & " is GOOD "

            Now just WHAT is attached that is a matter of what the core idea of the application is.....
            Now THAT flies right over my head. The "core idea" in this case is to see if a modem is available on a comport. As Erich pointed out, the way I was trying was fraught with imminent peril. Peter L's solution seems the better way.

            ==================================
            "Give me chastity and continence,
            but not yet."
            Saint Augustine (354-430)
            ==================================
            It's a pretty day. I hope you enjoy it.

            Gösta

            JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
            LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

            Comment


            • #7
              Note for those following this thread, there is a cleaner version of Peter's code (updated to PBWin 8.04) version at: http://www.powerbasic.com/support/pb...018#post293018 (Post 24).

              =============================
              Let's have some new cliches.
              Samuel Goldwyn
              =============================
              It's a pretty day. I hope you enjoy it.

              Gösta

              JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
              LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

              Comment


              • #8
                Originally posted by Cliff Nichols View Post
                Just to spurn on debate
                I have YET to see anything reply to a modem call (unless just echoing what was sent to it) unless something was attached.
                And that is where the potential trouble lies.

                I have a Moxa 8-port serial card because I have multiple serial devices. My PDA needs 2 (one for PPP, one for sync). I also have VHF and UHF transceivers that use serial ports. If I get a terminal node connector for them, that's another serial port. I also have an external modem, and I have a serial mouse (normally on the shelf) so if my regular mouse breaks I've got something to tide me over until I can get to the store.

                Someone making a modem-oriented program would not know in advance that I have those diverse serial devices. What would each device do when getting a modem command? I have never bothered to find out, but it could be nothing or something disruptive. At least a program made for just yourself or a limited audience gives you the advantage of a chance to find out what devices are out there now and planned for the future.
                Erich Schulman (KT4VOL/KTN4CA)
                Go Big Orange

                Comment


                • #9
                  Originally posted by Gösta H. Lovgren-2 View Post
                  Note for those following this thread, there is a cleaner version of Peter's code (updated to PBWin 8.04) version at: http://www.powerbasic.com/support/pb...018#post293018 (Post 24).
                  Peter posted another version of his code here..
                  http://www.powerbasic.com/support/pb...ad.php?t=35506
                  Rgds, Dave

                  Comment


                  • #10
                    Maybe this will help List Available Serial Ports

                    I know I posted a more simplified listing somewhere, but could not find it in the search on my own name
                    Engineer's Motto: If it aint broke take it apart and fix it

                    "If at 1st you don't succeed... call it version 1.0"

                    "Half of Programming is coding"....."The other 90% is DEBUGGING"

                    "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                    Comment

                    Working...
                    X