Accessor: net/SerialPort


This accessor sends and/or receives data from a serial port on the host. The accessor lists all the serial ports that it finds as options. Whenever an input is received on the toSend input, the data on that input is sent to the serial port. Whenever data is received from the serial port, the data is produced on the received output. When wrapup() is invoked, this accessor closes the serial port.

The send and receive types can be any of those supported by the host. The list of supported types will be provided as options for the sendType and receiveType parameters. For the Nashorn host, these include at least 'string', 'number', 'JSON', and a variety of numeric types. The type 'number' is equivalent to 'double'.

The data chunks sent on the received output depend on the receiveType parameter. Each output will be of the specified type. Note that if receivedType is 'string' or 'JSON' then the output is produced only after a null byte is received on the serial port. If the type is 'JSON', then this accessor will attempt to parse the JSON. If parsing fails, then the raw byte array will be sent to the invalid output port.

When type conversions are needed, e.g. when you send a double with sendType set to int, or an int with sendType set to byte, then a "primitive narrowing conversion" will be applied, as specified here: .

For numeric types, you can also send an array with a single call to this.send(). The elements of the array will be sent in sequence.

Accessors that extend this one can override the toSendInputHandler function to customize what is sent.

This accessor requires the 'serial' module.


  • $$Id$$
  • Edward A. Lee, Beth Osyk, Chadlia Jerad, Victor Nouvellet
Name Type Description
toSend The data to be sent over the serial port.
Name Type Description
received The data received from the serial port converted to the specified type.
invalid Byte arrays that fail to parse in JSON.
Name Type Description
port int The port on the host to connect to. This defaults to the last (most recently added, presumably) serial port in the list of serial ports on the host.
receiveType string See above.
sendType string See above.


(static) initialize()

Initiate a connection to the server using the current parameter values, set up handlers for for establishment of the connection, incoming data, errors, and closing from the server, and set up a handler for inputs on the toSend() input port.


(static) setup()

Set up the accessor by defining the parameters, inputs, and outputs.


(static) toSendInputHandler()

Handle input on 'toSend' by sending the specified data over the radio.


(static) wrapup()

Close the web socket connection.