12-10-2023 12:55 PM
I have a serial device whose communication protocol has some weaknesses to where there is a possibility that I go to read from it expecting certain info I may read some random junk, that is undocumented, especially if users actually interact with it directly via its keypad. Basically it's totally possible to become unsynched if erroneous or unexpected data shows up among my 1:1 write/read. I consider this unlikely for the most part, but since this device and the system that will use it has not ever gone into production I have no idea the likelyhood of an operator to do actually need/want to restart the device or use the keypad. The only logic I can think of to programatically re-synch control is if I go to read and get some unexpectected data instead of the data/type I expected the best thing to do would be to get back to a state where there is no data at the port and then retry a set of commands to get back to a known state.
Basically I would like to completely empty the read buffer in order to begin this recovery. I could of course just read the buffer until it timesout (I assume that means it would be empty at that point, I'm using a termination character so timeout should =empty). But that doesn't seem as elegant as something to flush buffer. I'm a little confused about the flush buffer function so I have some questions:
1) when I open the serial connection, I am not setting anything to do with buffer size. Does this mean I (A) don't even have a buffer to flush (B) have a buffer of some OS default size or (C) this is not even the buffer that is flushed by the flush buffer function?
2) is the buffer that is flushed something on the computer side of the communication or on the device itself?
3) What is meant by the flush buffer options when it specifies whether an IO operation will occur? I'm only concerned with flushing the read buffer so I'm not sure why IO to the device would occur but I also don't really understand the details of how serial works at a low enough level.
4) I don't mind flushing both read & write buffers (though I fully expect the write buffer to be empty), but I definitely don't want to send any new commands to the device during the flush, basically if the write buffer is like a queue of sorts, I want the queue items to be erased, not sent to the device. Make sense?
Anyways, any help in how to get to a state where there are no commands queued to write or read from this serial connection would be appreciated. Thanks
12-11-2023 07:22 AM
Clearing the input buffer may not be enough to get rid of extra data, but certainly it helps. Consider to always clear the buffer before sending a command.
1) there is a default size buffer
2) on the computer side
3 and 4) you are really interested in the input buffer, not the output: the buffer content (if any) will be simply discarded
Another possibility to clear the input is to use the infamous property "Bytes at port": pass the property output to a VISA Read. No timeout will elapse.