Solved

VB.NET SerialPort used for multiple forms

Posted on 2014-09-29
6
952 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 34

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 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