LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Intellegent Wait for Serial Communication

Hello,

 

I'm communicating with a a serial port through VISA, as my attached VI shows. My problem comes with the wait command I have to use between the write and read functions. The device I'm talking too is a little slow, so I'm using a static wait function. However I was wondering is there something i can do to make the wait function more intellegent, basically I want it to start reading when it detects information in the serial buffer for it, instead of waiting 1.4 seconds and hoping something is there.

 

Thanks in advance.

0 Kudos
Message 1 of 8
(4,825 Views)

Put your "Bytes at port" property node in a while loop and break out of the loop when you detect a number >0 (or you could wait until you have the number of bytes you are expecting). 

0 Kudos
Message 2 of 8
(4,802 Views)
I can't look at the VI right now but if you are reading a string with a termination character, you just have the read and no wait at all. Your timeout has to be greater than the max time the instrument will take.
0 Kudos
Message 3 of 8
(4,798 Views)

@BFeigum wrote:

Put your "Bytes at port" property node in a while loop and break out of the loop when you detect a number >0 (or you could wait until you have the number of bytes you are expecting). 


I like the while loop suggestion, I'll try that. However I will need to kick out eventually if I dont get anything back. Is there a way to timeout, or should I just or the termination with a comparison against a high number and itterations, or is there a smarter way of doing this.? The number of bytes I'm expecting could be problamatic because it has the possability of being different each time.

 


@Dennis_Knutson wrote:
I can't look at the VI right now but if you are reading a string with a termination character, you just have the read and no wait at all. Your timeout has to be greater than the max time the instrument will take.

 I dont have any termination characters coming from the device so that wont work sadly. How do I change the timeout feature?

0 Kudos
Message 4 of 8
(4,789 Views)

Receiving serial communication in not trivial.

 

Waiting for a character with VISA will usually block that thread. So wait with little timeout in a while loop that has a WAIT/DELAY in itself.

Collect what you get and return when your done.

 

Gabi

 

 

7.1 -- 2013
CLA
0 Kudos
Message 5 of 8
(4,784 Views)

If you do not have any option for termination characters as you said and the number of characters varies, how do you know when the message is complete? Is the number of characters in the response know for each command that you write to the device?

 

A for loop with an array of the numbers to be sent would simplify your code considerably.

 

Lynn

0 Kudos
Message 6 of 8
(4,766 Views)

I'd post my VI for this but my employer isn't a fan of posting any of the code base.  But basically I handle it like this:

 

Read bytes at port in a loop, if it equals zero then wait some reasonable time (lets say 100 ms).

If it doesn't equal zero then wait some small amount of time (lets say 2 ms) and read the bytes at port again.  If the two reads are equal it is probably safe to assume you've read everything being sent, if not it is still sending so loop again.

Check the total elapsed time in each loop iteration for time out purposes.

 

Of course you can adjust the wait times to whatever works best with your devices.

Message 7 of 8
(4,761 Views)

If you want to make it intelligent the I think you need to change the timeout value as per your requirement. Use property node of timeout and use it after writing VISA and before reading. The formula to calculate the timeout needs to be done only by you as you know the application very well.

--------------------------------------------------------------------------------------------------------
Kudos are always welcome if you got solution to some extent.

I need my difficulties because they are necessary to enjoy my success.
--Ranjeet
0 Kudos
Message 8 of 8
(4,737 Views)