首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >串口编程

串口编程
EN

Stack Overflow用户
提问于 2010-08-14 15:10:07
回答 1查看 1.7K关注 0票数 4

我的要求是检测任何通信设备所连接的端口号

另外,我们可以如何添加包中的任何通信设备的驱动程序,可以安装与我的包安装

EN

回答 1

Stack Overflow用户

发布于 2010-08-15 05:09:56

代码语言:javascript
复制
Option Explicit
'******************************************************************************
' Description: Scans machine using the WIN32 API for all available comm hardware
' Usage:    main program calls the 'CommSettings' sub, passing the
'           name of the communications control and a combobox name.
'******************************************************************************

Dim CommCntrl             As Control                                  ' the communications control
Dim cmboPort              As Control                                  ' combobox to populate
Dim bNoComm               As Boolean

Private Const MAX_COMM = 16                                           ' 32 max port # to check

Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_FLAG_OVERLAPPED = &H40000000
Private Const INVALID_HANDLE_VALUE = -1

Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
                                    (ByVal lpFileName As String, _
                                     ByVal dwDesiredAccess As Long, _
                                     ByVal dwShareMode As Long, _
                                     ByVal lpSecurityAttributes As String, _
                                     ByVal dwCreationDisposition As Long, _
                                     ByVal dwFlagsAndAttributes As Long, _
                                     ByVal hTemplateFile As String) As Long

Private Declare Function CloseHandle Lib "kernel32" ( _
                                     ByVal hObject As Long) As Long


Public Sub GetPorts(serialCntrl As Control, comboBox As Control)
    '******************************************************************************
    ' Usage:    Pass the name of the communications control, a combo box, and the
    '           current com port setting in the calling routine.
    '******************************************************************************

    Dim iCntr             As Integer                                  ' loop counter
    Dim hRet              As Long                                     ' api return value
    Dim sCom              As String                                   ' comm port name

    On Error Resume Next

    Set cmboPort = comboBox

    Set CommCntrl = serialCntrl
    Err = 0

    cmboPort.Clear

    ' Close the port if it's open
    If CommCntrl.PortOpen = True Then
        CommCntrl.PortOpen = False
        DoEvents
    Else
        bNoComm = True
    End If

    ' Scan for all possible hardware so we can display all available ports
    ' in the combo box. Dynamically adjusts for PC's with addin cards
    For iCntr = 1 To MAX_COMM
        ' try to open the port.
        ' \\.\ required for ports > 9, works for all ports
        sCom = "\\.\Com" & CStr(iCntr) & vbNullChar
        hRet = CreateFile(sCom, GENERIC_READ Or _
                                GENERIC_WRITE, 0, vbNullString, OPEN_EXISTING, _
                          FILE_FLAG_OVERLAPPED, vbNullString)

        If hRet <> INVALID_HANDLE_VALUE Then
            hRet = CloseHandle(hRet)
            cmboPort.AddItem Format$(iCntr)
            Debug.Print iCntr
        Else
            ' dll error 5 = already open
            ' dll error 2 = no harware
            If Err.LastDllError = 5 Then
                cmboPort.AddItem Format$(iCntr) & " - Busy"
            End If
        End If
    Next

End Sub
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3482382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档