我正在使用以下代码从 com 端口读取值:
Private port As New SerialPort("COM13", 9600, Parity.None, 8, StopBits.One)
Private Sub port_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
Debug.Print(port.ReadExisting())
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler port.DataReceived, New SerialDataReceivedEventHandler(AddressOf port_DataReceived)
port.Open()
End Sub
这工作得很好,但时不时地它不会获取所有数据,并且返回结果是两个字符串而不是一个。
一个例子是,如果 com 端口通过单词“HELLO2YOU”发送它看起来像:
HEL
LO2YOU
或者
HELLO2
YOU
我如何在其中放置一个缓冲区,以确保在显示之前读取所有数据?
谢谢!