vb.net程序如何实现向电脑的Com口发送一段字符串信息,之后再接收显示发送的字符串信息?
程序设计如下,在电脑的同一个com口上,使用SerialPort1控件,Button1发送数据,利用SerialPort1_DataReceived()事件来获取发送的信...
程序设计如下,在电脑的同一个com口上,使用SerialPort1控件,Button1发送数据,利用SerialPort1_DataReceived()事件来获取发送的信息,并显示在TextBox1中。 我自己写的程序,接收不到发送的字符信息,请各位大侠帮忙写一段能实现数据接收的源代码!
展开
展开全部
'vb.net的串口接收数据要用到委托
Delegate Sub SetTextCallback(ByVal InputString As String) '声明一个委派类,并声明符合函数参数有一个,而其型态是字符串
Private Sub ShowString(ByVal comData As String)
txt_Re.Text &= comData '将收到的数据入接收文字框中--- txt_Re.Text 是接收用的文本框
txt_Re.SelectionStart = txt_Re.Text.Length
txt_Re.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)
Invoke(d, inData)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '发送指令
SerialPort1.Write("123")
End Sub
Delegate Sub SetTextCallback(ByVal InputString As String) '声明一个委派类,并声明符合函数参数有一个,而其型态是字符串
Private Sub ShowString(ByVal comData As String)
txt_Re.Text &= comData '将收到的数据入接收文字框中--- txt_Re.Text 是接收用的文本框
txt_Re.SelectionStart = txt_Re.Text.Length
txt_Re.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)
Invoke(d, inData)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '发送指令
SerialPort1.Write("123")
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询