03-24-2009 09:09 AM
I am developing a VISA VI to communicate with a serial device. I started by customizing the Basic Serial Read and Write.vi example to fit my own needs. My code is working, but before i add more complexity to the code I would like a critique of my work. Could the basic logic of this code be improved or could i run into problems further down the road? It's much easier to change my approach now.
Here are the basics of what the device understands...
send "L1" to turn device ON. Device sends "!" if understood.
send "L0" to turn device OFF. Device sends "!" if understood.
send "?" to request data. Device returns 8 lines with important values. Line 9 is a "!" to confirm transmission.
Besides these simple events, the code must also be able to handle an error alert from the device at any time. An error is sent from the device in the form "!S" or "!C".
My approach is to use an event structure inside of a while loop. If the L1/L0 command is not received through a button press event within 1 second, the code will timeout. After a timeout, the code will read the serial buffer. If bytes are found in the buffer, then these bytes are interpreted as an error alert and an error is read. If no bytes are in the buffer, the code will request data "?". This will update critical values on the screen for the user. Then the code returns to the event structure to await another possible button press.
I like the code run even slower (less than 1 loop/sec) because i do not need to update data quickly, but i would like the code to respond very quickly if an error is sent. Is there anyway to trigger an event based on new data appearing in buffer before the event timeout is reached? Thinking outside the box here, is there a better way to develop this code?
03-24-2009 09:41 AM - edited 03-24-2009 09:45 AM
I've got 2 things to help simplify your code.
You can use the data in the selector of a case structure directly. You don't need to branch the data outside and send it in a separate tunnel.
To the right of your VI, you have an unnecessary case structure. Just wire the result of the comparison directly to the local variable.
03-24-2009 10:36 AM
Thanks for the tip, much appreciated.
I'm also looking for critique in a more general sense. Are there better ways to execute this operation or is the event structure within the while loop the best approach? And assuming the event structure is the right way to go, are there better events to use? Is it sensible to send a command based on a button "value change" and is my use of the timeout event reasonable?
03-24-2009 10:50 AM
I believe your use of the event case along with the timeout case is perfectly acceptable. I'm trying to figure out if there are any traps in your code, such as getting stuck in a while loop waiting for a byte to come that never arrives. But I believe you've handled that as well.
I would continue with what you have and see how it behaves.