Serial
The Serial module lets you use the serial port for communication.
Communication
Select the desired serial port from the ones returned by getAvailablePorts, and pass it as an argument to open. Pass bytes (in ArrayBuffer objects) to write to send them over the connection. Incomming data will come over the onRead signal (also in ArrayBuffer objects). When finished, call close to terminate the connection.
Configuration
Various serial port parameters can be set: BaudRate, FlowControl, Parity, DataBits, and StopBits.
Members
BaudRate
This enum describes the baud rate which the communication device operates with.
Properties:
| Name | Type | Description |
|---|---|---|
BAUD_1200 | 1200 Baud. | |
BAUD_2400 | 2400 Baud. | |
BAUD_4800 | 4800 Baud. | |
BAUD_9600 | 9600 Baud. | |
BAUD_19200 | 19200 Baud. | |
BAUD_38400 | 38400 Baud. | |
BAUD_57600 | 57600 Baud. | |
BAUD_115200 | 115200 Baud. |
DataBits
This enum describes the number of data bits used.
Properties:
| Name | Type | Description |
|---|---|---|
DATA_5 | The number of data bits in each character is 5. It is used for Baudot code. It generally only makes sense with older equipment such as teleprinters. | |
DATA_6 | The number of data bits in each character is 6. It is rarely used. | |
DATA_7 | The number of data bits in each character is 7. It is used for true ASCII. It generally only makes sense with older equipment such as teleprinters. | |
DATA_8 | The number of data bits in each character is 8. It is used for most kinds of data, as this size matches the size of a byte. It is almost universally used in newer applications. |
FlowControl
This enum describes the flow control used.
Properties:
| Name | Type | Description |
|---|---|---|
NO_FLOW_CONTROL | No flow control. | |
HARDWARE_CONTROL | Hardware flow control (RTS/CTS). | |
SOFTWARE_CONTROL | Software flow control (XON/XOFF). |
onError
This signal is emitted when an error occurs. The listener will be passed the following arguments:
- error: A description of the error.
onRead
This signal is emitted whenever data are read from the port. The listener will be passed the following arguments:
- data: An ArrayBuffer containing the data read.
Parity
This enum describes the parity scheme used.
Properties:
| Name | Type | Description |
|---|---|---|
NO_PARITY | No parity bit is sent. This is the most common parity setting. Error detection is handled by the communication protocol. | |
EVEN_PARITY | The number of 1 bits in each character, including the parity bit, is always even. | |
ODD_PARITY | The number of 1 bits in each character, including the parity bit, is always odd. It ensures that at least one state transition occurs in each character. | |
SPACE_PARITY | Space parity. The parity bit is sent in the space signal condition. It does not provide error detection information. | |
MARK_PARITY | Mark parity. The parity bit is always set to the mark signal condition (logical 1). It does not provide error detection information. |
StopBits
This enum describes the number of stop bits used.
Properties:
| Name | Type | Description |
|---|---|---|
ONE_STOP | 1 stop bit. | |
ONE_AND_HALF_STOP | 1.5 stop bits. This is only for the Windows platform. | |
TWO_STOP | 2 stop bits. |
Methods
close()
Closes the currently open port.
Returns:
| Type | Description |
|---|---|
| A promise. | No arguments are passed to the completion handler. |
getAvailablePorts()
Returns a list of serial port names that can be used for communication.
Returns:
| Type | Description |
|---|---|
| A promise. | The completion handler will be passed the following arguments: - ports: An array of port names, each of which could be used for communication. |
getBaudRate()
Get the current serial port baud rate for the desired direction.
Returns:
| Type | Description |
|---|---|
| A promise. | The completion handler will be passed the following arguments: - baud_rate: The serial port's current BaudRate. |
getDataBits()
Get the serial port data bits in a frame.
Returns:
| Type | Description |
|---|---|
| A promise. | The completion handler will be passed the following arguments: - data_bits: The number of DataBits used. |
getFlowControl()
Get the serial port flow control mode.
Returns:
| Type | Description |
|---|---|
| A promise. | The completion handler will be passed the following arguments: - flow_control: The serial port's current FlowControl mode. |
getParity()
Get the serial port parity checking mode.
Returns:
| Type | Description |
|---|---|
| A promise. | The completion handler will be passed the following arguments: - parity: The serial port's current Parity checking mode. |
getStopBits()
Get the serial port stop bits in a frame.
Returns:
| Type | Description |
|---|---|
| A promise. | The completion handler will be passed the following arguments: - stop_bits: The serial port's current StopBits. |
open(port)
Initiates connection using a specific serial port. If another port is already open, it is closed.
Parameters:
| Name | Type | Description |
|---|---|---|
port | String | The device name of the port we wish to use. |
Returns:
| Type | Description |
|---|---|
| A promise. | No arguments are passed to the completion handler. |
setBaudRate(baud_rate)
Set the baud rate for the desired direction.
Parameters:
| Name | Type | Description |
|---|---|---|
baud_rate | BaudRate | The desired BaudRate. |
Returns:
| Type | Description |
|---|---|
| A promise. | No arguments are passed to the completion handler. |
setDataBits(data_bits)
Set the serial port data bits in a frame.
Parameters:
| Name | Type | Description |
|---|---|---|
data_bits | DataBits | The desired DataBits. |
Returns:
| Type | Description |
|---|---|
| A promise. | No arguments are passed to the completion handler. |
setFlowControl(flow_control)
Set the serial port flow control mode.
Parameters:
| Name | Type | Description |
|---|---|---|
flow_control | FlowControl | The desired FlowControl. |
Returns:
| Type | Description |
|---|---|
| A promise. | No arguments are passed to the completion handler. |
setParity(parity)
Set the serial port parity checking mode.
Parameters:
| Name | Type | Description |
|---|---|---|
parity | Parity | The desired Parity mode. |
Returns:
| Type | Description |
|---|---|
| A promise. | No arguments are passed to the completion handler. |
setStopBits(stop_bits)
Set the serial port stop bits in a frame.
Parameters:
| Name | Type | Description |
|---|---|---|
stop_bits | StopBits | The desired StopBits. |
Returns:
| Type | Description |
|---|---|
| A promise. | No arguments are passed to the completion handler. |
write(data)
Writes data to the currently open port. If no port is open, the call will throw an error.
Parameters:
| Name | Type | Description |
|---|---|---|
data | An ArrayBuffer containing the data to be written to the port. |
Returns:
| Type | Description |
|---|---|
| A promise. | No arguments are passed to the completion handler. |