title: VB.NET Serial Port reading HEX data and insert in the textbox
body: <p>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.</p>
  
  <p>Kindly help me I am new in VB.NET Programming.I need a vb.net code for this task:</p>
  
  <p>My code: </p>
  
  <pre><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
  </code></pre>
  
  <p>End Class</p>