?? comportroutines.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 + -