LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do you monitor a serial port without suspending the rest of the program?

Hi,

I'm trying to figure out a way to monitor a serial port for activity while still being able to use the rest of my program.  A simplified example would be to have a boolean button control a case structure that outputs a string to an indicator.  I want to be able to watch the serial port for an incoming string while still being able to push the boolean button and have that string appear in the indicator.

Thanks

0 Kudos
Message 1 of 6
(4,534 Views)

One way to do it is to use the VISA Bytes at Port function to poll to see if there is any activity on the serial port.  If the Bytes at Port > 0 then you've got something on your port and you can read it.  If not, then you can monitor your button.  You can do this numerous ways (have the Boolean press captured by an event structure while the timeout case monitors the Bytes at Port, monitor the Bytes at Port in a parallel loop and send a notifier to your main loop when you see something, etc).

 

Hope this helps!

 

jason

0 Kudos
Message 2 of 6
(4,525 Views)
Apparently I simplified my example a bit too much 😛   Here is a part of my bigger code.  It has a few buttons on it that send a command through the serial port, over a few wireless xbees to another computer which interprets the command.  These control a few breakers that we have in the lab.  Push a button to open or close.  Right now I have it set up in a cause/effect sort of way.  I send a command then I read the serial port for the response.  I need to be able to watch for a warning command sent to me, at any time, without me asking for it, telling me that a breaker has switched w/o me telling it to.  So I need to be able to monitor the serial port while still being able to use the rest of my program.
0 Kudos
Message 3 of 6
(4,519 Views)

I like using VISA events.

 

https://www.ni.com/docs/en-US/bundle/labview-api-ref/page/properties-and-methods/visa/visa-events.ht...

 

You can set up a loop that just monitors events from the serial port and reports back to your main loop.

 

Dan

Dan Shangraw, P.E.


   

0 Kudos
Message 4 of 6
(4,516 Views)

I didn't set up my main program in a loop.  I've tried playing around with a visa wait on event in a while loop, maybe it was the way I set up the while loop the wait on event was in, but it just sat in the loop and never left it, so I was not able to use the rest of my program.  That's what I did for my simple visaevents test.  I figured if I could push the button and make the string appear while it's watching the serial port, then my real program would work.  It didn't work for me

 

 

http://myfreefilehosting.com/f/dfcc6914bb_1.83MB

0 Kudos
Message 5 of 6
(4,511 Views)

At minimum, you would need to have a parallel thread in your program.  It typically takes form of parallel loops.

 

A more sophisticated coding approach would be to have a Dynamic VI (daemon) whose purpose would be to carry out serial communication.

Data could be sent to/from the main program using Action Engines.

 

R

0 Kudos
Message 6 of 6
(4,493 Views)