We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Checking CD on serial port with API

paolo070697
paolo070697 asked
on
Medium Priority
215 Views
Last Modified: 2012-06-27
Hello, I need a piece of _working_ VB4 (32 bit) code (related declarations included) to check if there's CD (carrier detect) on COM1, without using MSComm control: I need to check the state without disturbing (or locking) a Remote Access procedure from getting control of the port, and I think I need to use Win32 API.
Ie, I need to _spy_ the connection to check when data exchange begins and ends.

Thank you,
       Paolo
Comment
Watch Question

Commented:
An alternate way is to use the RasEnumConnections to get the names of the currently active connections. This will not detect the actual modem carrier, but it will detect when there is an active connection, which I believe would fill the purpose?

Author

Commented:
Yes, I think it would fill the purpose, but I've checked and I don't happen to have the necessary VB declarations in Win32api.txt (or the like) that comes with VB. I would consider the question answered (and appreciate very much it, by the way), if you put in ALL the necessary declarations (including constants) and also some lines of working code, or someplace where to get them (declarations, constants, type definitions and samples for VB, of course).
Thank you,
        Paolo
Commented:

Declarations:
--------------------

Private Type rasconn
    dwSize As Long
    hrasconn As Long
    szEntryName As String * 257
    szDeviceType As String * 17
    szDeviceName As String * 130
End Type

Private Declare Function RasEnumConnections Lib "rasapi32" Alias "RasEnumConnectionsA" (buf As rasconn, numbytes As Long, numconns As Long) As Long


Code:
---------

Dim numbytes As Long, numconns As Long, success As Long
Dim rc As rasconn
rc.dwSize = Len(rc)
numbytes = Len(rc)
success = RasEnumConnections(rc, numbytes, numconns)
If numbytes > 0 Then
    ' Connect
    Print "Name of connection: "; rc.szEntryName
Else
    '
    Print "not connected"
End If

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Just one mre, and I'll grade the question: since the RasEnumConnections says ON before the connection is really on, I'd like to use the RasGetConnectStatus, but I can't get it to work, probably because I haven't figured out the right parameters (the main problem is that I don't have the file with declarations nor the C header file), mainly RASCONNSTATE enum and the RASCS_DONE constant. May I impose further on you for the necessary delarations ?

Thank you,

Paolo


Commented:
Sure. If you'd post your e-mail, I'll send you a copy of the full C header files too.

I might as well send you all the VB code I've written as samples for different people. This is really code for connecting and disconnecting, but I use the RasGetConnectStatus function in it too.

You may notice that the structures are bigger than sum of all members described in the API, probably due to alignment.

Private Const RASCS_DONE = &H2000
Private Const RASCS_Connected = RASCS_DONE
Private Const RASCS_Disconnected = RASCS_DONE + 1

Private Type rasconn
    dwSize As Long
    hrasconn As Long
    szEntryName As String * 257
    szDeviceType As String * 17
    szDeviceName As String * 130
End Type
Private Type RASCONNSTATUS
    dwSize As Long
    rasconnstate As Long
    dwError As Long
    szDeviceType As String * 17
    szDeviceName As String * 131
End Type
Private Type RASDIALPARAMS
    dwSize As Long
    szEntryName As String * 257
    szPhoneNumber As String * 129
    szCallbackNumber As String * 129
    szUserName As String * 257
    szPassword As String * 257
    szDomain As String * 19
End Type

Private Declare Function RasEnumConnections Lib "rasapi32" Alias "RasEnumConnectionsA" (buf As rasconn, numbytes As Long, numconns As Long) As Long


Private Declare Function RasDial Lib "rasapi32" Alias "RasDialA" (ByVal extensions As Long, ByVal phonebook As String, params As RASDIALPARAMS, ByVal notifiertype As Long, ByVal notifier As Long, rasconn As Long) As Long
Private Declare Function RasHangUp Lib "rasapi32" Alias "RasHangUpA" (ByVal rasconn As Long) As Long
Private Declare Function RasGetConnectStatus Lib "rasapi32" Alias "RasGetConnectStatusA" (ByVal rasconn As Long, connstatus As RASCONNSTATUS) As Long

Dim rasconn As Long


Private Sub Command1_Click()
Dim numbytes As Long, numconns As Long, success As Long
Dim rc As rasconn
rc.dwSize = Len(rc)
numbytes = Len(rc)
success = RasEnumConnections(rc, numbytes, numconns)
If numbytes > 0 Then
    ' Connect
    Print "Hanging up "; rc.szEntryName
    RasHangUp rc.hrasconn
Else
    '
    Print "not connected"
End If
End Sub

Private Sub Command2_Click()
    Dim status As Long
    Dim rdp As RASDIALPARAMS
    Dim rcs As RASCONNSTATUS
    Dim rasconn As Long
    rdp.dwSize = Len(rdp)
    rdp.szEntryName = "Swipnet" & Chr(0) ' select ras entry to use
    rdp.szPhoneNumber = Chr(0) ' use phone number from ras entry
    rdp.szCallbackNumber = Chr(0) ' use callback number from ras entry
    rdp.szUserName = "UserName" & Chr(0) ' user name for PPP logon
    rdp.szPassword = "Password" & Chr(0) ' password for PPP logon
    rdp.szDomain = "*" & Chr(0) ' use default domain
    status = RasDial(0, vbNullString, rdp, 0, 0, rasconn)
    If status = 0 Then
        rcs.dwSize = Len(rcs)
        status = RasGetConnectStatus(rasconn, rcs)
        If status <> 0 Then
            Debug.Print "RasGetConnectStatus error " & status
            RasHangUp rasconn
            Exit Sub
        Else
            If rcs.dwError Then
                Debug.Print "Connection failed, error " & rcs.dwError
                RasHangUp rasconn
                Exit Sub
            End If
            If rcs.rasconnstate = RASCS_Connected Then
                Debug.Print "Connected"
            Else
                RasHangUp rasconn
            End If
        End If
    Else
        Debug.Print "RasDial error " & status
        If rasconn Then RasHangUp rasconn
    End If
End Sub

Private Sub Command3_Click()
    RasHangUp rasconn
End Sub




Author

Commented:
Sure, thanks ! You can send them at asioli@geocities.com.

Thank you very much,

Paolo


Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.