0

シリアル ポートを使用して体重計に接続し、weedbridge Windows アプリで体重計の読み取り値を取得しています。ボタンをクリックして読み取り値を開き、読み取りを開始すると、読み取り値が適切に出力され、このコードを使用するようにコーディングしました

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

ポートを閉じてから再度開いてみると、ポートを閉じて数分間待ってから再度ポートを開き直そうとすると、チャレンジが表示されますが、機能しません (測定値がスケールに表示されません)。 )。しかし、ポートを閉じてフォームを終了し、フォームに戻ってからポートを開くと、測定値が表示されます。私の質問は、フォームを閉じたりアプリを再起動したりせずに、ポートを開閉して再度開くにはどうすればよいかということです。

4

0 に答える 0