Search code examples
vb.netwinformsrfid

VB.NET Serial Port reading HEX data and insert in the textbox


I want to read the data from UHF RFID reader that is connected on one of my com port, the data should be in HEX and must be a complete HEX that should be pasted in the textbox in my windows form application that I made in VB.NET.

Kindly help me I am new in VB.NET Programming.I need a vb.net code for this task:

My code:

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
   For Each s In System.IO.Ports.SerialPort.GetPortNames() 
        ComboBox1.Items.Add(s)
    Next s

End Sub


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If ComboBox1.SelectedIndex = -1 Then
        MessageBox.Show("Please select a port")
        Exit Sub
    Else
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = IO.Ports.Parity.None
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.PortName = ComboBox1.SelectedItem.ToString
        SerialPort1.Open()
    End If
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.ReadExisting()
        buffer = String.Concat(buffer, rcv)


        Dim hexVal As Integer
        hexVal = Convert.ToInt32(rcv, 16) '16 specifies the base

        txtReceived.Text = hexVal

    Catch ex As Exception
    End Try

End Sub

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

End Sub

End Class


Solution

  • I see your problem..

    the line

    hexVal = Convert.ToInt32(c, 16) '16 specifies the base
    

    is trying to convert a character to int32, but the function is assuming that the incoming value is already hexadecimal and you're getting a completely wrong answer.

    Incidentally, your data is 8 bit binary and your serial port reader reads each 8 bits as a char type.

    What you need is to use the VB built in function Hex, which takes an integer/byte (in this case the ascii code of each character) and returns a hex value as a string so

    Dim x As Integer = 15
    Dim s As String = hex(x)
    

    will assign "F" to the string

    All well and good. However, If you want a 2 digit hex string, you'll need to check if the returned string is only one character and if it is, add a "0" to the beginning.

    So, your lines

    Dim hexVal As Integer
    hexVal = Convert.ToInt32(rcv, 16) '16 specifies the base
    txtReceived.Text = hexVal
    

    should be replaced with ..

    Dim hexVal As string =""
    For Each c As Char In rcv
        hexVal = heval & AsciiCharToHexSring(c)
    Next
    txtReceived.Text = hexVal
    

    and add this function to convert the character and add a "0" if necessary ..

    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
    

    If you don't need a 2 digit hex number every time, just delete the If..End If block