|Top Previous Next|
In the previous section, we explained how to handle an incoming stream of data. You could say it was incoming-data driven. Sometimes you need just the opposite — you need to perform operations based on the sending of data.
For example, supposing that in a certain system, you need to send out a long string of data when a button is pressed. A simple code for this would look like this:
The code above would work, but only if at the moment of code execution the necessary amount of free space was available in the TX buffer (otherwise the data would get truncated). So, obviously, you need to make sure that the TX buffer has the necessary amount of free space before sending. A simple polling solution would look like this:
Again, this is not so good, as it would block other event handlers. So, instead of doing that, we would employ a code that uses on_ser_data_sent:
When we press the button, on_button_pressed event is generated, so now the system knows we have a string to send. Using ser.notifysent we make the system fire the on_ser_data_sent event when the necessary amount of free space becomes available. This event will only be fired once — and will be fired immediately if there is already enough available space.
Within the on_ser_data_sent event handler we put the data in the TX buffer and start sending it.