例如我的计算机有效的串口是com3和com4,怎样才可以知道?

解决方案 »

  1.   

    Declare Function EnumPorts Lib "winspool.drv" Alias "EnumPortsA" (ByVal pName As String, ByVal Level As Long, ByVal lpbPorts As Long, ByVal cbBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long 
    说明 
    枚举一个系统可用的端口 
    返回值 
    Long,非零表示成功,零表示失败。会设置GetLastError 
    参数表 
    参数 类型及说明 
    pName String,指定服务器的名字。用vbNullString指定本地系统 
    Level Long,1或2(1用于NT 3.51),分别指定PORT_INFO_1 或 PORT_INFO_2 
    lpbPorts Long,包含PORT_INFO_1 或 PORT_INFO_2结构的缓冲区 
    cbBuf Long,lpbPorts缓冲区中的字符数量 
    pcbNeeded Long,指向一个Long型变量的指针,该变量用于保存请求的缓冲区长度,或者实际读入的字节数量 
    pcReturned Long,载入缓冲区的结构数量(用于那些能返回多个结构的函数) 
    注解 
    参考AddPort函数,了解进一步的情况
      

  2.   

    具体例子看
    http://www.mentalis.org/apilist/EnumPorts.shtml
      

  3.   

    comPorts(0)是一个Comm控件
    Private Sub FillPorts()
        On Error GoTo staErr
        Dim i As Long, fInvalid As Boolean
        For i = 1 To 256
            DoEvents
            With comPorts(0)
                .CommPort = i
                Me.Caption = APP_NAME + "---正在搜索可用串口...已完成" + CStr(Int(i / 256 * 100)) + "%"
                If Not .PortOpen = True Then .PortOpen = True
                If Not fInvalid Then
                    cboComPorts.AddItem "COM" + Trim(CStr(i))
                    cboComPorts.ItemData(i) = 1
                    Load comPorts(comPorts.Count)
                    comPorts(comPorts.Count + 1).CommPort = i
                End If
                .PortOpen = False
             End With
        Next
        With cboComPorts
            If .ListCount > 0 Then .Text = .List(0)
            glCurrentPort = 1
            Me.Caption = APP_NAME + "---共找到" + CStr(.ListCount) + "个" + "可用串口"
        End With
        Exit Sub
    staErr:
        If Err.Number = comPortInvalid Then
            fInvalid = True
        Else
            fInvalid = False
        End If
        Resume Next
    End Sub
      

  4.   

    试试Option ExplicitPrivate Declare Function EnumPorts Lib "winspool.drv" Alias "EnumPortsA" (ByVal pName As String, ByVal Level As Long, ByVal lpbPorts As Long, ByVal cbBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long
    Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
    Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)
    Private Declare Function HeapAlloc Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
    Private Declare Function GetProcessHeap Lib "kernel32" () As Long
    Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, lpMem As Any) As LongPrivate Type PORT_INFO_2
        pPortName As String
        pMonitorName As String
        pDescription As String
        fPortType As Long
        Reserved As Long
    End TypePrivate Type API_PORT_INFO_2
        pPortName As Long
        pMonitorName As Long
        pDescription As Long
        fPortType As Long
        Reserved As Long
    End TypeDim Ports(0 To 100) As PORT_INFO_2Function TrimStr(strName As String) As String
      
        Dim x As Integer    x = InStr(strName, vbNullChar)
        If x > 0 Then TrimStr = Left(strName, x - 1) Else TrimStr = strName
    End FunctionFunction LPSTRtoSTRING(ByVal lngPointer As Long) As String
        Dim lngLength As Long    lngLength = lstrlenW(lngPointer) * 2    LPSTRtoSTRING = String(lngLength, 0)    CopyMem ByVal StrPtr(LPSTRtoSTRING), ByVal lngPointer, lngLength    LPSTRtoSTRING = TrimStr(StrConv(LPSTRtoSTRING, vbUnicode))
    End FunctionFunction GetAvailablePorts(ServerName As String) As Long
        Dim ret As Long
        Dim PortsStruct(0 To 100) As API_PORT_INFO_2
        Dim pcbNeeded As Long
        Dim pcReturned As Long
        Dim TempBuff As Long
        Dim i As Integer    
        ret = EnumPorts(ServerName, 2, TempBuff, 0, pcbNeeded, pcReturned)
        
        TempBuff = HeapAlloc(GetProcessHeap(), 0, pcbNeeded)
        ret = EnumPorts(ServerName, 2, TempBuff, pcbNeeded, pcbNeeded, pcReturned)
        If ret Then
        
            CopyMem PortsStruct(0), ByVal TempBuff, pcbNeeded
            For i = 0 To pcReturned - 1
                Ports(i).pDescription = LPSTRtoSTRING(PortsStruct(i).pDescription)
                Ports(i).pPortName = LPSTRtoSTRING(PortsStruct(i).pPortName)
                Ports(i).pMonitorName = LPSTRtoSTRING(PortsStruct(i).pMonitorName)
                Ports(i).fPortType = PortsStruct(i).fPortType
            Next
        End If
        GetAvailablePorts = pcReturned    If TempBuff Then HeapFree GetProcessHeap(), 0, TempBuff
    End Function
     
    Private Sub Command1_Click()
        Dim NumPorts As Long
        Dim i As Integer    NumPorts = GetAvailablePorts("")
        List1.Clear
        For i = 0 To NumPorts - 1
            List1.AddItem Ports(i).pPortName
        Next
    End Sub
     
      

  5.   

    查注册表:
    我的电脑\HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\WINDOWS NT\CURRENTVERSION\PORTS