Solved

VB.NET SerialPort used for multiple forms

Posted on 2014-09-29
6
902 Views
Last Modified: 2014-10-05
I have a program that needs to use a serial port in VB.NET 2013.
When the program starts I have a splash screen that loads and when the user clicks the "Proceed" button the splash screen is hidden (me.hide) and the user is presented with an "Options" form. The two options are "Run Test" and "System Check". Each of these launches a different form and both forms call the same module that communicate with hardware on the serial port.
In this module I need to refer to the serial port. What I need to know is, can I put the serial port (named "serialport1") on the splash screen and then in the module, when I need to call the serial port, simply say: frmSplash.serialport1 . . .
instead of putting separate serial ports on each form and then modifying the module to call a different serial port depending on which form is being used?

Thanks,
Charlie
0
Comment
Question by:charlieb01
6 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 40352344
Abstraction is your friend (OOP).

The two options are "Run Test" and "System Check". Each of these launches a different form and both forms call the same module that communicate with hardware on the serial port.

Just inject the serial port into your module. Thus each of your forms has a separate serial port, put the concrete used one is injected, e.g. by using the constructor, into your module.

The rest depends on your concrete code.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40352365
>>putting separate serial ports on each form

Are you talking about putting the serial port as a control onto the form (like you do for example a button) ?
0
 

Author Comment

by:charlieb01
ID: 40352596
Andy, Yes, I am talking about putting the serial port on the form. This is the way we always did it in VB6 days and the serial port is a windows form control.

Is there another way to do this?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40352799
>>can I put the serial port (named "serialport1") on the splash screen and then in the module, when I need to call the serial port, simply say: frmSplash.serialport1 . . .

Yes, that should work as you only hide the splash screen.  I assume the frmSplash is a reference to the splash screen that is open and hidden.
0
 
LVL 16

Accepted Solution

by:
hjgode earned 500 total points
ID: 40352808
Hello

instead of dropping the serialport control onto a form you can also create an instance of the serialport class.

But finally there is no difference.

To be able to access the serialport object from other forms/code, the referrer ( another form for example) needs a reference to the 'global' object. If you place the serialport on form A and want to access that from Form B or Module X, the serialport in Form A must be public  and the referrer needs a ref to Form A to be able to access the object. In example in Form B you need to write FormA.SerialPort1 to get the object.

Instead of placing the 'global' object into a Form it seems better to move it to a global module where all code can access this without a reference to a form. Using a module makes it also easier to maintain a single instance of this  SerialPort. It results in a better pattern and practice and you can easier avoid multiple instances for the same port (which will not work).
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question