Showing results for 
Search instead for 
Did you mean: 

How can I make my program stop trying to visa read after I've sent my command?

Go to solution

I'm trying to adapt the continuous serial read right example to meet my simple needs but the problem I'm having is that after I've sent my command to the device and received every response the instrument has for that command, it keeps trying to read after that and eventually throws a timed out exception/code. I've linked the vi and have shown a picture of what the output of the instrument is.


So the command is log "status" and the machine spits out a permutation of measure 7 times before outputting the actual value because it sends back each character of what you typed to it (don't ask me why). Then I just strip out the number out of that and put it into the measurement indicator. After that it keeps trying to read and then eventually times out. How do I stop it from calling the Visa Read block once I get my measurement? Any way I can compare the response to "x =" and then stop reading once I get that and then allow it to input more commands?


Thanks, Sincerely, Jack.

0 Kudos
Message 1 of 9

The NI Serial examples are rather poor.  They show you some features of serial communication, but they are never really meant to be the basis of real world serial communication.


If you want to do just a write of a command and read of response whenever you tell it to, then move the Read stuff into the same case structure as the Write stuff.  Get rid of the Read button.  The write button should be changed to a Latch action if you only want it to write and read once per press.  Leave it switch action if you want it to write and read continually until you turn off the button.


You should probably get rid of the feedback node stuff on the response unless you truly desire to keep a long running history of everything you read back.

0 Kudos
Message 2 of 9

Yes that's what I want to do but I have to read back 8 responses because it gives me that many for the log status command. Do I use a for loop 8 times or what is the proper way?

0 Kudos
Message 3 of 9
Accepted by topic author crash_override

So 1 Write will give you 8 lines to Read?


Then yes, put the VISA Read in a For Loop with an 8 wired to it.


You could also leave the Read in a while loop (inside the Write's case structure), but have the while loop end if there is an error.

0 Kudos
Message 4 of 9

Any way to go back to just accepting commands input once it finishes getting the response?

0 Kudos
Message 5 of 9

What is what will happen with your outer while loop!

0 Kudos
Message 6 of 9
Accepted by topic author crash_override

Any way to go back to just accepting commands input once it finishes getting the response?


I am going to get yelled at, but, you can try the attached VI. You need to Disable any Termination Characters for Read, and use the dreaded "Bytes at Port" Function. This VI will read until the output stops; if your output never stops, then don't use this VI.



0 Kudos
Message 7 of 9
Accepted by topic author crash_override

@mcduff wrote:

I am going to get yelled at

Yes, you are.  A MUCH better approach here would be to just read lines in the loop.  If you expect 8 lines, then just read the 8 lines.  If unknown number of lines, then have a short-ish timeout and stop the loop when a read times out.


What I do in my applications is use independent loop that handles the serial port, using the Bytes At Port just to see if a message has started to come in.  Data is passed back and forth using queues.

There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 8 of 9
Accepted by topic author crash_override

I just solved it by making an input loop for an okay button, then setting it to only read enough responses for that one command. This will work for now but I will have to alter it later I think to accommodate some setup commands. Can just set it to compare the response string to something I expect and then stop the loop that way. I will give the timeout approach and the bytes at port function a shot as well. Thank you.


0 Kudos
Message 9 of 9