以正确的方式关闭和打开串口

时间:2013-01-24 09:09:42

标签: vb.net serial-port port

我正在使用串口连接秤,并在地磅窗口应用程序中获取刻度读数。现在我编码的方式是当我点击按钮打开并开始接收读数时,它开始输出读数并且我使用此代码

Private Sub cmdOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOpen.Click
    comm.Parity = cboParity.Text
    comm.StopBits = cboStop.Text
    comm.DataBits = cboData.Text
    comm.BaudRate = cboBaud.Text
    comm.DisplayWindow = rtbDisplay
    comm.OpenPort()

    cmdOpen.Enabled = True
    cmdClose.Enabled = True
    cmdSend.Enabled = True

End Sub
Public Function OpenPort() As Boolean
    Try
        'first check if the port is already open
        'if its open then close it
        If comPort.IsOpen = True Then
            comPort.Close()
        End If

        'set the properties of our SerialPort Object
        comPort.BaudRate = Integer.Parse(_baudRate)
        'BaudRate
        comPort.DataBits = Integer.Parse(_dataBits)
        'DataBits
        comPort.StopBits = DirectCast([Enum].Parse(GetType(StopBits), _stopBits), StopBits)
        'StopBits
        comPort.Parity = DirectCast([Enum].Parse(GetType(Parity), _parity), Parity)
        'Parity
        comPort.PortName = _portName
        'PortName
        'now open the port
        comPort.Open()
        'display message
        _type = MessageType.Normal
        _msg = "Port opened at " + DateTime.Now + "" + Environment.NewLine + ""
        '_msg = Environment.NewLine
        DisplayData(_type, _msg)
        'return true
        Return True
    Catch ex As Exception
        DisplayData(MessageType.[Error], ex.Message)
        Return False
    End Try
End Function
Public Sub ClosePort()
    If comPort.IsOpen Then
        _msg = "Port closed at " + DateTime.Now + "" + Environment.NewLine + ""
        _type = MessageType.Normal
        DisplayData(_type, _msg)
        comPort.Close()
    End If
End Sub
  Private Sub comPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
    'determine the mode the user selected (binary/string)
    Select Case CurrentTransmissionType
        Case TransmissionType.Text
            'user chose string
            'read data waiting in the buffer
            Dim msg As String = comPort.ReadExisting()
            'display the data to the user
            _type = MessageType.Incoming
            _msg = msg
            DisplayData(MessageType.Incoming, msg + "" + Environment.NewLine + "")
            Exit Select
        Case TransmissionType.Hex
            'user chose binary
            'retrieve number of bytes in the buffer
            Dim bytes As Integer = comPort.BytesToRead
            'create a byte array to hold the awaiting data
            Dim comBuffer As Byte() = New Byte(bytes - 1) {}
            'read the data and store it
            comPort.Read(comBuffer, 0, bytes)
            'display the data to the user
            _type = MessageType.Incoming
            _msg = ByteToHex(comBuffer) + "" + Environment.NewLine + ""
            DisplayData(MessageType.Incoming, ByteToHex(comBuffer) + "" + Environment.NewLine + "")
            Exit Select
        Case Else
            'read data waiting in the buffer
            Dim str As String = comPort.ReadExisting()
            'display the data to the user
            _type = MessageType.Incoming
            _msg = str + "" + Environment.NewLine + ""
            DisplayData(MessageType.Incoming, str + "" + Environment.NewLine + "")
            Exit Select
    End Select
End Sub
Private Sub MonitorSP_DataReceived(ByVal sender As Object, ByVal e As IO.Ports.SerialDataReceivedEventArgs)
    Dim SP As IO.Ports.SerialPort = DirectCast(sender, IO.Ports.SerialPort)
    Dim Data As String = SP.ReadLine
    'Remember this is in a background thread so you can not update the UI from here without using Me.Invoke
End Sub

Private Sub CloseSP()
    For Each selSP As IO.Ports.SerialPort In MonitorSP
        selSP.Close()
    Next
    MonitorSP.Clear()
End Sub
 Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
    'Comm is an instance of the class that contains the methods I showed above like Datareceived and so forth
    comm.ClosePort()
    comm.ClosePort()
    'SetControlState()
    SetDefaults()
End Sub

现在,当我关闭端口并等待几分钟然后尝试再次重新打开端口时,我尝试关闭并再次重新打开端口时出现挑战,但是读数没有显示在规模上)。但当我关闭端口退出表格然后回到表格然后打开端口然后读数出现。我的问题是如何打开和关闭端口并再次打开它,而无需关闭表单或重新启动应用程序。

0 个答案:

没有答案