Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

Serial port buffer - how to read last entry?

Solved!
Go to solution

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 

 

0 Kudos
Message 1 of 7
(6,932 Views)

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 !

greetings from the Netherlands
0 Kudos
Message 2 of 7
(6,929 Views)

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

0 Kudos
Message 3 of 7
(6,926 Views)

Hi

 

In my 4th line I wrote, if still data in the buffer read again, so a while that reads until all data is gone. (bytes available=zero)

That means a while loop instead of a single read.

This is the fastest you can have with little calculations.

greetings from the Netherlands
0 Kudos
Message 4 of 7
(6,922 Views)

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.

0 Kudos
Message 5 of 7
(6,921 Views)

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.

 

 

Message Edited by Henrik Volkers on 03-23-2009 10:15 AM
Greetings from Germany
Henrik

LV since v3.1

“ground” is a convenient fantasy

'˙˙˙˙uıɐƃɐ lɐıp puɐ °06 ǝuoɥd ɹnoʎ uɹnʇ ǝsɐǝld 'ʎɹɐuıƃɐɯı sı pǝlɐıp ǝʌɐɥ noʎ ɹǝqɯnu ǝɥʇ'


0 Kudos
Message 6 of 7
(6,897 Views)
Solution
Accepted by topic author sylvester

To give an idea, place something like this in you diagram:

 

 

 

 

Message Edited by Henrik Volkers on 03-23-2009 10:38 AM
Greetings from Germany
Henrik

LV since v3.1

“ground” is a convenient fantasy

'˙˙˙˙uıɐƃɐ lɐıp puɐ °06 ǝuoɥd ɹnoʎ uɹnʇ ǝsɐǝld 'ʎɹɐuıƃɐɯı sı pǝlɐıp ǝʌɐɥ noʎ ɹǝqɯnu ǝɥʇ'


0 Kudos
Message 7 of 7
(6,891 Views)