LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

serial commands with a state machine

Solved!
Go to solution

Hi,

 

I am reading with VISA from a serial device. I write to the serial the start character and the device is sending some data.

Now after I read the data I want to stop the sending by writing to serial a stop character. The reading is in a separate loop.

 

I want to make a stop button that writes to serial and a start button that writes to serial and reads the data. Is there any way to do this without a state machine ?

 

 

Btw, is there any examples for a Serial reading and writing state machine ?

0 Kudos
Message 1 of 7
(5,105 Views)

You should use an event structure.

 

Felix 

0 Kudos
Message 2 of 7
(5,096 Views)

Can you help me with an example for such an event structure for serial commands ?

 

Thank you.

0 Kudos
Message 3 of 7
(5,079 Views)
Solution
Accepted by topic author John_Rif

This turned out to be a little more complicated than I originally thought.  So I wrote you a demo.  Just add your serial write and read code where indicated.

It is not a state machine.  It is more like a producer-consumer architecture.  The event loop produces start and stop commands.  The other loop executes according to the commands.  The trick was to get the serial read loop in such a manner that it could be stopped at any time, and restarted again at any time.

 

- tbob

Inventor of the WORM Global
Message 4 of 7
(5,042 Views)

Thank you.

 

I addapted the program and now the start and stop sends the right commands to the device, but I can't read the data on the other loop.

 

I debugged and the visa resource is not entering the reading loop, how can I pass VISA resource to the other loop ?

0 Kudos
Message 5 of 7
(5,016 Views)

I am using local variables to connect data between the two loops and it seems to work.

 

Thank you.

0 Kudos
Message 6 of 7
(4,989 Views)

Remove your first while loop.  You are initializing the serial port over and over again until you press OK.  You only need to initialize once.  Put all of the code before the loop.  The loop should just delay for a few mS until the OK is pressed.  You should always put a dely in a loop so as not to chew up 100% CPU time.  Even a 0mS delay will allow the CPU time to do other things.

 

In the second loop, since you have an event structure, you don't need a delay.  But you do need to wire in a value to the Timeout hourglass icon on the top left corner.  I typically use 100.  Again this is to prevent 100% CPU usage.

 

Do you understand data flow execution?  This means that a loop or block of code or a function will not execute until all of its inputs are present.  Your third loop will not start until you press the quit button because you have two wires going from the second loop to the third.  These wires will only pass their data to the third loop when the second loop is over (pressing the quit button).  When you press quit, the second loop terminates and data goes to the third loop.  Now all of the third loop's inputs are present and it can execute.  This is not what you want.  Delete the wires between second and third loop.  Then the third loop will execute from the beginning.  The serial read control will tell it when to read and when not to read.  You will need to pass the VISA Resource Name to the third loop by using a Local Variable.  From your statement, it sounds like you already did this.  Pass a no error condition into the third loop also.  You can make one or use a local variable of error in.

 

In the third loop, you have the VISA Close inside the loop.  This is not good.  The VISA session will close before you read.  Move this to after the third loop.  Then it will close only after the third loop stops.

 

After you delete the wires between loops 2 and 3, you will have to merge errors from the second and third loops to make sure you catch all errors.

 

In each one of your loops, replace the VISA Session terminals and Error terminals with shift registers.  This is good practice and is recommended by NI.  With terminals, if there is an error on one loop iteration, it will get lost because the next iteration takes in the original no error condition from the left side.  With a shift register, the error gets propogated to the next loop iteration.  If your loop executes zero times (For Loops can do this), the output of the VISA terminal will be a null value and the VISA close will cause an error.  With a shift register, even if the loop does not iterate at all, the VISA Session value on the input will be propogated to the output.

 

I think that is enough for now.  Please make these changes and re-post your vi.  You are making good progress.

 

- tbob

Inventor of the WORM Global
Message 7 of 7
(4,960 Views)