SuperCollider CLASSES

SerialPort

serial port interface
Inherits from: Object

Class Methods

*new (port, baudrate: 9600, databits: 8, stopbit: true, parity, crtscts: false, xonxoff: false, exclusive: false)

opening the port.

Arguments:

port

device path or index.

baudrate

baudrate [4800..230400]

databits

5 | 6 | 7 | 8

stopbit

true | false

parity

nil | 'even' | 'odd'

crtscts

hardware flow control (true | false)

xonxoff

software flow control (true | false)

exclusive

open the device exclusively (true | false)

*devices

returns an array of available device.

SerialPort.devices;

*listDevices

prints to postbuffer

SerialPort.listDevices;

*devicePattern

*devicePattern = value

change device selection

SerialPort.devicePattern = "/dev/ttyUSB*"; // Linux usb serial
SerialPort.devices;

SerialPort.devicePattern = nil;
SerialPort.devices;

*closeAll

close all ports.

Inherited class methods

Undocumented class methods

*cleanupAll

Instance Methods

-next

Read a byte from the device. Non-blocking read.

-read

Read a byte from the device. Blocking read.

-rxErrors

Rx errors since last query.

-put (byte, timeout: 0.005)

Write a byte to the device. Always blocks.

-putAll (bytes, timeout: 0.005)

Write multiple bytes to the device. Collection may be Int8Array or String.

-doneAction

-doneAction = value

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 to the post window.

-close

close the port.

Inherited instance methods

Undocumented instance methods

-isOpen

Examples

(
p = SerialPort(
    "/dev/tty.usbserial-181",
    baudrate: 9600,
    crtscts: true);
)

// read a byte from the device

p.next;            // doesn't block
fork{p.read.postln};    // may suspend thisThread - should be called within a routine

// write a byte to the device

fork{p.put(42)};    // may suspend thisThread - should be called within a routine

// write multiple bytes to the device

p.putAll("whaddayawant");
p.putAll(Int8Array[13, 10]);

p.doneAction = { "my serial port got closed".postln; }

p.close;    // close the port

SerialPort.closeAll;    // close all ports

Arduino write example

First load the sketch Examples/Communication/Dimmer. See http://www.arduino.cc/en/Tutorial/Dimmer

NOTE: Always make sure the serial monitor is closed in the Arduino application before opening the port in SuperCollider.
(
p = SerialPort(
    "/dev/tty.usbserial-A800crTT",    //edit to match your port. SerialPort.listDevices
    baudrate: 9600,    //check that baudrate is the same as in arduino sketch
    crtscts: true);
)

//send serial data - slow pulsating
(
r= Routine({
    inf.do{|i|
        p.put(i.fold(0, 100).linexp(0, 100, 1, 255).asInteger.postln);
        0.02.wait;
    };
}).play;
)

r.stop;
p.close;

Arduino read example

First load the sketch Examples/Communication/Graph. See http://www.arduino.cc/en/Tutorial/Graph

NOTE: Always make sure the serial monitor is closed in the Arduino application before opening the port in SuperCollider.
(
p = SerialPort(
    "/dev/tty.usbserial-A800crTT",    //edit to match your port. SerialPort.listDevices
    baudrate: 9600,    //check that baudrate is the same as in arduino sketch
    crtscts: true);
)

//read 10bit serial data sent from Arduino's Serial.println
(
r= Routine({
    var byte, str, res;
    99999.do{|i|
        if(p.read==10, {
            str = "";
            while({byte = p.read; byte !=13 }, {
                str= str++byte.asAscii;
            });
            res= str.asInteger;
            ("read value:"+res).postln;
        });
    };
}).play;
)

r.stop;
p.close;