Skip to content
Oliver Steele edited this page Oct 31, 2022 · 3 revisions

Recipes

Log transmitted and received lines to the text console

The library can be configured to log transmitted and received records to the text console (at the bottom of the source code window).

Note: The documentation for the Processing Serial library does not recommend using the console in this way for high-speed data. "When sending data to the console, such as via print() or println(), note that the console is relatively slow. It does not support high-speed, real-time output (such as at 60 frames per second). For real-time monitoring of serial values, render those values to the Processing window during draw()." The SerialRecord library supports it anyway, for monitoring data that is only sent or received occasionally, or if you are comfortable ignoring the Processing web site's recommendations.

After an instance of SerialRecord has been created, call serialRecord.log(true). For example:

  serialRecord = new SerialRecord(this, serialPort, 2);
  serialRecord.log(true)

At this point, any records sent via serialRecord.send() or received via serialRecord.receive() or its synonymous, will be logged to the console.

Display the most-recently transmitted and received lines on the canvas

Display the most recently transmitted (TX) and received (RX) values on the canvas.

Note that this is already the default behavior.

  serialRecord.logToCanvas();

Stop displaying the most-recently transmitted and received lines on the canvas

The canvas display of the most-recently transmitted and received lines is helpful during initial development, but at some point you may require a canvas that contains only what you have drawn. At this time, call any one of the following functions.

  // Prevents SerialRecord from drawing on the canvas.
  serialRecord.logToCanvas(false);
  // Prevents SerialRecord from drawing on the canvas.
  // Log to the console, instead.
  serialRecord.logToCanvas(false);
  serialRecord.logToConsole(true);
  // Prevents SerialRecord from drawing on the canvas.
  // Log to the console, instead.
  // This is equivalent to the previous fragment.
  serialRecord.log(true, false);

Direct the Arduino to echo received values

In a system where data is transmitted from Processing to the Arduino (where the Processing sketch contains serialRecord.send() and the Arduino sketch contains serialRecord.receive()), there is a facility for requesting the Arduino to send back the latest values that it received.

This can be used as an end-to-end test that the connection is basically working.

There are two methods to do this:

  1. Call serialRecord.requestEcho(). Each time this function is called, it requests an echo from the Arduino. The SendMultipleValues example contains an example of this, where clicking on the canvas requests an echo.
  2. Call serialRecord.periodicEchoRequest(100) in setup(). This configures the serial port that this instance of SerialRecord is connected to, to automatically request an echo about every 100 milliseconds. The echo is requested at the beginning of a call to draw(), so it will not occur on exact 100 ms boundaries, and will not occur at all if noLoop() has been called.