Serial Port reading HEX data and insert in the separate textboxes (Book RFID Hex & User RFID Hex)

我的未来我决定 提交于 2019-12-24 16:42:42

问题


GUI of Project

[

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)

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

回答1:


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


来源:https://stackoverflow.com/questions/46472571/serial-port-reading-hex-data-and-insert-in-the-separate-textboxes-book-rfid-hex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!