UART
Out of the box, the Raspberry Pi is also equipped with the good old serial port called UART. For serial communications, the baud rate, the bit count and parity can all be set via the open()
method.
import { UART } from 'miletus';
...
let uart = new UART();
await uart.open({baudRate: 9600, port: 'miniUART',
parity: 'none', stopBit: 1, bitCount: 8});
Methods
new UART()
Create a new UART connection object.
open(options)
options
UARTOptions - Connection options object.baudRate
number - Serial baud rate.port
string - Which serial port to connect to. Supported values:miniUART
,firstPL011
anddevUSB
.parity
string - Parity bit. Supported values:none
,odd
,even
,mark
andspace
.stopBit
number - Stop bit. Supported values:1
,1.5
and2
.bitCount
number - Data bits.dev
string (optional) - Path to the UART interface. Only use in combination withdevUSB
ports. For example:/dev/ttyUSB0
.
Open the UART connection.
Returns Promise
- Resolves when the connection is created successfully.
close()
Close the UART connection.
writeBuffer(buffer, length)
buffer
ArrayBuffer - Buffer to write.length
number - Length of the buffer data to write.
Write a buffer of data.
Returns Promise<number>
- Resolves with the number of bytes written.
readBuffer(length)
length
number - Number of bytes to read.
Read a buffer of data.
Returns Promise<ArrayBuffer>
- Resolves with an ArrayBuffer which corresponds to the read buffer.
canRead([timeout])
timeout
number (optional) - Test period in milliseconds.
Check if data can be read from the port. Status is tested for a period of time given by the timeout parameter (in milliseconds).
Returns Promise<boolean>
- Resolves with a boolean indicating if data can be read from the port.
canWrite([timeout])
timeout
number (optional) - Test period in milliseconds.
Check if data can be written to the port. Status is tested for a period of time given by the timeout parameter (in milliseconds).
Returns Promise<boolean>
- Resolves with a boolean indicating if data can be written to the port.
setRTSToggle(enable)
enable
boolean - Enable/disable RTS driven communication.
Returns Promise
- Resolves when the RTS driven communication is enabled/disabled.
waitingData()
Returns Promise<number>
- Resolves with the number of bytes waiting for reading.
sendingData()
Returns Promise<number>
- Resolves with the number of bytes waiting for sending.
modemStatus()
Returns Promise<number>
- Resolves with the modem status code.
setRTS(value)
value
boolean - The value of the RTS signal.
Returns Promise
- Resolves when the method finishes.
getCTS()
Returns Promise<boolean>
- Resolves with the value of the CTS signal.
setDTR(value)
value
boolean - The value of the DTR signal.
Returns Promise
- Resolves when the method was executed.
getDSR()
Returns Promise<boolean>
- Resolves with the value of the DSR signal.