VB.Net SerialPort的DataReceived不触发
PrivateSubReceiveData(senderAsObject,eAsEventArgs)HandlesSerialPort1.DataReceivedText...
Private Sub ReceiveData(sender As Object, e As EventArgs) Handles SerialPort1.DataReceived
TextBox1.Text = TextBox1.Text & SerialPort1.ReadExisting()
End Sub
读写缓存均为1字节,为何该事件不触发,串口可以肯定接收到数据了,因为将上一句放到发送函数的后面紧跟着就可以正常在TextBox里显示数据了(直接将串口的收发引脚接一块了,所以发送完也就接收完了)
Delegate Sub SetTextCallBack(ByVal Input As String)
Private Sub ShowString(ByVal InputData As String)
TextBox1.Text += InputData
TextBox1.SelectionStart = TextBox1.Text.Length
TextBox1.ScrollToCaret()
End Sub
Private Sub ReceiveData(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim InData As String = SerialPort1.ReadExisting
Dim d As New SetTextCallBack(AddressOf ShowString)
BeginInvoke(d, InData) End Sub 展开
TextBox1.Text = TextBox1.Text & SerialPort1.ReadExisting()
End Sub
读写缓存均为1字节,为何该事件不触发,串口可以肯定接收到数据了,因为将上一句放到发送函数的后面紧跟着就可以正常在TextBox里显示数据了(直接将串口的收发引脚接一块了,所以发送完也就接收完了)
Delegate Sub SetTextCallBack(ByVal Input As String)
Private Sub ShowString(ByVal InputData As String)
TextBox1.Text += InputData
TextBox1.SelectionStart = TextBox1.Text.Length
TextBox1.ScrollToCaret()
End Sub
Private Sub ReceiveData(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim InData As String = SerialPort1.ReadExisting
Dim d As New SetTextCallBack(AddressOf ShowString)
BeginInvoke(d, InData) End Sub 展开
2个回答
展开全部
要用委托才行
Delegate Sub SetTextCallback(ByVal InputString As String)
Private Sub ShowString(ByVal comData As String)
txt_Rect.Text += comData '将收到的数据入接收文字框中
txt_Rect.SelectionStart = txt_Rect.Text.Length
txt_Rect.ScrollToCaret()
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim inData As String = SerialPort1.ReadExisting
Dim d As New SetTextCallback(AddressOf ShowString)
BeginInvoke(d, inData)
End Sub
更多追问追答
追问
是这样吗?还是不触发呀
追答
会触发的,在按钮下写条发送指令 SerialPort1.Write("123456" & vbCr)
'就能看到txt_Rect文本框中自动返回"123456",前提是2,3脚短接
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询