This class provides basic support for serial port communication. Ports are opened with *new and closed with -close.
Each SerialPort object uses an 8KB internal buffer and reads data as soon as it is available. If the data is not read out of the buffer and the buffer fills up, incoming bytes will be dropped. Use -rxErrors to get a count of the number of bytes dropped.
Since it is constantly polling the port for available data, a SerialPort object knows almost immediately when the port has been lost. When this happens, it will call the -doneAction callback and mark itself as closed.
Creates and opens the port. Throws if creation fails; this may be because the port does not exist, the port could not be opened, or the settings were invalid.
port |
A String representing the port to be opened. (An Integer index into *devices is allowed, but this is deprecated.) |
baudrate |
Integer baud rate, typically in the range |
databits |
Bits per character. Typically 8, but can be any integer. |
stopbit |
A Boolean indicating whether to use two stop bits ( |
parity |
Whether the port uses even, odd, or no parity. Pass |
crtscts |
A Boolean indicating whether to use hardware flow control (RTS/CTS signals). |
xonxoff |
A Boolean indicating whether to use software flow control (XON/XOFF signals). |
exclusive |
A Boolean indicating whether to open the device exclusively. This option is not implemented on Windows. |
crtscts
and xonxoff
cannot both be true; *new
will throw an error if both are set.
Retrieve an array of available devices represented as Strings. On macOS and Linux, this list is obtained using a number of regular expression rules on files in the /dev/
directory. On Windows, this is obtained using a registry key. The matching rules are designed to be identical to that of the Arduino IDE.
For backward compatibility, if SerialPort.devicePattern
is not nil, SerialPort.devicePattern.pathMatch
is returned instead of the default behavior.
Prints the list of available devices, one per line. Shorthand for SerialPort.devices.do(_.postln)
.
If set to a non-nil value, SerialPort.devicePattern
instead returns SerialPort.devicePattern.patchMatch
. That is, the value of this class variable is used as a file glob.
This is a legacy feature and no longer recommended. File globbing alone is not powerful enough to capture a general set of possible serial port paths, and this level of customization was not necessary for SerialPort.devices
. If you need to refine the results returned by SerialPort.devices
, it is better to do your own matching or filtering.
Calls -close on all ports.
Deprecated; use *closeAll instead.
Whether this object represents a valid serial port connection.
Read a byte from the device. Non-blocking read.
Read a byte from the device. Blocking read.
RX (receive) errors since last query. An RX error occurs when the internal buffer is completely full. This count is reset to 0 every time this method is called.
Write a byte to the device. Always blocks.
byte |
An Integer or Char. Only values in the range from 0 to |
timeout |
Unused, deprecated. |
A Boolean indicating whether the write was successful.
Write multiple bytes to the device.
bytes | |
timeout |
Unused, deprecated. |
A Function which will be evaluated if the port gets closed (maybe unexpectedly so, due to hardware failure or accidental disconnection). This allows you to for example to make a backup solution and activate it (like using fake input data for your algorithm, or trying to reopen the device). By default it will post a message reading "SerialPort X was closed".
Closes the port.
First load the sketch Examples/Communication/Dimmer. See http://www.arduino.cc/en/Tutorial/Dimmer.
First load the sketch Examples/Communication/Graph. See http://www.arduino.cc/en/Tutorial/Graph.