📄 form1.vb
字号:
' objects to enable the user to open 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 btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
'Reset the state of the application objects
btnClose.Enabled = False
btnConnect.Enabled = True
lstCOMPorts.Enabled = True
'This section of code will try to close the COM port.
' Please note that it is important to use a try/catch
' statement when closing the COM port. If a USB virtual
' COM port is removed and the PC software tries to close
' 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
'Dispose the In and Out buffers;
SerialPort1.DiscardInBuffer()
SerialPort1.DiscardOutBuffer()
'Close the COM port
SerialPort1.Close()
'If there was an exeception then there isn't much we can
' do. The port is no longer available.
Catch ex As Exception
End Try
End Sub
'****************************************************************************
' Function:
' private void serialPort1_DataReceived( object sender,
' SerialDataReceivedEventArgs e)
'
' Summary:
' This function prints any data received on the COM port.
'
' Description:
' This function is called when the data is received on the COM port. This
' function attempts to write that data to the txtDataReceived textbox. If
' an exception occurs the btnClose_Click() function is called in order to
' close the COM port that caused the exception.
'
' Precondition:
' None
'
' Parameters:
' object sender - Sender of the event (this form)
' SerialDataReceivedEventArgs e - The event arguments
'
' Return Values
' None
'
' Remarks:
' None
'***************************************************************************/
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'The ReadExisting() function will read all of the data that
' is currently available in the COM port buffer. In this
' example we are sending all of the available COM port data
' to the SetText() function.
'
' NOTE: the <SerialPort>_DataReceived() function is launched
' in a seperate thread from the rest of the application. A
' delegate function is required in order to properly access
' any managed objects inside of the other thread. Since we
' will be writing to a textBox (a managed object) the delegate
' function is required. Please see the SetText() function for
' more information about delegate functions and how to use them.
Try
SetText(SerialPort1.ReadExisting())
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 SetText(string text)
'
' Summary:
' This function prints the input text to the txtDataReceived textbox.
'
' Description:
' This function prints the input text to the txtDataReceived textbox. If
' the calling thread is the same as the thread that owns the textbox, then
' the AppendText() method is called directly. If a thread other than the
' main thread calls this function, then an instance of the delegate function
' is created so that the function runs again in the main thread.
'
' Precondition:
' None
'
' Parameters:
' string text - Text that needs to be printed to the textbox
'
' Return Values
' None
'
' Remarks:
' None
'***************************************************************************/
Private Sub SetText(ByVal [text] As String)
'InvokeRequired required compares the thread ID of the
' calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true. We can
' use this attribute to determine if we can append text
' directly to the textbox or if we must launch an a delegate
' function instance to write to the textbox.
If txtDataReceived.InvokeRequired Then
'InvokeRequired returned TRUE meaning that this function
' was called from a thread different than the current
' thread. We must launch a deleage function.
'Create an instance of the SetTextCallback delegate and
' assign the delegate function to be this function. This
' effectively causes this same SetText() function to be
' called within the main thread instead of the second
' thread.
Dim d As New SetTextCallback(AddressOf SetText)
'Invoke the new delegate sending the same text to the
' delegate that was passed into this function from the
' other thread.
Invoke(d, New Object() {[text]})
Else
'If this function was called from the same thread that
' holds the required objects then just add the text.
txtDataReceived.AppendText(text)
End If
End Sub
'****************************************************************************
' Function:
' private void btnSendData_Click(object sender, EventArgs e)
'
' Summary:
' This function will attempt to send the contents of txtData over the COM port
'
' Description:
' This function is called when the btnSendData button is clicked. It will
' attempt to send the contents of txtData over the COM port. If the attempt
' is unsuccessful this function will call the btnClose_Click() in order to
' close the COM port that just failed.
'
' Precondition:
' None
'
' Parameters:
' object sender - Sender of the event (this form)
' EventArgs e - The event arguments
'
' Return Values
' None
'
' Remarks:
' None
'***************************************************************************/
Private Sub btnSendData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendData.Click
'This section of code will try to write to the COM port.
' Please note that it is important to use a try/catch
' statement when writing to the COM port. If a USB virtual
' COM port is removed and the PC software tries to write
' to 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
'Write the data in the text box to the open serial port
SerialPort1.Write(txtData.Text)
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
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -