LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to create logic based on received serial data in LabView?

Hi all!

 

I'm trying to manipulate a device through its serial port. I established a serial communication between LabView and the device. I can see the serial data coming out and can also send commands to the device. Now I'm trying to create a vi controller that would send a command as soon as certain data is read by LabView. For instance, I have the following data transmitted from the device:

 

I'm ok
I'm ok
I'm ok
I'm cold
I'm ok

As soon as the 'I'm cold' string is transmited, I want to send a comand (e.g. 'heating up'). The easiest way I can think of is to AND 'I'm cold' with the received output from the device, which would raise a flag as soon as 'I'm cold' appears. However, I can't figure out how to feed the serial output back to LabView. This is only an idea, of course, so I'm open to suggestions on how to go about this. The final result should involve no human interraction and only LabView should control the device. 

 

Regards,

Pavel

0 Kudos
Message 1 of 6
(3,091 Views)

Hello, Pavel.  You can simply compare the Serial String to the string "I'm Cold", something like the following:

Serial Logic.png

I've "hidden the details" of how to get a Serial String (actually, that's an empty sub-VI, but it certainly could be "the real thing") and how to Send a Warm Command inside two sub-VIs.  Notice that, by doing this, I make the logic of what's happening here much clearer than if there were lots of wires, lots of VISA functions, cluttering up the Block Diagram.  Also, if these were "real" sub-VIs, I could run them in isolation to specifically test that the code works.  And it makes such a neater, smaller Block Diagram ... [I'm something of a nut on sub-VIs, if you couldn't tell ...].

 

The "equality" comparison works if you know the exact string that you need.  If you worry about matching string case (i.e. allowing "I'm cold" or "I'm Cold"), you can use the "To Lower Case" function on the strings before the equality.  There are also String Functions (on the String Palette) that will let you do more sophisicated searches, should the need arise.

 

Bob Schor

0 Kudos
Message 2 of 6
(3,079 Views)
It sounds like you can simply wire the received string to a case statement or use a state machine and call a state based on what you read.
0 Kudos
Message 3 of 6
(3,067 Views)
Please note that a case statement can directly accept a string. It can also be set to ignore case.
0 Kudos
Message 4 of 6
(3,041 Views)

Thanks for the replies! 

 

Should I worry about line termination on the received serial messages or does LV detect a new line without explicitly marking a new line?

0 Kudos
Message 5 of 6
(2,877 Views)

@plaser wrote:

Thanks for the replies! 

 

Should I worry about line termination on the received serial messages or does LV detect a new line without explicitly marking a new line?


Yes you will need to deal with the termination. The VISA read will include the termination.

 

You can use "Trim Whitespace.vi" to remove it or you can include it in the compare

 

     "I'm Cold\n"

Omar
0 Kudos
Message 6 of 6
(2,866 Views)