串行端口读取HEX数据并插入单独的文本框(Book RFID Hex& User RFID Hex)

时间:2017-09-28 15:09:26

标签: .net vb.net winforms serial-port rfid

项目的GUI

[(image 1)]

在此图片中,我使用 UHF阅读器软件来测试RFID卡,因此我在文本框中的最终结果应该是RFID卡的唯一12字节HEX [(image 2)] (image 2 preview)

基本上,我正在从事图书馆管理系统(LMS)项目和工作。我正在尝试使用用户身份 HEX中的用户RFID标记ID )和图书标识来发行图书( 在HEX中预订RFID标签ID )。当用户进入图书馆发行书籍时,他/她将刷他/她的RFID卡,因此HEX中的用户ID(12字节)将立即出现在第一个文本框中,之后他/她将显示书籍对于该人想要发布的读者,HEX中的书签ID(12字节)将立即发送到第二个文本框。 (阅读器输出为20字节,如图像2 所示)

现在我遇到的问题如下:

1)我没有在单个文本框中使用命令( _SerialPort1.ReadExisting())接收20个字节的HEX,其中一些字节出现在第一个文本框中,其余字节出现在第二个文本框,( SerialPort1.ReadLine )此命令正在读取注意,因此该命令不适用于我

2)我面临的第二个问题是文本框中出现的一些字节被更改(更改),例如( CC FF FF 10 32 0D 01 E2 00 10 26 77 0D 01 51 23 30 23 55 2D )应接收这20个字节但实际收到的第一个字节 3F ,第二个 3F ,第3个 3F 和19是 3F 另一个具有不同HEX值的字节

请帮助我!我的代码如下:

Imports System                      
Imports System.IO.Ports              
Imports System.Threading

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = IO.Ports.Parity.None
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.PortName = "Com7"

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        SerialPort1.Open()

    End Sub

    Private Shared buffer As String = ""

    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        Try

            'Dim rcv As String = SerialPort1.ReadLine
            'Dim rcv As String = SerialPort1.ReadByte
            Dim rcv As String = _SerialPort1.ReadExisting()  

            buffer = String.Concat(buffer, rcv)


            Dim hexVal As String = ""
            For Each c As Char In rcv
                hexVal = hexVal & AsciiCharToHexSring(c)
            Next

            If txtReceived1.Text = "" Then
                txtReceived1.Text = hexVal

            ElseIf txtReceived2.Text = "" Then
                txtReceived2.Text = hexVal

            Else
                txtReceived1.Text = ""
                txtReceived2.Text = ""
            End If

        Catch ex As Exception

        End Try

    End Sub

    Private Function AsciiCharToHexSring(s As Char) As String
        Dim hexdigit As String = Hex(Asc(s))
        If hexdigit.Length = 1 Then
            hexdigit = "0" & hexdigit
        End If
        Return hexdigit
    End Function


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        SerialPort1.Close()

        txtReceived1.Text = ""
        txtReceived2.Text = ""
    End Sub


End Class

1 个答案:

答案 0 :(得分:0)

我认为您应该将数据读取为字节数组,并将该字节数组值转换为十六进制字符串。

您可以在此link找到阅读示例。请参阅DataReceived事件。就是这样。

Sales_person_Id  Date  Visited_Houses  Not_Visited_Houses
       1       12/6/16     [1,2]          [3,4,5]
       1       13/6/16     [1,2,3]        [4,5]
       2       12/6/16     [1]            [2,3,4,5]
       2       13/6/16     [1,3]          [2,4,5]
       3       12/6/16     [3]            [1,2,4,5]

阅读后,您可以将字节值转换为十六进制字符串,如

' Initialize a buffer to hold the received data 
Dim bufferArr(serialPort.ReadBufferSize) as Byte 

' There is no accurate method for checking how many bytes are read 
' unless you check the return from the Read method 
Dim bytesRead as Integer = serialPort.Read(bufferArr, 0, bufferArr.Length)