Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB.NET SerialPort used for multiple forms

Posted on 2014-09-29
6
Medium Priority
?
1,099 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 36

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 45

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 45

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 2000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

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…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

927 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