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

📄 form1.vb

📁 microchip PIC18,pic24系列单片机USB驱动的开发源码
💻 VB
📖 第 1 页 / 共 2 页
字号:
'Here are some useful articles when creating new PC applications for COM ports:
'(links valid as of June 3, 2008)
'
'"SerialPort Class"
'http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx
'
'"SerialPort Members"
'http://msdn.microsoft.com/en-us/library/system.io.ports.serialport_members.aspx
'
'"SerialPort.DataReceived Event"
'http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived.aspx
'
'"How to: Make Thread-Safe Calls to Windows Forms Controls"
'http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx
'
'"Visual Basic Developer Center"
'http://msdn.microsoft.com/en-us/vbasic/default.aspx?pull=/library/en-us/vbcon/html/vboriintroductiontovisualbasic70forvisualbasicveterans.asp

Imports System.IO.Ports

Public Class Form1

    'Create a delegate function for this thread that will take
    '  in a string and will write it to the txtDataReceived textbox
    Delegate Sub SetTextCallback(ByVal [text] As String)

    '****************************************************************************
    '   Function:
    '       private void UpdateCOMPortList()
    '
    '   Summary:
    '       This function updates the COM ports listbox.
    '
    '   Description:
    '       This function updates the COM ports listbox.  This function is launched 
    '       periodically based on its Interval attribute (set in the form editor under
    '       the properties window).
    '
    '   Precondition:
    '       None
    '
    '   Parameters:
    '       None
    '
    '   Return Values
    '       None
    '
    '   Remarks:
    '       None
    '***************************************************************************
    Private Sub UpdateCOMPortList()
        Dim s As String
        Dim i As Integer
        Dim foundDifference As Boolean

        i = 0
        foundDifference = False
        'If the number of COM ports is different than the last time we
        '  checked, then we know that the COM ports have changed and we
        '  don't need to verify each entry.

        If lstCOMPorts.Items.Count = SerialPort.GetPortNames().Length Then
            'Search the entire SerialPort object.  Look at COM port name
            '  returned and see if it already exists in the list.
            For Each s In SerialPort.GetPortNames()
                'If any of the names have changed then we need to update 
                '  the list
                If lstCOMPorts.Items(i).Equals(s) = False Then
                    foundDifference = True
                End If
                i = i + 1
            Next s
        Else
            foundDifference = True
        End If

        'If nothing has changed, exit the function.
        If foundDifference = False Then
            Exit Sub
        End If

        'If something has changed, then clear the list
        lstCOMPorts.Items.Clear()

        'Add all of the current COM ports to the list
        For Each s In SerialPort.GetPortNames()
            lstCOMPorts.Items.Add(s)
        Next s
        'Set the listbox to point to the first entry in the list
        lstCOMPorts.SelectedIndex = 0
    End Sub


    '****************************************************************************
    '   Function:
    '       private void timer1_Tick(object sender, EventArgs e)
    '
    '   Summary:
    '       This function updates the COM ports listbox.
    '
    '   Description:
    '       This function updates the COM ports listbox.  This function is launched 
    '       periodically based on its Interval attribute (set in the form editor under
    '       the properties window).
    '
    '   Precondition:
    '       None
    '
    '   Parameters:
    '       object sender     - Sender of the event (this form)
    '       EventArgs e       - The event arguments
    '
    '   Return Values
    '       None
    '
    '   Remarks:
    '       None
    '***************************************************************************/

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'Update the COM ports list so that we can detect
        '  new COM ports that have been added.
        UpdateCOMPortList()
    End Sub


    '****************************************************************************
    '   Function:
    '       private void btnConnect_Click(object sender, EventArgs e)
    '
    '   Summary:
    '       This function opens the COM port.
    '
    '   Description:
    '       This function opens the COM port.  This function is launched when the 
    '       btnConnect button is clicked.  In addition to opening the COM port, this 
    '       function will also change the Enable attribute of several of the form
    '       objects to disable the user from opening a new COM port.
    '
    '   Precondition:
    '       None
    '
    '   Parameters:
    '       object sender     - Sender of the event (this form)
    '       EventArgs e       - The event arguments
    '
    '   Return Values
    '       None
    '
    '   Remarks:
    '       None
    '***************************************************************************/
    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        'This section of code will try to open the COM port.
        '  Please note that it is important to use a try/catch
        '  statement when opening the COM port.  If a USB virtual
        '  COM port is removed and the PC software tries to open
        '  the COM port before it detects its removal then
        '  an exeception is thrown.  If the execption is not in a
        '  try/catch statement this could result in the application
        '  crashing.
        Try
            'Get the port name from the application list box.
            '  the PortName attribute is a string name of the COM
            '  port (e.g. - "COM1").
            SerialPort1.PortName = lstCOMPorts.Items(lstCOMPorts.SelectedIndex).ToString()

            'Open the COM port.
            SerialPort1.Open()

            'Change the state of the application objects
            btnConnect.Enabled = False
            lstCOMPorts.Enabled = False
            btnClose.Enabled = True

            'Clear the textbox and print that we are connected.
            txtDataReceived.Clear()
            txtDataReceived.AppendText("Connected." + vbCrLf)
        Catch ex As Exception
            'If there was an exception, then close the handle to 
            '  the device and assume that the device was removed
            btnClose_Click(Me, e)

        End Try
    End Sub


    '****************************************************************************
    '   Function:
    '       private void btnClose_Click(object sender, EventArgs e)
    '
    '   Summary:
    '       This function closes the COM port.
    '
    '   Description:
    '       This function closes the COM port.  This function is launched when the 
    '       btnClose button is clicked.  This function can also be called directly
    '       from other functions.  In addition to closing the COM port, this 
    '       function will also change the Enable attribute of several of the form

⌨️ 快捷键说明

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