Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Serial Port Confusion

Status
Not open for further replies.

carlossan

Programmer
Dec 10, 2005
18
CA
Hello,

I have an application that shows the user the serial ports available on a PC and allows him to select several of them and configure their parameters (speed, data bits, parity). That is done on a Form different from the main form called frmSetup.

When the user is done selecting the serial ports and selecting their parameters, the application closes the frmSetup and goes to the main form where the user sends and receives information on the selected serial ports.

My confusion is where should I add the serial port objects (and also private ? public ?), so they can be seen by the 2 forms (frmMain and frmSetup).

I'm adding the serial ports by code, not by dragging the control from the toolbox because I don't know how many serial ports the user is going to choose, so I was wondering where to put the code that generates n serial port objects (n = number of serial ports choosen by the user).

BTW: I'm using Visual Studio 2005

Regards,

Carlos
 
Let's assume that each serial port is managed through an instance of a class named SerialPort (which you have to implement if you haven't yet).

You could then have a structure containing an array of SerialPort objects.

Let's name this structure SerialPortsCollection.

The SerialPort class would allow configuring a port's parameters, and also expose some properties like IsOpen, IsValid (i.e. the user wants to configure COM13 which isn't physically present on the desired machine), etc...

The SerialPortsConnection class could allow you to enumerate the valid ports, offer you access to a certain port based on port number, add a port, delete a port from the collection, etc...

And also, this collection cand be a singleton since you don't need two instances in the same appplication. Being a singleton will allow you to access the unique instance of the collection within any form using SerialPortsCollection.GetInstance() or whatever you want to call the member that will return the instance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top