串行端口读取HEX数据并插入单独的文本框中(Book RFID Hex和用户RFID Hex) [英] Serial Port reading HEX data and insert in the separate textboxes (Book RFID Hex & User RFID Hex)

查看:39
本文介绍了串行端口读取HEX数据并插入单独的文本框中(Book RFID Hex和用户RFID Hex)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目GUI

[

在此图像中,我正在使用 UHF读卡器软件来测试RFID卡,因此文本框中的最终结果应该是RFID卡的唯一12字节十六进制十六进制值[

In this image I am using UHF reader's software for testing of RFID cards so my end result in the textboxes should be unique 12-byte HEX of RFID card [(image 2 preview)

基本上,我正在从事图书馆管理系统(LMS)项目和我正在尝试使用用户身份(HEX中的 用户RFID标签ID )和图书身份来发行图书( 十六进制中的Book RFID标签ID ).当用户进入图书馆发行书籍时,他/她将刷卡RFID卡,结果HEX中的用户ID(12字节)将立即出现在第一个文本框中,然后他/她将显示书籍给读者的是此人要发出的结果,十六进制的Book Tag ID(12字节)将立即发送到第二个文本框.(阅读器的输出为20个字节,如图片2 所示)

Basically, I am working on Library Management System(LMS) project & I am trying to issue books by using the User identity (User RFID Tag ID in HEX) and Book identity(Book RFID Tag ID in HEX). When user will come in the library for issue a book then he/she will swipe his/her RFID card as a result user ID (12-bytes)in HEX will appear in the first textbox instantly and after that he/she will show book to the reader that the person wants to issue as a result Book Tag Id (12-bytes) in HEX will send to the second textbox instantly. (Output of reader is 20 bytes as shown in image 2)

现在我面临的问题如下:

Now the problems I am facing are following:

1)我没有通过使用命令( _SerialPort1.ReadExisting())在单个文本框中接收到20个字节的十六进制,有些字节出现在第一个文本框中,其余字节出现在第二个文本框( SerialPort1.ReadLine )该命令正在读取,因此该命令不可用

1) I am not receiving 20 bytes of HEX in the single textbox by using the command (_SerialPort1.ReadExisting()) some of the bytes appear in first textbox and rest of the bytes appear in the second textbox, (SerialPort1.ReadLine) this command is reading noting so this command is not of my use

2)我面临的第二个问题是文本框中出现的某些字节已更改(更改),例如( CC FF FF 10 32 0D 01 E2 00 10 26 77 0D 01 51 23 3023 55 2D ),应该接收这20个字节,但实际上接收到的第一个字节为 3F ,第二个为 3F ,第三个为 3F ,第19位是 3F ,又另外一个字节,具有不同的十六进制值.

2) The second issue that I am facing is that some of the bytes that are appearing in textboxes are altered(changed) e.g (CC FF FF 10 32 0D 01 E2 00 10 26 77 0D 01 51 23 30 23 55 2D) these 20-bytes should be received but in actual first byte received is 3F, second is 3F,3rd is 3F and 19th is 3F and one more byte with different HEX value.

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

Kindly help me! My code is provided below:

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

推荐答案

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

I think you should read data as byte array and convert that byte array values to hex string.

您可以在以下

You can find reading example on this link. See DataReceived event. It is something like that.

' 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)

读取后,您可以像这样将字节值转换为十六进制字符串

After reading you can convert byte values to hex string like that

Dim hexString as String = ""
For Each item as Byte In bufferArr
    hexString &= item.ToString("X2")
Next

这篇关于串行端口读取HEX数据并插入单独的文本框中(Book RFID Hex和用户RFID Hex)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆