⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comportroutines.vb

📁 usb mass storage固件代码usbwiz_demo
💻 VB
字号:
option explicit on
option strict on

Friend Module ComPortRoutines

    Friend Structure ComPorts
        Friend PortName As String
        Friend BitRate As Integer
        Friend DataBits As Integer
        Friend StopBits As System.IO.Ports.StopBits
        Friend Parity As System.IO.Ports.Parity
    End Structure

    Friend MyMainForm As MainForm
    Friend MyPortSettings As PortSettings

    'Array of structures with information about the PC's COM ports:

    Friend ComPortsCount As Integer
    Friend myComPorts(15) As ComPorts

    Const ApplicationName As String = "COM Ports"

    Friend Sub FindComPorts()

        ' Purpose    : find the PC's COM ports and store parameters for each port.
        '              Use saved parameters if possible, otherwise use default values.  

        Dim ComPortIndex As Integer = 0
        Dim ComPortsArray(16) As String
        Dim ComPortName As String
        Dim ExistingComPorts(-1) As String
        
        'Find the COM ports and place the names in an array for sorting.
        ' The ports can change if a USB/serial converter is attached or removed,
        ' so this routine may run multiple times.

        ReDim myComPorts(15)
        ComPortsCount = 0

        For Each ComPortName In My.Computer.Ports.SerialPortNames

            'Store the names of all existing COM ports.
            ComPortsArray(ComPortsCount) = ComPortName
            ComPortsCount = ComPortsCount + 1
        Next

        'Remove unused items in the arrays.
        ReDim Preserve ComPortsArray(ComPortsCount - 1)
        ReDim Preserve myComPorts(ComPortsCount - 1)

        'Sort the names.
        Array.Sort(ComPortsArray)

        'Place the sorted names in the combobox.
        'Also store port data in the MyComPorts structure.
        For ComPortIndex = 0 To ComPortsCount - 1

            If ComPortsArray(ComPortIndex) <> "" Then

                'Store the port data in the MyComPorts array.
                'ComPortIndex corresponds to the index of the combobox.
                'ComPortIndex doesn't necessarily match the COM port number.
                myComPorts(ComPortIndex).PortName = ComPortsArray(ComPortIndex)
                myComPorts(ComPortIndex).BitRate = 9600
                myComPorts(ComPortIndex).DataBits = 8
                myComPorts(ComPortIndex).StopBits = IO.Ports.StopBits.One
                myComPorts(ComPortIndex).Parity = IO.Ports.Parity.None
            End If
        Next
    End Sub
End Module

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -