?
Solved

Checking CD on serial port with API

Posted on 1997-07-13
6
Medium Priority
?
194 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
0
Comment
Question by:paolo070697
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 5

Expert Comment

by:y96andha
ID: 1428337
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?
0
 

Author Comment

by:paolo070697
ID: 1428338
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
0
 
LVL 5

Accepted Solution

by:
y96andha earned 400 total points
ID: 1428339

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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:paolo070697
ID: 1428340
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


0
 
LVL 5

Expert Comment

by:y96andha
ID: 1428341
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




0
 

Author Comment

by:paolo070697
ID: 1428342
Sure, thanks ! You can send them at asioli@geocities.com.

Thank you very much,

Paolo


0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question