Showing results for 
Search instead for 
Did you mean: 

auto select serial port

Hi all,
I'm currently use Instrument I/O Assistant to communicate thru serial port and have to hardcode program to read certain ports (COM1 & COM2). Can LV be programmed to detect available serial port automatically so I don't have change my code everytime I move from one PC to another.
0 Kudos
Message 1 of 10
I'm not sure exactly what you mean by auto select.  But don't hardcode the com ports.  Put in a VISA control so you can select which com ports to use.  It will auto populate on each PC depending on which com ports are available.
Message 2 of 10

Thank you for the replay. I guess the operator has to be involved in the selection. My desire is to have LV detect available com ports and select itself without operator involvement. Is it possible ?


0 Kudos
Message 3 of 10
You could use "VISA Find Resource" and query for available COM ports.  You could then try to communicate with those ports found with a known message and listen for the expected reply.  No reply, then try the next port.  You'll have to check for COM errors like ports already opened by another application, etc.
Message 4 of 10
That kind of "smarts" you have to program yourself because you can run into many different situations. For example, what if you move to a computer that has 2 serial ports? Or, what if the computer has a serial port and a plug-in serial port card? Or, what if the computer has no serial ports, but has one of those USB<->Serial boxes? If the device responds to some sort of *IDN? command then you can have an initialization routine that sends that out over each serial port. When your program gets a valid response then you know that's the port to use.
Message 5 of 10

3 stars??  I'd rather have you not rate the post at allSmiley Mad

In order to do an auto select, you will have to have a way to query the instrument and find out if it exists on that com port.

You can create a loop that concatenates "Com" with a number to create a string such as Com1, Com2, ... as high as you need to check and feed that string into the VISA resource.  Write your query, read the response.  If you get a valid response then you can stop the loop and you know what your Com port is and use that later in the program.  If you get an error or an invalid response, repeat the loop incrementing to the next Com port.  Create an upper limit of possible com ports (such as com8) so that if the device fails to respond (such as if it got disconnected), the loop won't run endlessly.  Have the loop stop and raise an error saying the device is not connected.

Message 6 of 10

Let me throw out another option that is less automatic.

Com1, Com2, etc. are aliases. If the program uses constants, when the pc is configured with serial ports, the aliases can be renamed to match what the program wants. For example, if you add new ports (pci, usb, whatever) and they show up in MAX as Com3 and Com4, you can rename them Com1 and Com2. You can even name them something more meaningful such as the instrument name they are connected to. Your program could have constants such as DMM, Counter, whatever. This approach will require a bit of setup up front but less coding would be required.

0 Kudos
Message 7 of 10

Thank you all,

These are all good ideas to try out. I'll try them out and let you know which one works best for my case!

Thanks again



0 Kudos
Message 8 of 10

How would you do that, I am trying to name my COM ports and would love to know how to use this method. 

thank you


0 Kudos
Message 9 of 10

This is a program I wrote a while ago that detects all COM ports that are in use, sends out a test message, and if it gets the correct response it will send out the VISA info of the COM port that worked.  It's a debug version so it's not the cleanest code, but it worked.

Amateur programmer for over 10 years!
0 Kudos
Message 10 of 10