簡體   English   中英

在VB.net中使用“ IF”語句檢查串行端口接收到哪些數據

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

我能夠從串行端口接收數據,但是我想檢查接收到的數據。

這是代碼:

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

因此,我使用“ ReadExisting()”功能接收數據。 它允許我接收數據,但不能讓我檢查接收到了什么數據。

“我想檢查接收到的數據,如果接收到的數據與'字符-a'相同,則必須執行我想要的任務。”

提前致謝。 :)

如果您確實必須通過輪詢接收緩沖區來實現它,這是我建議的代碼來解決您的問題:

 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

概念是,檢查接收緩沖區中是否存在任何內容,如果存在,則讀出一個字符。 如果out char有效,請執行您自己的操作。

順便說一句 ,如果在串行端口上什么都沒收到,那么使用ByteToRead進行預檢查可以防止在調用ReadChar時阻塞計時器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM