VB.net串口接收不到数据
PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBas...
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ports As String() = SerialPort.GetPortNames() Dim port As String
For Each port In ports
portnamebox.Items.Add(port)
Next port
baudratebox.Items.Add("9600")
SerialPort1.BaudRate = Val(baudratebox.Text)
SerialPort1.PortName = portnamebox.Text
SerialPort1.DataBits = 8
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.Parity = IO.Ports.Parity.None
End Sub
Private Sub connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles connect.Click
SerialPort1.Open()
Label8.Text = SerialPort1.IsOpen
If SerialPort1.IsOpen = True Then
statuslabel.Text = "串口已连接"
statuslabel.ForeColor = Color.Green
End If
End Sub
Private Sub senddata_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles senddata.Click
Dim strincoming As String
SerialPort1.Write(senddata.Text, 0, 4)
If SerialPort1.BytesToRead > 0 Then
Threading.Thread.Sleep(100) '添加延时
strincoming = SerialPort1.ReadExisting.ToString '读取缓冲区中的数据
SerialPort1.DiscardInBuffer()
RXbox.Text = strincoming
End If
End Sub
程序如上 senddata为发送数据,RXbox为接收数据框 ,串口已经打开,自己编的程序senddata输入1 接收RXbox里面都是空的 ,请大师指教 展开
Dim ports As String() = SerialPort.GetPortNames() Dim port As String
For Each port In ports
portnamebox.Items.Add(port)
Next port
baudratebox.Items.Add("9600")
SerialPort1.BaudRate = Val(baudratebox.Text)
SerialPort1.PortName = portnamebox.Text
SerialPort1.DataBits = 8
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.Parity = IO.Ports.Parity.None
End Sub
Private Sub connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles connect.Click
SerialPort1.Open()
Label8.Text = SerialPort1.IsOpen
If SerialPort1.IsOpen = True Then
statuslabel.Text = "串口已连接"
statuslabel.ForeColor = Color.Green
End If
End Sub
Private Sub senddata_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles senddata.Click
Dim strincoming As String
SerialPort1.Write(senddata.Text, 0, 4)
If SerialPort1.BytesToRead > 0 Then
Threading.Thread.Sleep(100) '添加延时
strincoming = SerialPort1.ReadExisting.ToString '读取缓冲区中的数据
SerialPort1.DiscardInBuffer()
RXbox.Text = strincoming
End If
End Sub
程序如上 senddata为发送数据,RXbox为接收数据框 ,串口已经打开,自己编的程序senddata输入1 接收RXbox里面都是空的 ,请大师指教 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询