Link to home
Start Free TrialLog in
Avatar of paolo070697
paolo070697

asked on

Checking CD on serial port with API

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
Avatar of y96andha
y96andha

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?
Avatar of paolo070697

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of y96andha
y96andha

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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


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




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

Thank you very much,

Paolo