Buffer Memory Status
|Top Previous Next|
You cannot effectively use a buffer without knowing what its status is. Is it overflowing? Can you add more data? etc. Thus, each of the serial buffers has certain properties which allow you to monitor it:
The RX buffer
You can check the total capacity of the buffer with the ser.rxbuffsize property. You can also find out how much committed data the RX buffer currently contains with the ser.rxlen property (see Serial Settings for explanation of what committed data is).
Sometimes you need to clear the RX buffer without actually extracting the data. In such cases the ser.rxclear comes in handy.
The TX buffer
Similarly to the RX buffer, the TX buffer also has a ser.txbuffsize property which lets you discover its capacity.
Unlike the RX buffer, the TX buffer has two "data length" properties: ser.txlen and ser.newtxlen. The txlen property returns the amount of committed data waiting to be sent from the buffer (you commit the data by using the ser.send method). The newtxlen property returns the amount of data which has entered the buffer, but has not yet been committed for sending.
The TX buffer also has a ser.txfree property, which directly tells you how much space is left in it. This does not take into account uncommitted data in the buffer — actual free space is ser.txfree-ser.newtxlen!
When you want to clear the TX buffer without sending anything, use the ser.txclear method.
An example illustrating the difference between ser.txlen and ser.newtxlen:
Don't step through the code. The sending is fast — by the time you reach x and y by stepping one line at a time, the buffer will be empty and x and y will be 0. Set a breakpoint at the end of the code, and then check the values for the variables (by using the Watch).