[英]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.