当前位置:编程学习 > C#/ASP.NET >>

如何用VB.NET2005的SerialPort控件进行串口接收数据?

用串口调试助手进行调试
1.我先用的DataReceived事件不能自动触发
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Close()

        SerialPort1.Open()
    End Sub
    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        MsgBox(SerialPort1.ReadLine)
    End Sub
  串口调试助手发送数据给我,收不到。
2.我加了个Button手动触发,但是还是接收不到,设置断点后就死在里面
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Do
                Dim Incoming As String = SerialPort1.ReadLine()
                If Incoming Is Nothing Then
                    Exit Do
                Else
                    TextBox1.Text = Incoming
                End If
            Loop
            MsgBox(Incoming)
        Catch ex As Exception
            MsgBox(ex)
        End Try
    End Sub
请教各位帮我解决下吧,谢谢~~ --------------------编程问答-------------------- 看看~~ --------------------编程问答-------------------- Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        If Me.ifreceive = False Then
            str = SerialPort1.ReadExisting()
            Me.demoThread = New Threading.Thread(New Threading.ThreadStart(AddressOf Me.ThreadProcSafe))
            Me.demoThread.Start()
 End If
 End Sub
 

Private Sub ThreadProcSafe()
        Me.SetText(str)
        str = ""
    End Sub
    Private Sub SetText(ByVal [text] As String)
        If Me.RichTextBox1.InvokeRequired Then
            Dim d As New SetTextCallback(AddressOf SetText)
            Me.Invoke(d, New Object() {[text]})
        Else
            RichTextBox1.Text = RichTextBox1.Text + [text] + " "
        End If
    End Sub --------------------编程问答-------------------- 好像是你的SerialPort1.ReadLine有问题吧

用ReadByte接收看看 --------------------编程问答-------------------- 我现在把ReadLine改成用ReadExisting
然后再用DataReceived事件触发就可以了
    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        MsgBox(SerialPort1.ReadExisting())
    End Sub
为什么用ReadLine就不能触发呢,很奇怪哦
--------------------编程问答-------------------- readexisting 读取输入缓冲区中所有立即可用字节
readline 要一直读到 NewLine 才行,才有返回值 --------------------编程问答-------------------- 不懂串口.我要做从串口收发数据,肯请各位帮忙 --------------------编程问答-------------------- 学习! --------------------编程问答-------------------- 收益了,刚好我也在做读串口数据!
--------------------编程问答-------------------- 我的缓冲区里有数据,可是为什么没运行DATARECEIVED事件叱? --------------------编程问答-------------------- 为什么在应用程序模板上调通的程序,放到ppc下就有问题了呢? --------------------编程问答-------------------- 两个串口确定都打开了? --------------------编程问答-------------------- 我是单片机跟PC通信,校验用MARK,进不了DATARECEIVED……并且以后用串口调试助手调试发现多收了15个字节…… --------------------编程问答-------------------- 学习了,刚好在学串口。 --------------------编程问答-------------------- LZ:参考我的资源"VB.NET2008的串口工程" --------------------编程问答-------------------- 谢谢````先下了```看看有没有用```` --------------------编程问答-------------------- 用时钟 控件不断的读数据 --------------------编程问答-------------------- Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim ReadCount As Integer
        ReadCount = Me.SerialPort1.BytesToRead
        If ReadCount <> 3 Then
            Exit Sub
        End If
        ReDim InByte(SerialPort1.BytesToRead - 1)
        Me.SerialPort1.Read(InByte, 0, SerialPort1.BytesToRead)
        Me.Invoke(New Exp(AddressOf CommandExplain))
    End Sub --------------------编程问答-------------------- 这个在我的设备上接收正常 --------------------编程问答-------------------- 参阅下:http://download.csdn.net/source/1070246的NET工程 --------------------编程问答-------------------- ReadLine
要求独到回车换行,如果你数据一直没回车换行。就不会结束,一直停在ReadLine方法调用处。因为DataReceived是监听线程调用的。所以不会影响你界面的主线程。现象就是DataReceived执行了一次后,就不再执行了。 --------------------编程问答--------------------
引用 19 楼 zdingyun 的回复:
参阅下:http://download.csdn.net/source/1070246的NET工程

晕,已在14楼回复过了. --------------------编程问答-------------------- 下载来学习学习,谢谢
补充:.NET技术 ,  VB.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,