LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

confused about serial read buffer

I am communicating with a motor controller via the serial port, and through search I've solved most of my problems.  However, I'm getting some behavior I don't understand with the buffer and serial read.
 
For every command I send, there is a response.  What I'd like to do is essentially this:
 
SEND command
SEND command
READ response
 
where the first command is just an initialization to which I'm not terribly interested in the response, and the second is a query.  If I do it as above, I end up reading a different response every time as it seems to cycle through the buffer of input strings.
 
SEND command
FLUSH buffer
SEND command
READ response
 
exhibits the same behavior, but with some empty strings mixed in.
 
SEND command
READ response
SEND command
READ response
 
also cycles through various inputs, while:
 
FLUSH buffer
SEND command
READ response
SEND command
READ response
 
seems to work ok.  I must be misunderstanding what VISA flush buffer actually flushes, because I feel like {SEND FLUSH SEND READ} should work for me.  I'm sure there are far more grevious inefficiencies in my code than extra serial read commands, but I'd rather not depend on matching them up so precisely in order to be reading the correct status.
 
0 Kudos
Message 1 of 3
(1,670 Views)
If you are immediately doing a flush buffer after the write and you see unexpected responses then next time you do a read, I suspect that the instrument is still sending (or hasn't sent anything yet) after the flush is executed. I suppose you could put a wait after the first write and then do a flush buffer but I think the best solution is to do a read after every write like you have in the last example.
0 Kudos
Message 2 of 3
(1,667 Views)
ahh, good point-- most likely that flush is getting rid of non or half-written commands.  Thanks!
0 Kudos
Message 3 of 3
(1,655 Views)