views:

107

answers:

5

hi

Is it correct to leave COM1 open all along the program ?

and close it only in exit from the program ?

thank's in advance

+2  A: 

It is quite common to do this, because there will be overhead associated with the open/close operation. You might end up confusing the OS opening and closing it too frequently.

So yes... open it, keep it open (unless there is an error), and close it when you are finished.

The only reason you would close it during the application, is to let other applications share the port.

Fuzz
A: 

If you do that other applications won't be able to use that port until you shutdown your app. That doesn't sound like a good idea, does it? I think you should open it, get/send the data and close it immediately after you are done with it.

RaYell
+1  A: 

So far i would say that Fuzz and RaYell are both right (depending on the concrete situation). So for my projects i usually provide a button on my form where you can connect and disconnect the serial port. So you're also able to provide a list of available com ports, where the user can select the desired port and afterwards press connect. So the user is able to decide in the concrete situation if it is needed to close the port or not.

Maybe for more convenience you can also save the last settings, provide some command line arguments, etc. for a better user experience.

Oliver
A: 

It depends on how the hardware attached to it works. When you open/close a port, a hardware signal can be triggered by the UART on one of the pins. I once had a device that would reset itself whenever the port was closed. So, YMMV.

sybreon
A: 

It depends on what your program does and how long it's going to run for. If you're communicating with a modem, for instance, you probably don't want other applications to interrupt your datastream, so in that case you'd keep it open the whole time and close it when finished.

If you're monitoring the status of some external hardware, and only need to check it periodically, it's better to open and close it every time you access it, so other programs can also access it if necessary.

odd parity