LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Open and close multiple serial ports without closing the main program

Solved!
Go to solution

Hello, I have looked at all the NI examples on serial ports and a lot of other forum posts; I can't find anyone that has written a program where the serial port is not opened when the program starts, then within the program are able to open and close the port however many times they want without closing the main program.  The program I'm creating will be compiled to an EXE and ran in the field and needs to talk to multiple sensors that plug in via usb and operate as virtual serial ports.  I would like the user to be able to run the program, then choose serial ports based on what is available in windows and open them one by one.  I have figured out how to open the program then open the serial port, but I can't quite figure out how to close it, then re-open it.  I have something like this: 

 

I've seen so many different ways people do serial ports in LabVIEW that my brain hurts.  I know this code could be cleaner and I will clean it up after I figure out this open/close goal.  If anyone wants to recommend a completely different way to accomplish(structure wise)... go for it; I'll try and re-post.

0 Kudos
Message 1 of 5
(3,508 Views)

Hello,

 

In the inner loop you are not closing using visa close. Maybe tat is the reason its not closing. Are you sure that com ports are changing in the I/O VISA  Address control?

 

Thanks & Regards,
Bharath Kumar
GCentral
0 Kudos
Message 2 of 5
(3,486 Views)

Look into the Queued Message Handler.  The idea being that you have a loop that does nothing but handle the communications with a single device.  You can send it messages via a queue to do whatever (open, close, query, etc.)  Then your main loop can just be a simple loop with an Event Structure in it that does nothing but send messages around based on user inputs.  For display purposes, I like to use User Events for having your serial port loop send updates to the GUI event loop.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 3 of 5
(3,481 Views)

OK,, I am definitely going to look into Queued Message Handler as I'm seeing that in other more complicated examples like the Modbus Master/Slave examples,, but for now I got this working like I want for opening/closing the serial port.  It works great, thank god for being able to re-call the VISA serial even after the port is open,  and the VISA close even after the port is closed.  Now my only problem is handling the read timeout error.  All work great unless I don't get a response from the arduino.  I want to be able to trap this error and display a "No Response" indicator to the user.  I've seen examples with switches, cases, but this is the simplest I've seen and I want it to work so bad.  Can anyone tell me if this should work like I suspect:

 

Serial Example Forum v2.png

0 Kudos
Message 4 of 5
(3,407 Views)
Solution
Accepted by topic author ZSC100

Ok I figured out that I had the error code to match in the wrong input, I needed to use "exception code",  I feel stupid now.  I'm able to display the timeout by using the simple error handler vi with no dialog specified.  Then I clear that error only after the flat sequence with the general error handler so I can get any other errors that might arise.  I'm amazed every day at how many ways there are to accomplish any task.  I'll leave this here as my final post b/c it's working as I intended now.

 

mspaint_2019-04-18_18-43-29.png

0 Kudos
Message 5 of 5
(3,390 Views)