Results 1 to 8 of 8

Thread: [RESOLVED] Need help with API Communication Port State reading

Threaded View

  1. #3
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    6,167

    Re: Need help with API Communication Port State reading

    Here is a fast EnumSerialPorts implementation:

    thinBasic Code:
    1. Option Explicit
    2.  
    3. Private Declare Function QueryDosDevice Lib "kernel32" Alias "QueryDosDeviceA" (ByVal lpDeviceName As Long, ByVal lpTargetPath As String, ByVal ucchMax As Long) As Long
    4.  
    5. Public Function EnumSerialPorts() As Variant
    6.     Dim sBuffer         As String
    7.     Dim lIdx            As Long
    8.     Dim vRetVal         As Variant
    9.     Dim lCount          As Long
    10.    
    11.     ReDim vRetVal(0 To 255) As Variant
    12.     sBuffer = String$(100000, 1)
    13.     Call QueryDosDevice(0, sBuffer, Len(sBuffer))
    14.     sBuffer = vbNullChar & sBuffer
    15.     For lIdx = 1 To 255
    16.         If InStr(1, sBuffer, vbNullChar & "COM" & lIdx & vbNullChar, vbTextCompare) > 0 Then
    17.             vRetVal(lCount) = "COM" & lIdx
    18.             lCount = lCount + 1
    19.         End If
    20.     Next
    21.     If lCount = 0 Then
    22.         vRetVal = Array()
    23.     Else
    24.         ReDim Preserve vRetVal(0 To lCount - 1) As Variant
    25.     End If
    26.     EnumSerialPorts = vRetVal
    27. End Function
    28.  
    29. Private Sub Form_Load()
    30.     Dim vElem           As Variant
    31.    
    32.     For Each vElem In EnumSerialPorts
    33.         Debug.Print vElem
    34.     Next
    35. End Sub
    For new USB serial ports arrival you can use RegisterDeviceNotification to get WM_DEVICECHANGE message like this:

    thinBasic Code:
    1. Option Explicit
    2.  
    3. '--- Windows Messages
    4. Private Const WM_DEVICECHANGE               As Long = &H219
    5. '--- for RegisterDeviceNotification
    6. Private Const DEVICE_NOTIFY_WINDOW_HANDLE   As Long = &H0
    7. Private Const DBT_DEVTYP_DEVICEINTERFACE    As Long = &H5
    8. Private Const DBT_DEVICEARRIVAL             As Long = &H8000&
    9. Private Const DBT_DEVICEREMOVECOMPLETE      As Long = &H8004&
    10. Private Const GUID_DEVINTERFACE_USB_DEVICE  As String = "{A5DCBF10-6530-11D2-901F-00C04FB951ED}"
    11.  
    12. Private Declare Function RegisterDeviceNotification Lib "user32" Alias "RegisterDeviceNotificationA" (ByVal hRecipient As Long, ByRef NotificationFilter As Any, ByVal Flags As Long) As Long
    13. Private Declare Function UnregisterDeviceNotification Lib "user32" (ByVal Handle As Long) As Long
    14. Private Declare Function CLSIDFromString Lib "ole32" (ByVal lpsz As Long, pclsid As Any) As Long
    15.  
    16. Private Type DEV_BROADCAST_DEVICEINTERFACE
    17.     dbcc_size           As Long
    18.     dbcc_devicetype     As Long
    19.     dbcc_reserved       As Long
    20.     dbcc_classguid(0 To 3) As Long
    21.     dbcc_name           As Long
    22. End Type
    23.  
    24. Private m_hDevNotify                As Long
    25. Private m_pSubclass                 As IUnknown
    26.  
    27. Private Property Get pvAddressOfSubclassProc() As Form1
    28.     Set pvAddressOfSubclassProc = InitAddressOfMethod(Me, 5)
    29. End Property
    30.  
    31. Private Sub Form_Load()
    32.     Dim uFilter         As DEV_BROADCAST_DEVICEINTERFACE
    33.  
    34.     '--- on device insert/eject notify w/ WM_DEVICECHANGE
    35.     uFilter.dbcc_size = Len(uFilter)
    36.     uFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE
    37.     Call CLSIDFromString(StrPtr(GUID_DEVINTERFACE_USB_DEVICE), uFilter.dbcc_classguid(0))
    38.     m_hDevNotify = RegisterDeviceNotification(hWnd, uFilter, DEVICE_NOTIFY_WINDOW_HANDLE)
    39.     Set m_pSubclass = InitSubclassingThunk(hWnd, Me, pvAddressOfSubclassProc.SubclassProc(0, 0, 0, 0, 0))
    40. End Sub
    41.  
    42. Private Sub Form_Unload(Cancel As Integer)
    43.     If m_hDevNotify <> 0 Then
    44.         Call UnregisterDeviceNotification(m_hDevNotify)
    45.         m_hDevNotify = 0
    46.     End If
    47. End Sub
    48.  
    49. Public Function SubclassProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long, Handled As Boolean) As Long
    50.     Select Case wMsg
    51.     Case WM_DEVICECHANGE
    52.         Select Case wParam
    53.         Case DBT_DEVICEARRIVAL, DBT_DEVICEREMOVECOMPLETE
    54.             Debug.Print "wParam=&H" & Hex(wParam), Timer
    55.         End Select
    56.         Handled = True
    57.     End Select
    58. End Function
    This uses the Modern Subclassing Thunk for the IDE-safe subclassing.

    For serial ports overlapped I/O you can check out this cSerialPortConnector class test project.

    cheers,
    </wqw>

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width