vb.net expert needed - serialport communication with multiple forms

h725

Active Member
Licensed User
Longtime User
I am looking for a vb.net expert who can help me with the following topic:
I have a serialport waiterlock which I have to access through several forms. I looped through the internet but I
did not get a satisfying answer.
This code does what it should do:


B4X:
Imports System.IO.Ports
Imports System.Text

Public Class Form1
  Private serialport As SerialPort
  Public waiterbuffer As String
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  serialport = New SerialPort

  With serialport
  .PortName = "COM2"
  .BaudRate = 9600
  .DataBits = 8
  .StopBits = CType(1, StopBits)
  .Parity = Parity.None
  .Handshake = IO.Ports.Handshake.None
  .RtsEnable = True
  .ReceivedBytesThreshold = 1
  .NewLine = vbCr
  .DiscardNull = False
  .Encoding = Encoding.Default
  End With
  AddHandler serialport.DataReceived, AddressOf Me.serialport_DataReceived
  serialport.Open()

  End Sub



  Private Sub serialport_DataReceived(sender As Object, e As SerialDataReceivedEventArgs)
  waiterbuffer = serialport.ReadLine

  Me.BeginInvoke(New EventHandler(Of SerialDataReceivedEventArgs)(AddressOf txtreceived), New Object() {sender, e})

  End Sub
  Private Sub txtreceived(sender As Object, e As EventArgs)

  txt_debug.AppendText("ID: " & waiterbuffer & vbCrLf)

  End Sub
End Class

Now I have the following problem: I want to access the serialport from other forms. I have tried to use the serialport as a global variable and work with another addhandler on another form. But this does not work.
I obviously need a class or a module with the serialport.datareceived event which I can access from all forms.
If you can help me please make an offer.
 
Last edited:

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
I have tried to use the serialport as a global variable and work with another addhandler on another form. But this does not work.
Can you be more specific? What exactly doesn't work? Did you remove the handler from the first form before adding it to the second?

IMHO, a better solution would be to make the serial comm class self-contained, have it monitor and queue its events internally. That way you can interrogate the class and its status (as well as any queued messages) from any form at any time.
 

h725

Active Member
Licensed User
Longtime User
Yes I tried the following:
Form1 activated / load - addhandler of the datareceive event
Form1 deactivated / closing - removehandler of the datareceive event
Form2 activated / load - addhandler of the datareceive event
Form2 deactivated / closing - removehandler of the datareceive event

When Form2 was closed already I got an error in the datareceive event of Form2 at begininvoke(...) saying: Invoke or BeginInvoke cannot be called on a control until the window handle has been created when data should actually show up at Form1 again.

I am looking for someone who can offer me a sample with a class. That would be useful.
 
Last edited:

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
In .NET, your Form1 will continue to run in the background while the user is interacting with form2 if you use "me.hide" instead of "me.close" after you show form2. Is there any reason you need the serial communications to happen in form2, our could form1 just call public methods in form2 as needed?
 
Top