Serial port project form close not responding

谁都会走 提交于 2021-01-29 22:42:16

问题


I want to make serial port based project i have code like this .when i try to close my form it become not responding and hang my app please give suggestion and correct my mistakes in my code thank you in advance

Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports

Public Class frmMain
    Dim bw As BackgroundWorker
    Dim myPort As Array  'COM Ports detected on the system will be stored here
    Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data

    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        ReceivedText(SerialPort1.ReadLine())    'Automatically called every time a data is received at the serialPort
    End Sub

    Private Sub ReceivedText(ByVal [text] As String)
        'compares the ID of the creating Thread to the ID of the calling Thread
        If Me.Label1.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Me.BeginInvoke(x, New Object() {(text)})
        Else
            Me.Label1.Text = ([text] / My.Settings.deci)
        End If
    End Sub

    Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
      
    End Sub

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SerialPort1.PortName = My.Settings.comport          'Set SerialPort1 to the selected COM port at startup
        SerialPort1.BaudRate = My.Settings.baud
        'Set Baud rate to the selected value on 

        'Other Serial Port Property
        SerialPort1.Parity = IO.Ports.Parity.None
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.DataBits = 8            'Open our serial port
        SerialPort1.Open()
    End Sub

End Class

来源:https://stackoverflow.com/questions/64573461/serial-port-project-form-close-not-responding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!