BO.Serial Class
Enables use of Hardware (UART) and Software serial ports on the board.
Constructor
BO.Serial
-
opts
Parameters:
-
opts
ObjectOptions:
- board {IOBoard} A reference to the IOBoard instance.
- port {Number} The serial port to use (HW_SERIAL1, HW_SERIAL2, HW_SERIAL3, SW_SERIAL0, SW_SERIAL1, SW_SERIAL2, SW_SERIAL3)
- baud {Number} The baud rate of the serial port. Default = 57600.
- rxPin {Number} [SoftwareSerial only] The RX pin of the SoftwareSerial instance
- txPin {Number} [SoftwareSerial only] The TX pin of the SoftwareSerial instance
Example:
// Use a SoftwareSerial instance
var serial = new BO.Serial({
board: arduino,
port: BO.Serial.SW_SERIAL0,
baud: 57600,
txPin: 10,
rxPin: 11
});
serial.addEventListener(BO.SerialEvent.DATA, function (event) {
console.log(event.data);
});
serial.startReading();
// Use a HardwareSerial instance (pins RX1, TX1 on Leonardo, Mega, Due, etc)
var serial = new BO.Serial({
board: arduino,
port: BO.Serial.HW_SERIAL1,
baud: 57600
});
serial.addEventListener(BO.SerialEvent.DATA, function (event) {
console.log(event.data);
});
serial.startReading();
Item Index
Methods
Properties
- Serial.HW_SERIAL0 static
- Serial.HW_SERIAL1 static
- Serial.HW_SERIAL2 static
- Serial.HW_SERIAL3 static
- Serial.SW_SERIAL0 static
- Serial.SW_SERIAL1 static
- Serial.SW_SERIAL2 static
- Serial.SW_SERIAL3 static
Methods
addEventListener
-
type
-
listener
Parameters:
-
type
StringThe event type
-
listener
FunctionThe function to be called when the event is fired
close
()
Close the serial port. A new instance must be created in order to reopen the port.
dispatchEvent
-
type
-
optionalParams
Parameters:
-
type
EventThe Event object.
-
optionalParams
ObjectOptional parameters passed as an object. return {boolean} True if dispatch is successful, false if not.
flush
()
For HardwareSerial, waits for the transmission of outgoing serial data to complete. For SoftwareSerial, removes any buffered incoming serial data.
hasEventListener
-
type
Parameters:
-
type
StringThe event type return {boolean} True is listener exists for this type, false if not.
listen
()
For SoftwareSerial only. Only a single SoftwareSerial instance can read data at a time. Call this method to set this port to be the reading port in the case there are multiple SoftwareSerial instances.
removeEventListener
-
type
-
listener
Parameters:
-
type
StringThe event type
-
listener
FunctionThe function to be called when the event is fired
startReading
-
maxBytesToRead
Start reading the serial port.
Parameters:
-
maxBytesToRead
Number[optional] The number of bytes to read on each iteration of the main loop.
stopReading
()
Stop reading the serial port.
write
-
val
Write an array of data.
Parameters:
-
val
Number | ArrayA single byte or an array of bytes to write
Properties
Serial.HW_SERIAL0
Unknown
static
Not currently used. Corresponds to the default Arduino Serial port
Serial.HW_SERIAL1
Unknown
static
Pins RX1 and TX1 on the board
Serial.HW_SERIAL2
Unknown
static
Pins RX2 and TX2 on the board
Serial.HW_SERIAL3
Unknown
static
Pins RX3 and TX3 on the board
Serial.SW_SERIAL0
Unknown
static
One of four software serial instances
Serial.SW_SERIAL1
Unknown
static
One of four software serial instances
Serial.SW_SERIAL2
Unknown
static
One of four software serial instances
Serial.SW_SERIAL3
Unknown
static
One of four software serial instances