I am trying to write a program to read and write to a radio scanner I have via the serial port. I have the writing down (It was easy enough) and I can read the data from the scanner using the datarecieved event. I can display the read data in a messagebox with no problem but when I try to write it to a TextBox I run into a cross Threading issue. I have been reading up on the BackGroundWorker but I am having trouble wraping my head around it. A couple of questions I have is

What should I do in the DoWork event?
Run the Datarevieved event
or
the actual write to textbox event

Also some of the syntax is confusing to me

Here is the code, any help is appreciated (This project is just for personal learning so any coments about the code is appreciated)

VB Code:
  1. Imports System.Text
  2. Imports System.IO.Ports
  3. Imports System.Threading
  4. Imports System.ComponentModel
  5.  
  6.  
  7. Public Class Main
  8.  
  9.     Dim WithEvents WorkingPort As New SerialPort("com1", 19200)
  10.  
  11.  
  12.     Private Function EnterCommand(ByVal Input As String) As String
  13.         'Add vbcr to the ebd of all commands because the scanner protocall requires it
  14.         Dim addVbcr As String = Input & vbCr
  15.         Return addVbcr
  16.     End Function
  17.  
  18.     Private Sub RecieveScannerData(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles WorkingPort.DataReceived
  19.         'Gets data sent by the scanner and displays it in a msgbox
  20.         ' Also calls the WriteScannerData Sub which does not work and is comented out
  21.         Dim ScannerInput As String
  22.         ScannerInput = WorkingPort.ReadExisting
  23.         MsgBox("data received: " & ScannerInput)
  24.         'WriteScannerData(ScannerInput)
  25.     End Sub
  26.  
  27.     Private Sub WriteScannerData(ByVal MyDebStr As String)
  28.         'Writes scanner data to the txtFeedback TextBox
  29.         MsgBox(" passed Value : " & MyDebStr)
  30.         txtFeedback.AppendText(MyDebStr)
  31.     End Sub
  32.  
  33.     Private Sub btnOpenPort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenPort.Click
  34.         'Opens serialport
  35.         If WorkingPort.IsOpen = False Then
  36.             WorkingPort.Open()
  37.             btnOpenPort.Enabled = False
  38.             btnClosePort.Enabled = True
  39.             btnEnterCommand.Enabled = True
  40.         Else
  41.             MessageBox.Show("Port is all ready open")
  42.         End If
  43.         lblPortStatus.Text = WorkingPort.IsOpen
  44.     End Sub
  45.  
  46.     Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  47.  
  48.         lblPortStatus.Text = WorkingPort.IsOpen
  49.         If WorkingPort.IsOpen = False Then
  50.             btnOpenPort.Enabled = True
  51.             btnClosePort.Enabled = False
  52.             btnEnterCommand.Enabled = False
  53.         Else
  54.             btnOpenPort.Enabled = False
  55.             btnClosePort.Enabled = True
  56.         End If
  57.     End Sub
  58.  
  59.     Private Sub btnClosePort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClosePort.Click
  60.         'Closes serialport
  61.         If WorkingPort.IsOpen = True Then
  62.             WorkingPort.Close()
  63.             btnClosePort.Enabled = False
  64.             btnOpenPort.Enabled = True
  65.             btnEnterCommand.Enabled = False
  66.         Else
  67.             MessageBox.Show("Port is all ready closed")
  68.         End If
  69.         lblPortStatus.Text = WorkingPort.IsOpen
  70.     End Sub
  71.  
  72.     Private Sub btnEnterCommand_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterCommand.Click
  73.         'Sends the command chosen from the combobox "cboCommand" the the scanner via the EnterCommand function
  74.         Dim CommandText As String = cboCommand.Text
  75.         If WorkingPort.IsOpen = False Then
  76.             MessageBox.Show("You need to open the port first")
  77.             Exit Sub
  78.         ElseIf CommandText.Length = 0 Then
  79.             MessageBox.Show("Please Choose a command to send first")
  80.         Else
  81.             WorkingPort.Write(EnterCommand(CommandText))
  82.  
  83.         End If
  84.     End Sub
  85. End Class