From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Looking for some advice on good practice in designing interfaces to devices over serial

Solved!
Go to solution

I am working on a project where I collect data from an Arduino over a USB port. I had no experience with LabVIEW at the start of this project, and while I've learned some of the basics of connecting to VISA resources, I'm getting beyond my understanding and I'd love some advice or general practice good design principles.

 

Currently I am organizing code into a sort of state machine setup. I initialize some VI properties and reset values to default, then go into a "searching for device" state. In this state I send a query message to the Arduino until the Arduino is fully booted up and can send an acknowledge message back. After that I go into my main loop, where I first request and receive data, process and store the data, then graph and display the data.

 

I want to make this system more resilient to interrupted connections with the Arduino. Currently if the Arduino gets disconnected while running, the only solution is to restart the program. However, I'm not sure what the best ways to deal with this are. How do I make sure the VISA connection gets properly closed and restarted, how do I actually use the error messages in a proper and useful way, etc. I know there's some bad practice going on in my current code, mainly because I'm trying to figure out how the LabVIEW error handling goes as I code stuff.

 

Any advice, specific or just general good practice stuff that you have, would be really appreciated!

0 Kudos
Message 1 of 3
(971 Views)
Solution
Accepted by topic author Tarnarmour

Although I cannot open your code cuz it appears to be in 2020, I can probably offer some advice.

 

One quick solution to your problem; think about how frequently you're polling your Arduino.  How is your communication stream set up?  Does LabVIEW ask the Arduino for data and the Arduino responds, or does LabVIEW sit and wait and wait and wait and wait for data from the Arduino.  Neither scenario is technically wrong.  They both have their merits based on situation.  What you can do is then think about your problem and how to best resolve it based on your communication stream.

 

If LabVIEW is asking for data and gets nor response after X amount of times, then you've probably lost connection.  You can then go through the process of clearing that VISA resource and establishing a new connection.

 

If LabVIEW is waiting for data, you can then address this multiple ways.

1) Arduino can send little "I'm alive" packets to LabVIEW every second or so if your data is slow.  Obviously, every time you send a data packet, that counts as "I'm alive." 

2) If your data is pretty fast, then you can program LabVIEW to wonder why there's been no data in X amount of time and do whatever it is you want to do about it.

----- These are essentially the same solution

0 Kudos
Message 2 of 3
(911 Views)

You are using a state machine (good for you) but...

 

  1. Learn how to use Type Def enumerated constants for your state machine
  2. Then you can give your states proper names (init, com, display, etc) not just numbers!
  3. I will attach my Simple State Machine Tutorial so you can see what I mean

smrCapture.PNG

 

========================
=== Engineer Ambiguously ===
========================
Message 3 of 3
(907 Views)