LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

acquiring data serially with a producer consumer loop

I am fairly new to LabVIEW and am working on a project where I need to continually read data from a serial port and process the data in nearly real time.  I have researched the producer consumer architecture, however I am not sure if it is feasible for what I am attempting to do.   The data collected from the serial port is in a 1D string array where I will search the string for the first occurrence of the packet type byte.  The packet type byte will be 0x00, 0x01, or 0x02, and after i find this byte i must check the following byte which contains the packet length to ensure that I am at the beginning of the message.  If this starting point is invalid, i must pass this index to another state where I begin searching for the packet type byte beginning at the new index.  After the valid starting point has been found i must parse the data and look at the next byte to continue parsing until I can no longer parse the message.  Whatever data is left at the end of the message that can not be parsed into a complete packet must then be concatenated to the beginning of the new data that will be read from the serial port.

Does anyone have any suggestions at the approach i should take and is the producer/consumer architecture the right choice?  I read about the queued state machine architecture, however some of it seems like it is unnecessary for this application. 

Should i be using a state machine since the data can be parsed into 3 different types of packets?

Thanks
Gary Still
0 Kudos
Message 1 of 10
(3,621 Views)
I think it would depend on how fast the data is coming, whether there is any handshaking and how much parsing you have to do.

The problem with the state machine structure is that if more information is sent while in the middle of processing previous data, the new data might be discarded. The producer/consumer will at least read the information as fast as the Producer loop can read it and put any extra in a queue.

Once again it depends on the hardware though. If there is some kind of handshaking, where the hardware will not send new data while old data is waiting to be read, and the parsing is a relativly small process, a state machine would probably work fine.
0 Kudos
Message 2 of 10
(3,605 Views)
The data is being read very quickly because it is actually from a USB port which acts as a COM port.  I am reading the data at a baud rate of 1,000,000 bits per seconds.  There is no handshaking, but it is critical that I do not lose any of the data. The data must be parsed, written to a file and displayed in a graph.
0 Kudos
Message 3 of 10
(3,593 Views)
hi
 can u post the vi. which u created
------------------------------------------------------------------------------------------------------------------
Indrajit

| indrajitbarve@gmail.com | indar_indar2005@yahoo.co.in .
0 Kudos
Message 4 of 10
(3,591 Views)
the attachment has two state machines which i realized is wrong.  I have a state machine in the producer loop at the top just to initialize variables and constants and a state where i wait for a pushbutton to write to the serial line before I begin reading from it.  After I enter the "enqueue state" i try to stay there.  The bottom loop is the consumer loop which I think should be in a state machine.  I did not read the article on expression flow until after I created this code, so I was not aware of using cluster data packets containing the state and data.  I planned on re working the code today after I decided which architecture to follow.
0 Kudos
Message 5 of 10
(3,585 Views)
hi
  i hope this example will help u...............
------------------------------------------------------------------------------------------------------------------
Indrajit

| indrajitbarve@gmail.com | indar_indar2005@yahoo.co.in .
Download All
0 Kudos
Message 6 of 10
(3,575 Views)
thank you very much.  I think this will get me in the right direction.  I still have a few questions regarding your example and this architecture in general.

1.  What is the purpose of the random number generator in the for loop which is bundled with the data that goes into the queue?
2.  If i need to first wait for a user to click a button on the GUI to write a message to the USB before i begin polling it, where do i want to put this?
3.  Will the producer loop continuously enqueue the data, while it is being parsed and graphed in the consumer loop, or does it have to wait for the consumer to finish before it enqueues another read from the serial port?

0 Kudos
Message 7 of 10
(3,568 Views)
hi
 1. actually the for loop random number is an example... where u want to read the datas from USB .
2. add a enum at first and give the name intialize. where u can do the GUI where user can write a message.
3.sorry i am little bit confusing with ur questions . i want  some  flow chart ...can u plz
------------------------------------------------------------------------------------------------------------------
Indrajit

| indrajitbarve@gmail.com | indar_indar2005@yahoo.co.in .
0 Kudos
Message 8 of 10
(3,560 Views)

The producer loop will continuously read data and enqueue it as long as there is something to read, and doesn't have to wait for the consumer loop.  As mentioned before, that is an advantage of the producer / consumer architecture over a state machine, as the state machine may miss data while in the processing state.

Hope this helps.

-Cory
0 Kudos
Message 9 of 10
(3,547 Views)
In the example shown to me above, there is a case structure which is true or false based on the dequeue element timing out.  This is in the consumer loop.  Can I create a state machine within this case structure for parsing my data, since it can be parsed into 3 different types of data packets?  I was thinking this may work as long as my last state exits the state machine each time it is finished so the outer while loop (consumer loop) can dequeue the next chunk of data.

A flowchart of what the state machine within the true/false case structure would look like is attached below.
0 Kudos
Message 10 of 10
(3,523 Views)