03-22-2009 10:18 AM
Hello,
I try to read data from an instrument connected to the serial port. The instrument puts out one line of data, terminated with CR LF in pre-set intervals (e.g. 0.2 sec., 0.5 sec.,etc). I want to read the most recent line at a different time interval (e.g. 1 sec.). What happens is, that the buffer fills up, and when I read I always get whole buffer content, starting with the first (oldest) line of data. After read, I flush the buffer to prevent overflow.
From the string I red, I serach for occurence of CRLF and take the data before the match. This will give me the first (oldest) data line, sometimes incomplete.
What I want, is the latest line. There must be a easy way to achieve that?
I can not use flow control with my instrument.
Any help appreciated
Sylvester
Solved! Go to Solution.
03-22-2009 12:13 PM
but you can read until a specified end character.
specify LF as an end character and enable the use of the endcharacter.
Ask more data than available in one line and you get the complete line.
When still data in the buffer ask again until no data available and you have the last line.
No flushing needed anymore and no incomplete lines !
03-22-2009 12:38 PM
I am not sure, if that solves my problem. The instrument will add a new line terminated with CRLF every 0.2 seconds, while I read the port once every second using a loop. Means there will be several lines with terminations in the buffer, when I read it. With termination enabled, this will only read the first (oldest) line? I thought I need to read the complete buffer and search for (the last) LF in the result string.
Alternatively, I wonder if there is a way to read the buffer 'backwards', to the last but one CRLF sequence.
Sylvester
03-22-2009 01:06 PM
03-22-2009 01:11 PM
I think what Albert was saying was that you could have the VISA Read in a loop. You would have a VISA Bytes at Serial Port in the loop and when it returns 0, the loop is terminated. Each result from the VISA Read would be placed in an array and you could use a shift register and Build Array function to do that.
Other options would be to flush the buffer before doing a read. The next read would have the latest data. Another option would be to turn off the termination character and use the VISA Bytes at Serial Port to determine how many bytes to read. You would still have a partial string at the end.
03-23-2009 04:12 AM - edited 03-23-2009 04:15 AM
Whenever I have a device that push data where only the most recent value is requestet at unknown time ...
I set up a independed loop (parallel task) that continously read the device and feed the recent value to a functional global.
03-23-2009 04:37 AM - edited 03-23-2009 04:38 AM
To give an idea, place something like this in you diagram:
