Click here to Skip to main content
15,908,254 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi guys, quick question, in dealing with IO.Ports.Serialport

I have this class,

VB
''' <summary>
''' Routines for finding and accessing COM ports.
''' </summary>

Public Class ComPorts
'....this class contains different properties and subs that handle the serial port communication. for example:
   Friend Shared Sub FindComPorts()
'.....omitted
    End Sub
Friend Function OpenComPort() As Boolean
'...omitted
End Function

'and such..
(this came from a serial communication example from lvr.com)
I'm using this class on my first form , like such


VB
 Public UserPort1 As ComPorts
'...
UserPort1.OpenComPort()


My question arises here: now that the port is open on the first form(UserPort1 is declared:Public UserPort1 As ComPorts on the first form), how do I keep the port open,when I close/hide the first form and give focus/show the second form??

---------------------------

UPDATE:
Thank you for the reply. I added a public module

VB
Module PublicSerialPortModuel
    Public UserPort1 As ComPorts

End Module

and now I can use UserPort1 in form2 without the "form1.xxxx" prefix.

I run into another problem though, which got me really nonplussed(well..confused)
I used a even handler on form1 to handle DataReceived event
VB
AddHandler UserPort1.SelectedPort.DataReceived, AddressOf DataReceivedHandler

and that sub is something like

VB
  Private Sub DataReceivedHandler(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
      COMportReadBuffer1 = UserPort1.SelectedPort.ReadExisting
      While UserPort1.SelectedPort.BytesToRead > 0
          COMportReadBuffer1 &= UserPort1.SelectedPort.ReadExisting
      End While
End Sub

which works all fine on the first form. But when I do the same thing on form2,

VB
AddHandler UserPort1.SelectedPort.DataReceived, AddressOf DataReceivedHandler2

when DataReceived event is triggered and I get into DataReceivedHandler2(), the read buffer is blank and its length is 0

Can you think of any possible reasons for this? I can't ..with UserPort1 now being defined public in the public module
Posted
Updated 24-Aug-12 9:39am
v3

1 solution

I dont think your question is comepletely clear. If you just want to have contact and use the class you would have to do that with delegates:
http://msdn.microsoft.com/en-us/magazine/cc301810.aspx[^]
http://msdn.microsoft.com/en-us/library/ms173171%28v=vs.100%29.aspx[^]

Or something on this site:
http://www.codeproject.com/search.aspx?q=delegate&x=0&y=0&sbo=kw&usfc=false[^]
 
Share this answer
 
Comments
SandiegoSSD 23-Aug-12 16:01pm    
Thx for the reply

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900