简体   繁体   中英

Check which data is received by serial port with “IF” statement in VB.net

I am able to receive data from serial port, but i want to check which data is received.

Here is the code :

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
   If int = 0 Then
     If SerialPort1.ReadExisting() = "a" Then
       'Do Something i want
     End If
   End If

So, i used "ReadExisting()" funtion for receiving data. It allows me receive data but, not let me check what data is received.

"I want to check the received data and if data is received is same as as 'Character - a' then have to do the task i want."

Thanks in Advanced. :)

If you do really have to achieve it by polling receiving buffer , here's my suggested code to solve your question:

 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
     If SerialPort1.BytesToRead > 0 Then
        'once anything have been received and stored in inner buffer.
         If SerialPort1.ReadChar() = "a" Then
               'Do Something you want
         End If
     End If
   End Sub

The concept is , check if there's anything existed in receiving buffer , if it was , read one char out. If the out char is valid , do your own actions.

By the way , using ByteToRead to do pre-check could prevent your timer be blocked when calling ReadChar , if nothing be received on serial port.

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