VB2005中按照16进制读取串口缓冲区数据

来源:互联网 发布:出国游经济好玩知乎 编辑:程序博客网 时间:2024/06/06 03:36
Function FtBytetoRead(ByVal strbuff As String, ByVal RS232 As IO.Ports.SerialPort)
        Dim i As Integer
        'Dim strbyte() As Byte
        Dim n As Integer
        Dim bReadByte() As Byte  '从串口读取的数据(字节)
        Dim Hexbytetoread() As String '转换后的读取数据


        Dim value As Boolean = False

        Control.CheckForIllegalCrossThreadCalls = value   

 '获取或设置一个值,该值指示是否捕获对错误线程的调用,这些调用访问控件的 System.Windows.Forms.Control.Handle 属性。 如果捕获了对错误线程的调用,则为 true;否则为 false。

        strbuff = ""
        i = RS232.BytesToRead '获取接收缓冲区中数据的字节数。
        ReDim bReadByte(i - 1)
        ReDim Hexbytetoread(i - 1)
        RS232.Read(bReadByte, 0, i)
        For n = 0 To i - 1
            Hexbytetoread(n) = Hex(bReadByte(n))
            If Len(Hexbytetoread(n)) = 1 Then
                Hexbytetoread(n) = "0" & Hexbytetoread(n)
            End If
            strbuff &= Hexbytetoread(n)
        Next
        Return strbuff
    End Function
原创粉丝点击