Pauses program execution until the UART’s output FIFO has drained
Device
Nothing
This blocking methods pauses Squirrel until all the bytes in the UART buffer have been sent on the wire. At this point, execution resumes.
The following code envisages the imp connected to a computer and an Arduino on the same bus but with different parameters. It uses uart.flush() to ensure the buffer’s contents have all been sent before the bus is shut down briefly before being reconfigured.
// Assign parallel aliases for the UART according to role | |
arduino <- hardware.uart57; | |
computer <- hardware.uart57; | |
function relay() { | |
// UART initially configured for Arduino input | |
local byte = arduino.read(); | |
if (byte != -1) { | |
// We have a valid byte to relay so reconfigure the UART | |
// for output to the computer. Remember: disable arduino first | |
server.log("Switching UART to computer..."); | |
arduino.disable(); | |
// Configure UART for computer connection and write byte | |
computer.configure(115200, 8, PARITY_NONE, 1, NO_CTSRTS); | |
computer.write(byte); | |
// With the byte sent to the computer, reassign the UART | |
// back to the data source, Arduino, and await next byte | |
server.log("Switching UART to Arduino"); | |
// Flush the FIFO first to make sure the byte has been sent | |
// before we disable the computer link | |
computer.flush(); | |
computer.disable(); | |
// Configure the UART to read from Arduino | |
arduino.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, relay); | |
} | |
} | |
// Initialize by starting to read from Arduino | |
server.log("Switching UART to Arduino"); | |
arduino.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, relay); |