简体   繁体   中英

Waiting for exact value from SERIAL PORT VB.net

I'm trying to wait microcontroller print "OK" to Serial Port, and VB.net check that "OK" and assign that value to string variable - check_OK. The first problem i faced - its data recevied event handler i just dont know how to make it check for exact value from serial port. I used Richtextbox1 to save ok from serial port then trying make its value = check_OK. I tried like this:

Public Class Form1
Dim check_OK As String
Dim OK_rs As Byte

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub
Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    ReceivedText(SerialPort1.ReadExisting())
End Sub
Private Sub ReceivedText(ByVal [text] As String)
    If Me.RichTextBox1.InvokeRequired Then
        Dim x As New SetTextCallback(AddressOf ReceivedText)
        Me.Invoke(x, New Object() {(text)})

    Else
        Me.RichTextBox1.Text = ""
        Me.RichTextBox1.Text &= [text]
        If RichTextBox1.Text = "O" Or RichTextBox1.Text = "K" Then
            check_OK = check_OK & RichTextBox1.Text
        End If
    End If
End Sub

These serial communication problems often benefit from being broken down into several parts. Since the microcontroller is the sender, the first thing I'd check is whether it's sending properly. To do this, connect to a terminal program first. This takes your VB code out of the picture.

Now, if the terminal program is getting the correct input from the microcontroller, you've just proved half of your system correct and you can then focus on the VB side. Divide and conquer. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM