{"id":426,"date":"2019-04-23T17:48:03","date_gmt":"2019-04-23T17:48:03","guid":{"rendered":"https:\/\/arduino-developer.com\/arduino\/?p=426"},"modified":"2021-02-03T09:22:38","modified_gmt":"2021-02-03T09:22:38","slug":"uart-tx","status":"publish","type":"post","link":"https:\/\/arduino-developer.com\/arduino\/programming\/uart\/uart-tx","title":{"rendered":"UART TX"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Waiting for TX to complete &#8211; Flush<\/h4>\n\n\n\n<p>When you issue a Serial.print() or Serial.write() , the function returns almost immediately.  Instead of waiting until the string or data has been transmitted, it sets up a buffer for the data which is then transmitted via interrupts one byte at a time.  I.e., your program keeps running, with the transmitting of bytes happening in the background. <\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Serial.flush()<\/h5>\n\n\n\n<p>Using this in your code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  Serial.flush();<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/www.arduino.cc\/en\/Serial\/Flush\">https:\/\/www.arduino.cc\/en\/Serial\/Flush<\/a><\/p>\n\n\n\n<p>Causes program execution to wait for the transmission of outgoing serial data to complete.  Execution is stalled.<\/p>\n\n\n\n<p><strong><em>NOTE &#8211; this does not mean the byte has actually been transmitted!<\/em><\/strong><br> It just means the byte has been passed to the microcontroller TX register and is being transmitted. <\/p>\n\n\n\n<p>In our tests with a Mega2560 we also found it did not necessarily always return straight away, sometimes we saw long delays before it returned.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"> <br>Waiting for TX to complete &#8211;  Using Microcontroller Registers<\/h4>\n\n\n\n<p>Wait for the TX of a byte to complete using TXCn (USART Transmit Complete) bit<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Arduino Mega2560<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>    while (!(UCSR3A &amp; 0x40))      \/\/Bit6 = TXCn: USART Transmit Complete\n      ;<\/code><\/pre>\n\n\n\n<p>Use UCSR0A, UCSR1A, UCSR2A, UCSR3A depending on serial port (Serial &#8211; Serial3)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Waiting for TX to complete &#8211; Flush When you issue a Serial.print() or Serial.write() , the function returns almost immediately. Instead of waiting until the string or data has been transmitted, it sets up a buffer for the data which is then transmitted via interrupts one byte at a time. I.e., your program keeps running, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-426","post","type-post","status-publish","format-standard","hentry","category-uart"],"_links":{"self":[{"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/posts\/426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/comments?post=426"}],"version-history":[{"count":6,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/posts\/426\/revisions"}],"predecessor-version":[{"id":641,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/posts\/426\/revisions\/641"}],"wp:attachment":[{"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/media?parent=426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/categories?post=426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/tags?post=426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}