Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.NET SerialPort used for multiple forms

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

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

715 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