Member-only story
Hello dear reader!
Recently I found myself sending sensor data from a microcontroller to my computer via the serial port, and ran into trouble recording the data with Processing. At high data rates, new data arrives before I can finish processing the old data!
In my endeavor to fix this, I found a way to control serial communications logging from inside Processing which also works from any other programming language. I also wrote a nifty Serial logging Java class which you may find useful in your logging adventures to come.
Naive Implementation
Here was my initial implementation:
If all you’re doing is slow serial comms, this code makes sense. When there’s new data, you pull it in and print it, or process it however you want.
However, if you want to draw or handle user input in the draw() loop, our function starts getting crowded. The first drawback is that the draw() loop only runs at 60Hz by default, limiting our data receiving rate. The second is, because myPort can receive new data before we finish reading it, we can get stuck in the while
loop. A high data rate would starve any code we put in the draw loop.